Annie
Annie

Reputation: 683

How to plot3d in the inner plots window of RStudio

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?

Upvotes: 1

Views: 869

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226427

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.

Upvotes: 2

Related Questions