6

I am trying to get two 3D scatter plots, drawn with the rgl package, side-by-side in an R Markdown document. For example:

mfrow3d(nr = 1, nc = 2, sharedMouse = TRUE)  
plot3d(mtcars[, 1:3], type = "s")  
plot3d(mtcars[, 4:6], type = "s")
rglwidget()

This works perfectly when run directly in the console but when knit to an HTML document using R Markdown, it seems as if only the second plot is rendered.

I tried adding knit_hooks$set(webgl = hook_webgl) and setting the chunk option webgl = TRUE, but that did not help either.

Any suggestions would be appreciated.

This is my output from sessionInfo():

R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] rgl_0.96.0           tidyr_0.6.0          dplyr_0.5.0          smacof_1.8-13       
 [5] emojifont_0.3.3      gridExtra_2.2.1      knitr_1.14           MASS_7.3-45         
 [9] shinydashboard_0.5.1 shiny_0.13.2         RColorBrewer_1.1-2   ggplot2_2.1.0       

loaded via a namespace (and not attached):
 [1] gtools_3.5.0        splines_3.3.1       lattice_0.20-33     colorspace_1.2-6   
 [5] htmltools_0.3.5     yaml_2.1.13         base64enc_0.1-3     chron_2.3-47       
 [9] survival_2.39-4     DBI_0.5             foreign_0.8-66      plyr_1.8.4         
[13] stringr_1.1.0       munsell_0.4.3       gtable_0.2.0        htmlwidgets_0.7    
[17] evaluate_0.9        latticeExtra_0.6-28 httpuv_1.3.3        proto_0.3-10       
[21] Rcpp_0.12.6         acepack_1.3-3.3     xtable_1.8-2        polynom_1.3-8      
[25] scales_0.4.0        formatR_1.4         showtext_0.4-4      gdata_2.17.0       
[29] jsonlite_1.0        Hmisc_3.17-4        sysfonts_0.5        mime_0.5           
[33] weights_0.85        digest_0.6.10       stringi_1.1.1       showtextdb_1.0     
[37] grid_3.3.1          tools_3.3.1         magrittr_1.5        tibble_1.2         
[41] Formula_1.2-1       mice_2.25           cluster_2.0.4       Matrix_1.2-6       
[45] rsconnect_0.4.3     data.table_1.9.6    nnls_1.4            assertthat_0.1     
[49] rmarkdown_1.0.9013  R6_2.1.3            rpart_4.1-10        nnet_7.3-12
Mike Wise
  • 22,131
  • 8
  • 81
  • 104
Jandre Marais
  • 318
  • 2
  • 9
  • There's a typo in your first line - it should be: mfrow3d(nr = 1, nc = 2, sharedMouse = TRUE) –  Sep 01 '16 at 22:38
  • I am facing a similar problem when trying to plot few charts in a row and only the last one is visible on the HTML (although not side-by-side). – RgrNormand Sep 04 '16 at 13:30

2 Answers2

10

This was a bug in the conversion of the bounding box decoration to Javascript code. The bounds from the first plot carried over to the second one. Since they were calculated from unrelated quantities, you got weird boxes resulting. The bug is now fixed in version 0.96.1516, currently only on R-forge.

By the way, with current rgl you don't need hook_webgl or library(rglwidgets). Just use library(rgl) and call rglwidget() if you want to insert a plot. (There's a way to automatically insert plots without the rglwidget() call, but I don't recommend it.)

So your document could look like this:

```{r echo=TRUE}
library(rgl)
mfrow3d(nr = 1, nc = 2, sharedMouse = TRUE)  
plot3d(mtcars[, 1:3], type = "s",)  
plot3d(mtcars[, 4:6], type = "s")
rglwidget()
```

This produces the picture below in rgl 0.96.1516:

enter image description here

user2554330
  • 37,248
  • 4
  • 43
  • 90
2

There seems to be at least one, and more like two bugs in play here. Modifying your code a bit, this:

```{r echo=T}
library(rgl)
library(rglwidget)
library(knitr)
knit_hooks$set(webgl = hook_webgl)

mfrow3d(nr = 2, nc = 2, sharedMouse = T)  
plot3d(mtcars[, 1:3], type = "s",)  
plot3d(mtcars[, 4:6], type = "s")
plot3d(mtcars[, 1:3], type = "s")  
plot3d(mtcars[, 4:6], type = "s")
rglwidget()
```

produces this in a briefly visible device window:

enter image description here

but this in the R-Studio preview window:

enter image description here

And this in the html file loaded into the Chrome browser:

enter image description here

So only the first one works, and markdown seems to break it.

I would say you should probably just avoid composing multiple rgl plots in markdown until this (these) bugs get fixed. Might be hard as it seems to be some weird interaction between the various graphics devices and the library.

Update

Looks like D (?) fixed the issue per his answer, but there seems to be another issue in play too. Posting this as information.

enter image description here

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • 1
    Looks like a bug in the bbox code. If you draw those plots with `axes=FALSE` and follow with manual axes using box3d() and axis3d() they're basically okay. – user2554330 Sep 04 '16 at 19:46
  • Weird. I'm on a Mac, but most of the package versions look the same. Can you make the .html file available somewhere for me to look at? – user2554330 Sep 05 '16 at 13:05
  • I can't reproduce this. Is anyone else seeing it? – user2554330 Sep 06 '16 at 17:58
  • 2
    Had a look, the HTML actually works now and displays everything, it is only the R-Studio preview that can only display one rgl plot. – Mike Wise Sep 06 '16 at 19:16
  • What RStudio version are you using? If you feel really ambitious, you might be able to debug it as follows: right click in the viewer pane, and choose "Inspect element". Choose "Console" from one of the tabs at the top, and you can see all the error messages. See if you can figure out which ones were caused by rgl, if any. – user2554330 Sep 06 '16 at 20:52
  • I could't get at the json in the viewer pain. You can't scoll it or copy it with cut and paste. Maybe if I tried with a smaller example. – Mike Wise Sep 13 '16 at 10:18
  • The json is just data, it's not executable. You could get it by cut and paste from the .html file. – user2554330 Sep 14 '16 at 12:13
  • Well, if it is the same as it is in the html, then there is nothing to debug right? Since the html works fine in Chrome, then the viewer must be intepreting the json incorrectly. I think it might be different, but I can't get at it to see if that is true. – Mike Wise Sep 14 '16 at 14:40
  • There's also a big Javascript library that does all the work of rendering. When I look at something in RStudio, it displays as ``. I'm not sure how to see what's in it in that environment. – user2554330 Sep 14 '16 at 17:47