41

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

Community
  • 1
  • 1
Stéphane Laurent
  • 48,421
  • 14
  • 86
  • 170

1 Answers1

57

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))
```
Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
Yihui Xie
  • 22,808
  • 20
  • 167
  • 387
  • Thank you Yihui ! But I have tried the command `install.packages('knitr', repos = 'http://www.rforge.net/')` and I think this has not installed the latest version (`Error in knit_hooks$set(webgl = hook_webgl) : object 'hook_webgl' not found`) – Stéphane Laurent Feb 15 '13 at 09:24
  • 1
    @StéphaneLaurent Right, there is a one-day lag for building binaries; make sure you see the latest build here http://rforge.net/knitr/files/ Otherwise just try to install from source `install.packages('knitr', repos='http://www.rforge.net/', type='source')`, but RForge has problems with building my package under Windows, and you can also try `install.packages("knitr", repos="http://R-Forge.R-project.org")` – Yihui Xie Feb 15 '13 at 19:53
  • Thank you Yihui, that works well with `install.packages("knitr", repos="http://R-Forge.R-project.org")` – Stéphane Laurent Feb 16 '13 at 17:50
  • Hello Yihui. Please see the rendering here http://stla.overblog.com/including-an-interactive-3d-rgl-graphic-in-a-html-report-with-knitr1 (it works with Chrome only for me). It would be great if we could conveniently include multiple 3G graphics into a single hmtl report. – Stéphane Laurent Feb 16 '13 at 20:46
  • @StéphaneLaurent I just released knitr 1.1 to CRAN. For multiple plots in one page, I think it is possible in theory -- I did think about it when I was writing `hook_webgl()`, but I did not succeed because I was confused by the `prefix` argument in `rgl::writeWebGL()` which did not work as I expected. Anyway, feel free to file an issue here: https://github.com/yihui/knitr/issues I'll take anther look when I have got time. – Yihui Xie Feb 16 '13 at 22:51
  • Ok Yihui, but I firstly need to create an account on github. I will do it later. – Stéphane Laurent Feb 17 '13 at 20:02
  • FYI Devtools + windows should no longer need rtools installed unless the package has C source code – hadley Feb 18 '13 at 14:00
  • @hadley that is nice, but `knitr` still needs `make` in the age of R 2.x; after 3.0, it will be a pure R package, hence easy to install even under Windows – Yihui Xie Feb 18 '13 at 22:12
  • 1
    @StéphaneLaurent someone else has reported the issue for you https://github.com/yihui/knitr/issues/494 and it is fixed now – Yihui Xie Mar 07 '13 at 08:09
  • Thank you Yihui! I have just tried but the same problem occurs - I guess the new version for Windows is not available yet :) – Stéphane Laurent Mar 07 '13 at 19:23
  • @StéphaneLaurent yes, from time to time RForge fails to build the Windows version, but you can always install from source: `install.packages('knitr', repos='http://www.rforge.net/', type='source')` – Yihui Xie Mar 07 '13 at 21:05
  • Right, Yihui ! http://stla.overblog.com/including-an-interactive-3d-rgl-graphic-with-knitr Now it works with Firefox too. – Stéphane Laurent Mar 08 '13 at 18:27
  • Hello Yihui. Today I run the following code and the 3D plot does not appear in the html rendering : ```{r setup, echo=FALSE} library(rgl) knit_hooks$set(webgl = hook_webgl) ``` ```{r, webgl=TRUE} M – Stéphane Laurent Jun 24 '13 at 17:40
  • @StéphaneLaurent it does appear when I run your code; please update your original post with `library(knitr); library(rgl); sessionInfo()` – Yihui Xie Jun 24 '13 at 18:47
  • @StéphaneLaurent with the same version of knitr, I can see the 3D plot in Chrome, but I'm under Ubuntu and R 3.0.1. Please post the HTML source of the html file generated from RStudio(? or? you did not mention) on pastebin.com – Yihui Xie Jun 25 '13 at 00:07
  • Yes, from RStudio. The html code is here http://pastebin.com/HxGMhtME I have checked that this is not due to the Canvas.js file. – Stéphane Laurent Jun 25 '13 at 15:25
  • @Yihui I have knitted the Rmd source file of the article I wrote on my blog. The generated html file has some differences with the old one. This is surely due to an update of `rgl`. – Stéphane Laurent Jun 25 '13 at 17:15
  • @StéphaneLaurent I still cannot reproduce your problem. I used the same version of rgl and produced the same HTML file as yours. I can see the 3D plot in Google Chrome (Version 27.0.1453.93) under Ubuntu. If you see differences between the two versions of rgl, please post the results from `diff`. Or try a different web browser. It does not sound like a problem of rgl. – Yihui Xie Jun 25 '13 at 20:37
  • Yihui, I have just knitted the [preliminary version](http://pagist.github.io/?5861876) of my new article with my R.2.15.2 installation which contains an older version of `rgl` and this works very well. But it doesn't work with R.2.15.3 which contains the last version of `rgl`. I don't understand what is `diff` ? – Stéphane Laurent Jun 25 '13 at 21:31
  • Yihui, tomoroow I'll post two gists, one for each version. – Stéphane Laurent Jun 25 '13 at 22:07
  • `diff` is a *nix command to compare two files and calculate the difference; if you do not know that, just post the two HTML files and I can run `diff` – Yihui Xie Jun 26 '13 at 06:34
  • Yihui, I've asked to some people to try. It seems that this doesn't work for Windows users. Should I report this problem to the author of `rgl` ? – Stéphane Laurent Jun 27 '13 at 16:40
  • @StéphaneLaurent Yes, please report to Duncan. Both versions work for me under Ubuntu. – Yihui Xie Jun 27 '13 at 18:38
  • Yihui, the bug has been fixed by @havarc :-) – Stéphane Laurent Jul 05 '13 at 17:43
  • @StéphaneLaurent Nice! I'm glad to hear that. – Yihui Xie Jul 05 '13 at 19:25
  • And this, my friends, is the problem with link-based answers – David Jun 11 '19 at 16:20