title: including a interactive 3D figure with knitr
body: <p>Using <code>knitr</code> it is possible to embed a <code>rgl</code> 3D graphics in a html document from a <code>Rmarkdown</code> source file:</p>
  
  <pre><code>```{r setup}
  library(rgl)
  knit_hooks$set(rgl = hook_rgl)
  x &lt;- sort(rnorm(1000))
  y &lt;- rnorm(1000)
  z &lt;- rnorm(1000) + atan2(x,y)
  ```
  
  ```{r, rgl=TRUE}
  plot3d(x, y, z, col=rainbow(1000))
  ```
  </code></pre>
  
  <p>But the 3D graphic is not interactive in the html document. Is it possible to get an interactive 3D graphic ? The <code>writeWebGL()</code> function of the <code>rgl</code> 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 ?</p>
  
  <h2>Update 24/06/2013</h2>
  
  <p>Here is an example that does not work today (the 3D graphic does not appear in Chrome):</p>
  
  <ul>
  <li><p>the <a href="https://gist.github.com/stla/5870638" rel="noreferrer">Rmd source file</a>, which is very basic:</p>
  
  <pre><code>```{r setup}
  library(rgl)
  knit_hooks$set(webgl = hook_webgl)
  ```
  ```{r, webgl=TRUE}
  M &lt;- rbind(
    c(0,0,0),
    c(-1,4,0),
    c(4,9,0),
    c(6,3,0)
    )
    points3d(M,col='red')
  ```
  ```{r}
  sessionInfo()
  ```
  </code></pre>
  
  <p>I have knitted this file with the RStudio "knit" button in two situations using different versions of <code>rgl</code> and <code>knitr</code> packages (but this is surely due to the <code>rgl</code> package because the problem occurs with the output of the <code>writeWebGL</code> function) :</p></li>
  <li><p>old versions with R-2.15.2 : <a href="https://gist.github.com/stla/5870681" rel="noreferrer">source file</a> and <a href="http://pagist.github.io/?5870681" rel="noreferrer">html rendering</a>. And the <a href="https://gist.github.com/stla/5871039" rel="noreferrer">html file generated by <code>writeWebGL</code> with rgl_0.93.928</a>. 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). </p></li>
  <li><p>latest versions with R-2.15.3 : <a href="https://gist.github.com/stla/5870653" rel="noreferrer">source file</a> and <a href="http://pagist.github.io/?5870653" rel="noreferrer">html rendering</a>. And the <a href="https://gist.github.com/stla/5871051" rel="noreferrer">html file generated by <code>writeWebGL</code> with rgl_0.93.935</a>. 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.</p></li>
  </ul>
  
  <h2>Edit 28/06/2013</h2>
  
  <p>The problem raised by the 24/06 update has nothing to do with <code>knitr</code>. I have rephrased it in this post: <a href="https://stackoverflow.com/q/17365709/1100107">WebGL rendering with rgl 0.93.935 R package</a></p>