rstudio / rmarkdown-cookbook

R Markdown Cookbook. A range of tips and tricks to make better use of R Markdown.
https://bookdown.org/yihui/rmarkdown-cookbook/
578 stars 224 forks source link

rgl example does not display #370

Closed dmenne closed 2 years ago

dmenne commented 2 years ago

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 2 years ago

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

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

cderv commented 2 years ago

I think this is related to this : https://github.com/rstudio/rstudio/issues/5678

cderv commented 2 years ago

@dmurdoch if I may ping you here:

Are you aware of such behavior ? Is this related to https://github.com/rstudio/rstudio/issues/5678 according to you ?

Thank a lot!

dmenne commented 2 years ago
dmurdoch commented 2 years ago

@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.

dmenne commented 2 years ago

Hi, Duncan,

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

No change.

dmurdoch commented 2 years ago

This is the code that makes the decision on how to plot:

https://github.com/dmurdoch/rgl/blob/b888441b844ed9b116dd1ad8be5359def2ea6ff0/R/knitr.R#L47-L50

dmurdoch commented 2 years ago

@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?

dmenne commented 2 years ago

distill.zip

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

dmenne commented 2 years ago

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

Take the code from the book.

dmenne commented 2 years ago

Removed the hook: no change

dmurdoch commented 2 years ago

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:

x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
plot3d(x, y, z, col = rainbow(1000))
dmurdoch commented 2 years ago

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.

dmenne commented 2 years ago

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 2 years ago

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.

dmurdoch commented 2 years ago

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

I'd guess the issue is related to https://github.com/rstudio/rstudio/issues/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 2 years ago

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

dmurdoch commented 2 years ago

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

dmenne commented 2 years ago

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

dmurdoch commented 2 years ago

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.

dmenne commented 2 years ago

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?

dmenne commented 2 years ago

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 2 years ago

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

dmurdoch commented 2 years ago

@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.

dmurdoch commented 2 years ago

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.

dmenne commented 2 years ago

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

dmenne commented 2 years ago

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 
dmurdoch commented 2 years ago

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 2 years ago

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)

dmenne commented 2 years ago

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

dmenne commented 2 years ago

Sorry, correction. The above was in the version with

options(rgl.useNULL = TRUE)

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

dmurdoch commented 2 years ago

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.

dmenne commented 2 years ago

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

cderv commented 2 years ago

Thanks everyone ! Glad it works now.

dmurdoch commented 2 years ago

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 2 years ago

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.

dmenne commented 2 years ago

Please go ahead.