I'd like to include an interactive (rotating) rgl 3-d scatterplot in a .pdf knitted from a .Rnw file. I know there is a hook for including rgl
but maybe this is for html output only. I can't seem to include the plot so that it rotates. Here is a minimal example. The plot appears but there are lines rather than points and no rotation available.
\documentclass{article}
\begin{document}
<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
library(rgl)
knit_hooks$set(rgl = hook_rgl)
@
<<fancy-rgl, rgl=TRUE>>=
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
# open3d()
plot3d(x, y, z, col = 'black')
@
\end{document}
It's possible, but just barely. You need to install Asymptote, and use rgl::writeASY()
to write a program for it. Then include that program in your document, run LaTeX, then Asymptote, then LaTeX again.
There are examples of including Asymptote in LaTeX here: http://asymptote.sourceforge.net/doc/LaTeX-usage.html#LaTeX-usage.
The results are kind of disappointing.
My advice would be to abandon PDF. HTML5 is a much better target for output; rgl
does quite a good job (using rglwidget()
) producing output for web pages. It's really unlikely that any effort will be put into improving PDF output, whereas fixing the remaining gaps in HTML support are a priority.