Hadley Wickham, creator of the
ggplot2 (tentatively titled
r2d3?)… so I suspect it’s only a matter of time before he pulls the rest of the R community along.
A few initial comments:
something$contentsin R are equivalent to
something['contents'](in both languages).
Now, if you want to create anything yourself with D3, it’s useful to start by editing the examples and seeing how your changes affect the results. Let’s try running the example I started with, trying to read and understand and tweak a D3 choropleth map.
If you don’t have it yet, download D3 from its github site. (Near the top, there’s a ZIP button to download everything in one zip file.) Unzip the files into a handy directory and let’s first see whether the examples work for you. Go into the
.../examples/choropleth/ folder and open up
choropleth.html in your web browser. Chances are, it won’t load the nice map, because as the D3 wiki says:
When running the examples locally, note that your browser may enforce strict permissions for reading files out of the local file system. Some examples use AJAX which works differently via HTTP instead of local files. To view the examples locally, you must have a local web server. Any web server will work; for example you can run Python’s built-in server:
python -m SimpleHTTPServer 8888 &
- Go to the Start menu, then to Run, and type “cmd” to open a command line window.
- At the prompt, type in
so Windows knows where to search for Python. (Adjust as needed if you have Python elsewhere or another version; for example, if you have version 2.5 instead of 2.7, you’d type
- Navigate to wherever you unzipped and saved the D3 files, for example
- Start a local server from this directory by typing in
python -m SimpleHTTPServer 8888
- Open your browser and go to http://localhost:8888/
- Finally! You can see the files in the D3 directory. Click through to the
.../examples/choropleth/subdirectory, open up
choropleth.html, and it ought to work.
(If it doesn’t work… leave a comment and we’ll try some other approaches!)
Once you get it running, try opening the HTML file in a text editor such as Notepad. (You know some basic HTML, right? If not, head over to w3schools.com (edit: or better yet, Mozilla Developer Network) for a quick overview.) Okay. In the HTML file, see how the head section calls a script with
? That says to load up the general D3 library — it’s just like using
library(ggplot2) or whatever at the start of your R scripts. You’ll need to do that whenever you create a new tool that uses D3 functionality.
Further down, in the HTML body, it calls another script with
and this script is no longer a general library but specific code for the choropleth example. Open up that
Some other resources to tide you over:
- Luke Francl’s D3 for Mere Mortals seems to be a useful introduction for D3 newbies, including some basics of the underlying technology of SVG. There are more good D3 tutorials linked at the bottom.
See also part 2 for how to run the local server in R instead of using Python.