Here’s my alternative code, which works with all the D3 examples I’ve tried so far. Unlike Shankar’s approach with lower-level functions, I found it simpler to use Jeffrey Horner’s excellent Rook package.
# Load the Rook library library(Rook) # Where is your d3 directory located? myD3dir <- 'C:/Downloads' # Start the server s <- Rhttpd$new() s$start(quiet=TRUE) # To view a different D3 example, # change the directory and .html file names below # and rerun s$add() and s$browse() s$add( app=Builder$new( Static$new( # List all the subdirectories that contain # any files it will need to access (.js, .css, .html, etc) urls = c('/d3','/d3/examples','/d3/examples/choropleth'), root = myD3dir ), Redirect$new('/d3/examples/choropleth/choropleth.html') ), name='d3' ) s$browse(2) # browse(1) would load the default RookTest app instead # When you're done, # clean up by stopping and removing the server s$stop() s$remove(all=TRUE) rm(s)
If I understand the Rook documentation correctly, you just can’t browse directories using R’s local server. So you’ll have to type in the exact directory and HTML file for each example separately. But otherwise, this should be a simple way to play with D3 for anyone who’d rather stick within R instead of installing Python.