Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

including a interactive 3D figure with knitr

Using knitr it is possible to embed a rgl 3D graphics in a html document from a Rmarkdown source file:

```{r setup}
library(rgl)
knit_hooks$set(rgl = hook_rgl)
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
```

```{r, rgl=TRUE}
plot3d(x, y, z, col=rainbow(1000))
```

But the 3D graphic is not interactive in the html document. Is it possible to get an interactive 3D graphic ? The writeWebGL() function of the rgl package creates a html file with an interactive 3D graphic, is there a way to include directly this html code with Rmarkdown ? Otherwise how to include this html code manually ?

Update 24/06/2013

Here is an example that does not work today (the 3D graphic does not appear in Chrome):

  • the Rmd source file, which is very basic:

    ```{r setup}
    library(rgl)
    knit_hooks$set(webgl = hook_webgl)
    ```
    ```{r, webgl=TRUE}
    M <- rbind(
      c(0,0,0),
      c(-1,4,0),
      c(4,9,0),
      c(6,3,0)
      )
      points3d(M,col='red')
    ```
    ```{r}
    sessionInfo()
    ```
    

    I have knitted this file with the RStudio "knit" button in two situations using different versions of rgl and knitr packages (but this is surely due to the rgl package because the problem occurs with the output of the writeWebGL function) :

  • old versions with R-2.15.2 : source file and html rendering. And the html file generated by writeWebGL with rgl_0.93.928. For me it works well (there are just 4 red points in the 3D plot... not easy to see on my dirty screen but I see them).

  • latest versions with R-2.15.3 : source file and html rendering. And the html file generated by writeWebGL with rgl_0.93.935. For me it doesn't work: the 3D plot is not visible. I use Windows 7 and it doesn't work with Chrome, neither with Firefox.

Edit 28/06/2013

The problem raised by the 24/06 update has nothing to do with knitr. I have rephrased it in this post: WebGL rendering with rgl 0.93.935 R package

like image 960
Stéphane Laurent Avatar asked Feb 14 '13 16:02

Stéphane Laurent


2 Answers

I added a new hook hook_webgl() in knitr, which was incorporated into rgl later. Here is an example:

```{r setup}
library(knitr)
library(rgl)
knit_hooks$set(webgl = hook_webgl)
```

```{r testgl, webgl=TRUE}
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
plot3d(x, y, z, col=rainbow(1000))
```
like image 182
Yihui Xie Avatar answered Nov 12 '22 14:11

Yihui Xie


Things have changed since this question and answer were written in 2013. Nowadays I wouldn't recommend using rgl=TRUE or webgl=TRUE. Instead, use the following approaches:

The easiest approach is to have a setup code chunk containing

   ```{r include=FALSE}
   options(rgl.useNULL = TRUE)
   rgl::setupKnitr(autoprint = TRUE)
   ```

With this setup, plots will appear automatically. If you run several commands to produce a plot, only the final version will appear. (Some functions in other packages don't cooperate with this scheme and nothing will appear; for those, you can trigger it by making a call to rgl::lowlevel() in the chunk once the graphic is complete.)

If you don't want the automatic inclusion, then use

   ```{r include=FALSE}
   options(rgl.useNULL = TRUE)
   rgl::setupKnitr()
   ```

With this option you make an explicit call to rglwidget() whenever you want a graphic to display.

There are several knitr chunk options supported: rgl.newwindow, rgl.closewindow, rgl.margin, snapshot, dpi, fig.retina, fig.width, fig.height, fig.keep, fig.hold, fig.beforecode, dev; they are described in the ?rgl::setupKnitr help topic.

like image 1
user2554330 Avatar answered Nov 12 '22 14:11

user2554330