Stéphane Laurent
Stéphane Laurent

Reputation: 84529

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):

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

Upvotes: 44

Views: 21100

Answers (2)

user2554330
user2554330

Reputation: 44867

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.

Upvotes: 3

Yihui Xie
Yihui Xie

Reputation: 30114

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))
```

Upvotes: 58

Related Questions