R: using rgl to generate 3d rotatable plots that can be viewed in a web browser?

Setjmp picture Setjmp · Oct 5, 2011 · Viewed 11.7k times · Source

In the world of the R statistics package, rgl allows me to generate 3d plots that I can rotate with my mouse. Is there a way I can export these plots in a portable format, load them in a web browser or other third party tool and rotate them there? I Am especially interested in the web browser solution since this will allow me to share the plots on an internal wiki.

If rgl does not allow this, are there other libraries or strategies that would allow me to accomplish this?

Answer

pete picture pete · Oct 14, 2011

You could try the vrmlgen package. It will produce 3d VRML files that can be displayed with a browser plugin; you can find a plugin at VRML Plugin and Browser Detector.

Once you've installed a plugin, try this:

require(vrmlgen)
example(bar3d)

NB: the example code didn't automatically open in a browser for me (RStudio, Win7, Chrome) because the path got mangled. You might need to use:

require(stringr)
browseURL(str_replace_all(file.path(outdir, 'barplot.html'), fixed('\\'), '/'))

If you don't want to install a VRML plugin, you could use X3DOM instead. You'll need a converter, but your users should be able to view them with just a (modern) browser. You might have to modify the following code to get the paths right:

setwd(outdir)
aopt <- 'C:/PROGRA~1/INSTAN~1/bin/aopt' # Path to conversion program
vrml <- 'barplot.wrl'
x3dom <- 'barx.html'
command <- paste(aopt, '-i', vrml, '-N', x3dom)
system(command)
# LOG   Avalon   Init: 47/616, V2.0.0 build: R-21023 Jan 12 2011
# LOG   Avalon   Read url
# LOG   Avalon   Read time: 0.074000
# ============================================
# Call: writeHTML with 1 param 
# Write raw-data to barx.html as text/html
# WARNING   Avalon   Run NodeNameSpace "scene" destructor and _nodeCount == 3
# WARNING   Avalon   Try to remove nodes from parents
# WARNING   Avalon   PopupText without component, cannot unregister
# WARNING   Avalon   Avalon::exitSystem() call and node/obj left: 0/3331
browseURL(file.path(outdir, 'barx.html'))
setwd(curdir)