How to install R on a Jupyter notebook

Data Journalism in R, How to
Share on FacebookShare on Google+Tweet about this on TwitterPin on PinterestShare on LinkedInEmail this to someone

This post walks through how to install R on a Jupyter notebook, load in data and build a visualization. Start by installing python using homebrew.

Install python

Open Terminal and enter the following commands.

$ brew install python

It should already be installed, in which case you’ll need to upgrade

$ brew upgrade python

Install jupyter

Next install the jupyter notebook

$ brew install jupyter

Install zmq

$ brew install zmq

Install the following R packages

These packages can be installed using R or RStudio. They are also found in the jupyter-install-pkgs.R file.

                 repos = c('', 
                 type = 'source')
IRkernel::installspec(user = FALSE)

Launch jupyter notebooks

Open a new terminal window, enter the following commands.

$ jupyter notebook

This will launch the jupter notebook. You’ll see this in your Terminal:

The jupyter notebook will open in the browser

Click on the New icon in the upper right corner of the Jupyter notebook and select R.

Click on the Untitled text next to the Jupyter on the top of the notebook and rename the file something meaningful.

Using jupyter notebooks

The Jupyter notebooks combine code cells with markdown (just like RStudio Notebooks). I’ll start this notebook with a markdown title cell.

As you can see, this cell has markdown syntax highlighting. If I want to see how the cell will look after running it, hold down control + return (this is how to run a cell in Jupyter notebooks).

Run some R code

Enter the following code in the next cell and run it (either by using control + return or clicking Run)

# run this in jupyter notebook --------------------------------------------
# install packages

Let’s print a portion of a data frame to see what it looks like in Jupyter notebooks.

# get data set
cabrera <- tigerstats::cabrera

Tables look pretty in Jupyter!

Run ggplot in Jupyter

I’ll see how a ggplot2 visualization looks next. Enter the following commands into the next code cell (I turned off the warnings before and after the plot commands to get rid of all the excess output).

# turn off warnings for chart ----- 
# plot spray chart
cabrera %>% 
  ggplot2::ggplot(aes(x = hitx, y = hity)) + 
  ggplot2::geom_point(aes(color = hit_outcome)) + 
  ggplot2::coord_equal() + 
  ggplot2::facet_wrap(~ season) +
    title = "Spray Chart of Miguel Cabrera’s balls hit into play by season (2009-2012)",
    subtitle = "from tigerstats package")
# turn warnings back on -----

The graph output is directly beneath the cell! Beautiful!

Download options

Under File > Download as I can see I have the option to save this file as a .ipynb (which is a JSON file), .md.r, and many others.

Happy coding!

Martin Frigaard is a graduate student at UCSF. Find him on Twitter.

Leave a Reply