Javascript and D3 for R users, part 2: running off the R server instead of Python

Thank you all for the positive responses to Basics of JavaScript and D3 for R Users! Quick update: last time we had to dabble in a tiny bit of Python to start a local server, in order to actually run JavaScript and D3 examples on our home computer… However, commenter Shankar had the great idea of using the R server instead. He provided some example code, but reported that it didn’t work with all the examples.

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.

2 Responses to Javascript and D3 for R users, part 2: running off the R server instead of Python

  1. Hi I am not able to get this working; i am following all the steps correctly. i have d3 downloaded in c:\downloads

    i am getting http://127.0.0.1:28056/custom/d3/d3/examples/choropleth/choropleth.html

    and File not found: /d3/examples/choropleth/choropleth.html as the response

    it somewhere adds an extra / (pl note /d3/d3).

    also when i do getwd() in R console, it gives me

    > getwd()
    [1] “C:/Users/194460/Documents”
    Warning message:
    In normalizePath(path.expand(path), winslash, mustWork) :
    path[1]=”C:/Downloads//d3/examples/choropleth/choropleth.html”: The system cannot find the path specified

    pl note the extra / before the d3. pl help in getting it working for me.

    thanks in advance.

  2. Hi Chandra, sorry for the slow response.
    The extra / shouldn’t be what’s causing the problem.

    Where exactly is your choropleth.html file? Is it in
    C:/Downloads/d3/examples/choropleth/
    or in
    C:/Downloads/d3/d3/examples/choropleth/
    ?
    If the correct path contains .../d3/d3/... then you’ll need to edit my code above so that it also has /d3 twice. Either add another /d3 at the end when you define myD3dir, or add it at the beginning of every string when you call s$add.
    Let me know if that works!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>