1

I'm trying to build a flexdashboard which includes an rgl widget to display some multivariate data. The problem is identical to this post, where the widget I'm trying to create doesn't appear in the final document. Unfortunately the comments within the post didn't help me with my solution, (I re-installed the rgl package from the forge repo) and there weren't any answers posted. I've also looked at this post from Duncan himself, and wasn't able to implement a solution with what was said there either. I don't have the reprex package, nor have I used it before, and I'm a little pressed for time, so here's my best attempt at a reprex with the formatting I'm attempting to use.

---
title: "3d widget"
output: html_document
knitr::opts_chunk$set(echo = TRUE)

library(rgl)

with(mtcars, plot3d(x = mpg,
                    y = disp,
                    z = hp,
                    col = cyl,
                    size = 1,
                    type = "s",
                    axes = FALSE,
                    xlab = "",
                    ylab = "",
                    zlab = ""))
rglwidget()

When I knit the .rmd file, I don't see any errors, nothing obviously appears to go wrong during knit. And when I inspect the .html file I find the rglwidget element within the html code, but the space where it should be is still blank. The widget device will show up in the console if I run the code outside of markdown though, just not in the final document. This doesn't work with flexdashboard output either.

Here's my session info. I'm using 32-bit R because the data is coming from an access database and I need to keep architecture compatible:

R version 3.6.0 (2019-04-26)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

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

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

other attached packages:
[1] rgl_0.100.51

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.3              digest_0.6.20           later_0.8.0             mime_0.7               
 [5] R6_2.4.0                jsonlite_1.6            xtable_1.8-4            magrittr_1.5           
 [9] evaluate_0.14           rlang_0.4.4             miniUI_0.1.1.1          promises_1.0.1         
[13] rmarkdown_2.1           webshot_0.5.1           tools_3.6.0             manipulateWidget_0.10.0
[17] htmlwidgets_1.3         crosstalk_1.0.0         shiny_1.3.2             httpuv_1.5.1           
[21] xfun_0.7                yaml_2.2.0              compiler_3.6.0          htmltools_0.4.0        
[25] knitr_1.28
TylerH
  • 20,799
  • 66
  • 75
  • 101
aromatic6tet
  • 91
  • 1
  • 8
  • This sounds like a browser issue; your example works for me (once I add the missing bits: the dashes to end the YAML, the backtick markers around the code chunk). Are you using RStudio as the viewer? Which version? And why are you using 32 bit R in 64 bit Windows? – user2554330 Mar 20 '20 at 16:25
  • You may also be bitten by this issue: . – user2554330 Mar 20 '20 at 16:36
  • Even in my browser this doesn't appear: Chrome Version 79.0.3945.130 (Official Build) (64-bit). 32 bit R with 64 bit windows is because my data is coming from a 32 bit Access DB, which requires 32 ODBC driver. I've been making noise about migrating this to SQL server while the DB is still small enough to catch issues. The powers-that-be move slowly, however. – aromatic6tet Mar 20 '20 at 18:20
  • In Chrome, enter "chrome://gpu". See if WebGL is showing as disabled. Ideally it will be "Hardware accelerated". I'm not sure if software emulation is good enough. – user2554330 Mar 20 '20 at 19:48
  • WebGL and WebGL2 are both hardware accelerated. – aromatic6tet Mar 20 '20 at 21:47
  • I'd like to try the .html output in my own browser. Can you put it online somewhere? – user2554330 Mar 20 '20 at 23:43

2 Answers2

1

It appears that it's a problem with RStudio 1.2. As a workaround that worked for me, insert this line of code before loading rgl like RStudio community suggests:

options(rgl.useNULL=TRUE) 

A full working example:

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


```{r, setup}
options(rgl.useNULL=TRUE) 
library(rgl)


```{r testrgl}
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)


plot3d(x, y, z, col = rainbow(1000))
rglwidget(elementId = "plot3drgl")

This shouldn't be necessary, but it's a hack for now I reckon.

Rafs
  • 614
  • 8
  • 19
  • Hmm. My code, and your example are still not working within a flexdashboard. The work around I used before we went a different direction with the project was to use the magick library to put together a .gif file of the widget using spin() and movie() from rgl. Then I just linked that .gif into the dashboard. Thank you for the input! – aromatic6tet Oct 01 '20 at 15:43
  • My pleasure. I have to admit I didn't test within `flexdashboard`... – Rafs Oct 01 '20 at 15:49
0

Solved. It was there the whole time. The renderer that comes with Rstudio wasn't rendering the widget. As soon as I opened my window in a web browser that supports webGL, then bang! There it was. Not sure if this something that the Rstudio team needs to know about, or if webGL is even something that they want to incorporate within the IDE.

Anyway, cheers everyone.

aromatic6tet
  • 91
  • 1
  • 8