3

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

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • I may be wrong, but I was under the impression that PDF files can only render static content. – nrussell Nov 05 '16 at 16:52
  • I hope this is not true. You can include animations. The hooks example uses this very example: http://yihui.name/knitr/hooks/ This also indicated interactive rgl could happen but this doesn't render as dynamic either: https://github.com/yihui/knitr-examples/blob/master/092-latex-rgl.Rnw – Tyler Rinker Nov 05 '16 at 16:56
  • This indicates it's just a single image: `if we want to insert 3D snapshots produced in rgl` from http://yihui.name/knitr/hooks/ – Tyler Rinker Nov 05 '16 at 17:12
  • 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](http://r.789695.n4.nabble.com/Creating-interactive-3D-graphs-for-embedding-in-pdf-not-possible-in-R-td4634174.html) is somewhat relevant to your question. – nrussell Nov 05 '16 at 17:30
  • 2
    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 Nov 05 '16 at 18:35
  • @Whomever downvoted you must be either a troll, ignorant of the help center guide (I'll post here for your reading convenience: http://stackoverflow.com/help), or you simply made an error. If you really feel the question is that poorly formatted or out of scope please post an explanatory comment so that I may improve the question. – Tyler Rinker Nov 05 '16 at 22:59

1 Answers1

3

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.

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • Thanks. I agree on the pdf. It's for a dissertation where I needed extreme control over formatting and much of the cool rmarkdown stuff we have now was new or missing when I started it 2 years ago. – Tyler Rinker Nov 05 '16 at 22:55
  • 1
    @TylerRinker You can insert a static screenshot in the PDF, and link to the interactive HTML version that you deploy somewhere like Github pages or Dropbox. – Yihui Xie Nov 12 '16 at 03:37
  • It would be very helpful if someone included an example of this as another answer. – user101089 Nov 14 '16 at 03:25