rstudio/rmarkdown-cookbook

rgl example does not display

dmenne opened this issue · 37 comments

https://bookdown.org/yihui/rmarkdown-cookbook/rgl-3d.html

Does not display rgl (Windows, RStudio, R 4.2.0)

Crosscheck: Display ok when only code is run

cderv commented

Can you try render in R console using rmarkdown::render().

I am under the impression this is an issue with RStudio 🤔

cderv commented

I think this is related to this : rstudio/rstudio#5678

cderv commented

@dmurdoch if I may ping you here:

  • We have an example in the Cookbook that seems to work ok when Rmd is rendered using rmarkdown::render()
  • It does not when knitting the document in RStudio IDE

Are you aware of such behavior ? Is this related to rstudio/rstudio#5678 according to you ?

Thank a lot!

  • Produces valid html report with html when run with console/render
  • When built in RSTudio, no rgl plot is displayed, but the data are there. Even when viewed with Chrome directly from Windows-Explorer double-click.
    distill.zip

@cderv: No, rstudio/rstudio#5678 was something different.

I'm not really sure what the issue is here, but I would guess that rgl thinks it can't display in that context. I don't use hook_webgl myself, and it's only present for legacy support. I'd recommend using the auto-printing option, i.e. in the setup chunk use

 rgl::setupKnitr(autoprint = TRUE)

and then rgl graphics should act almost like base graphics.

Hi, Duncan,

library(rgl)
rgl::setupKnitr(autoprint = TRUE) #### New
knitr::knit_hooks$set(webgl = hook_webgl)

No change.

@dmenne: You don't want to set the hook; that's all done by setupKnitr now.

But I'm not sure I understood exactly what the issue is, my comment up above may be about a different issue. Can you give me reprex instructions?

distill.zip

I did a diff on theses two and did not notice anything suspicious with the exception of the slightly different random ids.

Removed the hook: no change

This works fine for me:

---
title: Embed 3D plots with rgl
output: html_document
---

Set up to include **rgl** plots:

```{r, setup}
library(rgl)
setupKnitr(autoprint = TRUE)
```

See if it works for this 3D plot:

```{r, test-rgl}
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
plot3d(x, y, z, col = rainbow(1000))
```

I'm on macOS. If you're on Windows, you need the very newest rgl, version 0.108.3.2; R 4.2.0 triggered a bug in previous ones. But I would have expected different symptoms than you describe if you were getting caught by that.

I have 0.108.3.2, and R 4.2.0
Your example above has the same symptoms (good on render, bad when created in Rstudio). Also tried Firefox (default is Chrome), same. Even Edge (ok, that's Chromium now).
With and without autoprint and hook also makes no difference.

cderv commented

This works fine for me:

@dmurdoch does this works for you if you Render the file using the RStudio IDE ?
It only works for me too if I use rmarkdown::render()

It seems like the IDE is interferring.

No, rstudio/rstudio#5678 was something different.

I'll open a new issue then in the RStudio repo.

@cderv: Yes, it's fine in the macOS Rstudio IDE.

I'd guess the issue is related to rstudio/rstudio#11043 somehow, though I can't see how. rgl 0.108.3.2 was designed to fix the crash reported there. Conceivably the previous workaround (setting options(rgl.debug = TRUE) before library(rgl)) could make a difference.

I'll try to fire up Windows and see if I can reproduce the problem.

cderv commented

FWIW I am using 2022.06.0-daily+420 RStudio version on Windows 11

I can reproduce in Windows 10 under VirtualBox, but so far haven't spotted what's going on.

No change with rgl.debug = TRUE.
Also tried with daily+421, noch change

If you can easily build rgl, you could also try remotes::install_github("dmurdoch/rgl"), which has quite a few changes beyond those in the current release, but I can't think of anything related to this issue, so I wouldn't be hopeful.

Build 0.108.40, no change

Have you tried to do a diff on the good and the bad file in the zip file I posted above?

I made the two version comparable by adding set.seed(1).
distill_RStudio.zip
2022-05-19 20_21_22-cmd

This gave an revealing diff when the viewport is set - see image

cderv commented

Thanks that is helpful (I think). I haven't yet find what part the IDE could cause this behavior though.

@dmenne, that's really helpful. If I change the code to

p <- plot3d(x, y, z, col = rainbow(1000))
par3d("viewport")

I see all zeros for the viewport when run in RStudio, and the usual 0 0 256 256 in rmarkdown::render. I should be able to track down where that's coming from.

Here's a workaround. Before starting rgl, run options(rgl.useNULL = TRUE). This prevents rgl from trying to use OpenGL, and in my tests it gets the viewport right, and everything is fine. (The document will only use WebGL. It doesn't display in RStudio in the VM I'm using, but it is fine in an external browser.)

I'll try to debug what's going wrong with the version that actually uses OpenGL.

Confirmed, but it also works in the RStudio viewer, so this seems to be a problem of VM.

Gives 256/256 for me even when it does not display (but reserves empty space on the page)

{r, echo = TRUE, results = "asis"}
print(par3d("viewport"))

 x      y  width height 
 0      0    256    256 

Could you try running this code chunk:

```{r, test-rgl}
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y) 
p <- plot3d(x, y, z, col = rainbow(1000))
par3d("viewport")
s <- scene3d()
s$rootSubscene$par3d$viewport
```

I'd expect the two ways of printing the viewport to give the same results. On my system when I click knit I get all zeros from both, and 0 0 256 256 from both when using rmarkdown::render. What you showed in the file comparison is consistent with this, because for some calculations the viewport is calculated relative to the root viewport, and dividing by width and height of 0 would give NaN.

cderv commented

On my system when I click knit I get all zeros from both, and 0 0 256 256 from both when using rmarkdown::render.

FWIW This is what I get also on my side (Windows 11, RStudio IDE, R 4.2, rgl 0.108.40)

Both in RStudio and from console all results are 0 0 256 256.

(Windows 10, RStudio IDE/console, R 4.2, rgl 0.108.40

Sorry, correction. The above was in the version with

options(rgl.useNULL = TRUE)

Without, I get 0 0 0 0 under RStudio as expected

I think I have diagnosed the cause of this now, and have a possible workaround.

To set the initial size of its window or resize it, rgl sends a message to Windows asking it to do the resizing, then records the size that comes back in a WM_SIZE message. When run by clicking on knit, that message never comes back, so the window never changes from the initial 0,0,0,0 size.

The workaround is simply to set the initial size to 0,0,256,256. When I do that the graphic appears.

I don't think I know how to investigate why those messages aren't getting through. That needs some low-level debugging ability that I don't have.

Thanks, works for me. I leave this open because it would be better if the issued in the IDE were fixed.

cderv commented

Thanks everyone ! Glad it works now.

I agree with @dmenne that this isn't really fixed: figuring out why the messages aren't coming to rgl and fixing that issue would be a lot more comforting. But that's an RStudio issue, not an rmarkdown-cookbook issue.

cderv commented

But that's an RStudio issue, not an rmarkdown-cookbook issue.

Yes we should open an issue in the IDE, you're absolutely right. Would you like to open one ?
Otherwise I'll do it tomorrow.

Please go ahead.