I am plotting a 3d graph using plot3d
{rgl} but it is displayed on an external window frame, and for that reason, it is not captured in the markdown html/pdf file. Is there a way that I can plot the graph in the inner plots window of RStudio so that it can be captured when I compile the notebook?
Asked
Active
Viewed 790 times
1

Ben Bolker
- 211,554
- 25
- 370
- 453

Annie
- 681
- 7
- 14
1 Answers
2
No. rgl
uses a completely different plotting system. Yihui Xie's knitr hook documentation explains how to use the rgl.snapshot()
function to include rgl
output in a knitr
document:
knit_hooks$set(rgl = function(before, options, envir) {
if (!before) {
## after a chunk has been evaluated
if (rgl.cur() == 0) return() # no active device
name = paste(options$fig.path, options$label, sep = '')
rgl.snapshot(paste(name, '.png', sep = ''), fmt = 'png')
return(paste('\\includegraphics{', name, '}\n', sep = ''))
}
})
(then use rgl=TRUE
in your chunk options). You can also include the output as an interactive webgl element in an HTML document. If you use the knitr spin
option (see here), you should be able to include a line like
#+ my_rgl_plot, rgl=TRUE
before your plot to set the chunk options.

Community
- 1
- 1

Ben Bolker
- 211,554
- 25
- 370
- 453
-
Thanks much for your reply. I tried but in a loop where multiple plots are generated, it doesn't work. – Annie Jul 29 '16 at 17:11
-
Can you give a reproducible example? You might need to construct your own code to run `rgl.snapshot` each time and then incorporate the results in the output (i.e., this might be a little too complicated for notebook format, explicitly writing a `.rmd` file might be easier than working within the limitations of the notebook) – Ben Bolker Jul 29 '16 at 18:56
-
thanks much for your help, I found a solution http://stackoverflow.com/questions/38664432/how-to-inlcude-multiple-plot3d-in-markdown-knitr-under-a-loop – Annie Aug 02 '16 at 09:42