Tyler Rinker - 4 months ago 61
R Question

# Include rgl interactive plot in .Rnw (pdf output)

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

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.
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.