including a interactive 3D figure with knitr

18,231

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))
```
Share:
18,231
Stéphane Laurent
Author by

Stéphane Laurent

Updated on July 23, 2022

Comments

  • Stéphane Laurent
    Stéphane Laurent almost 2 years

    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

  • Stéphane Laurent
    Stéphane Laurent about 11 years
    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)
  • Yihui Xie
    Yihui Xie about 11 years
    @StéphaneLaurent Right, there is a one-day lag for building binaries; make sure you see the latest build here 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")
  • Stéphane Laurent
    Stéphane Laurent about 11 years
    Thank you Yihui, that works well with install.packages("knitr", repos="http://R-Forge.R-project.org")
  • Stéphane Laurent
    Stéphane Laurent about 11 years
    Hello Yihui. Please see the rendering here stla.overblog.com/… (it works with Chrome only for me). It would be great if we could conveniently include multiple 3G graphics into a single hmtl report.
  • Yihui Xie
    Yihui Xie about 11 years
    @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: github.com/yihui/knitr/issues I'll take anther look when I have got time.
  • Stéphane Laurent
    Stéphane Laurent about 11 years
    Ok Yihui, but I firstly need to create an account on github. I will do it later.
  • hadley
    hadley about 11 years
    FYI Devtools + windows should no longer need rtools installed unless the package has C source code
  • Yihui Xie
    Yihui Xie about 11 years
    @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
    Yihui Xie about 11 years
    @StéphaneLaurent someone else has reported the issue for you github.com/yihui/knitr/issues/494 and it is fixed now
  • Stéphane Laurent
    Stéphane Laurent about 11 years
    Thank you Yihui! I have just tried but the same problem occurs - I guess the new version for Windows is not available yet :)
  • Yihui Xie
    Yihui Xie about 11 years
    @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')
  • Stéphane Laurent
    Stéphane Laurent about 11 years
    Right, Yihui ! stla.overblog.com/… Now it works with Firefox too.
  • Stéphane Laurent
    Stéphane Laurent almost 11 years
    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 <- rbind( c(0,0,0), c(-1,4,0), c(4,9,0), c(6,3,0) ) points3d(M,col='red')
  • Yihui Xie
    Yihui Xie almost 11 years
    @StéphaneLaurent it does appear when I run your code; please update your original post with library(knitr); library(rgl); sessionInfo()
  • Yihui Xie
    Yihui Xie almost 11 years
    @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
  • Stéphane Laurent
    Stéphane Laurent almost 11 years
    Yes, from RStudio. The html code is here pastebin.com/HxGMhtME I have checked that this is not due to the Canvas.js file.
  • Stéphane Laurent
    Stéphane Laurent almost 11 years
    @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.
  • Yihui Xie
    Yihui Xie almost 11 years
    @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.
  • Stéphane Laurent
    Stéphane Laurent almost 11 years
    Yihui, I have just knitted the preliminary version 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
    Stéphane Laurent almost 11 years
    Yihui, tomoroow I'll post two gists, one for each version.
  • Yihui Xie
    Yihui Xie almost 11 years
    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
  • Stéphane Laurent
    Stéphane Laurent almost 11 years
    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 ?
  • Yihui Xie
    Yihui Xie almost 11 years
    @StéphaneLaurent Yes, please report to Duncan. Both versions work for me under Ubuntu.
  • Stéphane Laurent
    Stéphane Laurent almost 11 years
    Yihui, the bug has been fixed by @havarc :-)
  • Yihui Xie
    Yihui Xie almost 11 years
    @StéphaneLaurent Nice! I'm glad to hear that.
  • David
    David almost 5 years
    And this, my friends, is the problem with link-based answers