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

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


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 programvrml <- '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/3331browseURL(file.path(outdir, 'barx.html'))setwd(curdir)


For a simple solution try this...

x <- sort(rnorm(1000))y <- rnorm(1000)z <- rnorm(1000) + atan2(x,y)plot3d(x,y,z,        col=rainbow(1000),       type = "s",       size=1,       xlab = "x",        ylab = "y",        zlab = "z",       box=T)# This writes a copy into temporary directory 'webGL', and then displays itbrowseURL(paste("file://", writeWebGL(dir=file.path("C:/Your-Directory-Here/", "webGL"), width=700), sep=""))

open the index.html file in Firefox or similar browser that supports HTML5 and WebGL


Pete's suggestion is worth the bounty. The wrl-detour is not really necessary, it is rather easy to generate the xml-file with sprintf and friends.

The problem is speed: As a comparison, I had a color-code stomach MRI with 17000 spheres (for voxels), which was quite responsive on my screen with rgl.

When I ported it to x3dom, the system froze. A reduced set with 450 spheres works:

http://www.menne-biomed.de/uni/x3dsample.html

Browser support is inconsistent. Some of the samples on the x3dom example page work best with (believe it or not) Internet Explorer + Flash 11. Check the dynamic light example.

My example works, but looks flat on Firefox 7.0.1. Best is always Chrome.

Added later:Here is another example:

Stomach3D as Zip

The x3d file contained in it can be displayed even with on-board graphics using the Instant Reality Viewer. The html file generated from it sometimes loads, but cannot be rotated.