litedown icon indicating copy to clipboard operation
litedown copied to clipboard

rgl displays not showing up

Open dmurdoch opened this issue 9 months ago • 4 comments

I just had a report that the vignettes in rgl are not displaying the graphics properly. They were using R Markdown and markdown::html_format as the output driver, and that stopped working with this commit https://github.com/rstudio/markdown/commit/b4c328da067dbe22b2169325d95d0a767f89ca74 to that package.

For example, this vignette: https://github.com/dmurdoch/rgl/blob/8e12a29031f2b0e52fc204cc1b947fcdabd6bdf0/vignettes/transparency.Rmd should display movable shapes, but it just displays blank blocks now.

Of course, markdown is no longer being maintained so I can't post an issue there, and I tried to follow the instructions to use litedown::html_format instead, with no luck.

I can switch rgl back to using rmarkdown::html_vignette; it still works, but I liked avoiding Pandoc with markdown::html_format, and from the design goals, it seems like litedown should suit me if I can figure out how to get it to work.

I think the things I need to know are necessary changes to knit_print methods and sew methods to work in litedown (and maybe changes to code to detect that I'm in litedown). Are those documented anywhere?

dmurdoch avatar Apr 12 '25 13:04 dmurdoch

I'm glad that you asked. This is an area where I haven't decided what to do yet. I'd like to give package authors a way to register their CSS/JS assets for their widgets, so litedown can pick them up and automatically insert them to the HTML <head>.

For now, the public API I have provided is litedown::vest(), to which you can pass CSS/JS paths. This is a manual approach. That is, the Rmd document authors need to know which CSS/JS assets they need for their documents (there are some examples here: https://yihui.org/litedown/#sec:simple-dt). This is definitely not a reasonable requirement to users.

I do have internal API to register CSS/JS, but I'd like to know if vest() works for you before publicizing the internal mechanism. You can do a quick test.

yihui avatar Apr 15 '25 14:04 yihui

I'm not sure if vest() will be enough. I'm working through some tests with leaflet, which might be a bit easier than rgl, but I hit this snag. When I try to call vest on one of its dependencies, I get this error:

litedown::vest(css = "/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/leaflet/htmlwidgets/lib/leaflet/leaflet.css")
Error in file(con, "rb") : cannot open the connection

That file definitely exists; it was obtained using system.file("htmlwidgets/lib/leaflet/leaflet.css", package = "leaflet"). I can't quite follow what it is trying to do: it appears to read the file, then litedown:::resolve_url() ends up being called, and it dies.

dmurdoch avatar Apr 15 '25 21:04 dmurdoch

I have a fix for the issue above, but I think I don't understand the intention of the original code, so it might cause other problems. The original code that goes wrong looks like this: https://github.com/yihui/litedown/blob/637bdf024a413116174f09c25f7d366c4bd43a94/R/utils.R#L1290-L1308 Here x is supposed to be the a filename or URL pointing to a dependency to link or include. When x is an absolute file path, none of is_web, is_rel, link1 or link2 are true, so the contents should be inserted, and that's what happens at the end with the call to resolve_external(). However, that function sees some content in the leaflet file that makes it think it needs to download something, and then things go wrong.

What I don't understand is the logic that relative paths should be distinguished from absolute paths. My example works fine if litedown just reads and inserts the file when is_web is FALSE.

dmurdoch avatar Apr 17 '25 15:04 dmurdoch

The error was due to this line in leaflet.css (which uses an obsolete behavior property): https://github.com/rstudio/leaflet/blob/947ea176d177fe519ac6d6a9a2366bef033c4c81/inst/htmlwidgets/lib/leaflet/leaflet.css#L109 The R package leaflet is still using a version of leaflet.js that is 8 years old, and the current leaflet.js no longer contains that behavior property: https://github.com/Leaflet/Leaflet/blob/main/dist/leaflet.css

I have excluded the behavior property when embedding url() resources in CSS, so the above leaflet.css should work now.

yihui avatar Apr 26 '25 16:04 yihui