3
votes

Description of Problem

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.

Question(s)

  1. Is including the interactive rgl in a pdf possible?
  2. If so...How can I do this with knitr?; what am I doing incorrectly?

MWE

\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}

What I see in pdf:

enter image description here

1
I may be wrong, but I was under the impression that PDF files can only render static content. - nrussell
I hope this is not true. You can include animations. The hooks example uses this very example: yihui.name/knitr/hooks This also indicated interactive rgl could happen but this doesn't render as dynamic either: github.com/yihui/knitr-examples/blob/master/092-latex-rgl.Rnw - Tyler Rinker
This indicates it's just a single image: if we want to insert 3D snapshots produced in rgl from yihui.name/knitr/hooks - Tyler Rinker
I'm not sure how the linked knitr examples are supposed to work, but when I tried them they did not generate an interactive plot in the PDF document. I'm pretty sure this is just a limitation of the PDF format in general, not necessarily specific to knitr or rgl. This thread is somewhat relevant to your question. - nrussell
Only Adobe PDF Reader supports animations. The animation was created by the LaTeX package animate. I remember there are LaTeX packages that support interactive 3D plots, but I don't know if rgl can be connected with these packages. - Yihui Xie

1 Answers

3
votes

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.