Project Jupyter talk for R North-East group

January 2016

Jupyter lets you create interactive documents containing text and code + output

Many languages are supported, including Julia, Python and R

Good tool for (a) documenting an analysis in one file (b) interactive R use

The main interface is the "Jupyter notebook" - a web app that runs in the browser

Project background

Jupyter is an open source project written in Python

It's a spin-off of "IPython" - similar project for notebooks containing Python code only

R support is quite new so not a lot of documentation yet!

Notebook file format

Notebooks are stored as JSON objects

These contain full details of current session

So you can immediately view someone's analysis by opening a file

And then modify and rereun parts if you choose

Notebook capabilities

Below I list some things that a Jupyter notebook can display

Input can be plain text

Or markdown, allowing for easy formatting features

e.g.

  1. italics, bold
  2. Equations (using latex):
    $\int_{-\infty}^\infty e^{-\theta^2} \sin \theta\ d\theta$
  3. Lists!
  4. Hyperlinks
  5. Raw HTML:

Explanatory text can be mixed with code.

Let's estimate $\int_0^1 x^3 dx$ by Monte Carlo integration (correct answer is 0.25)

i.e. I'll simulate uniform random points in the unit square and find the proportion below the graph of $x^3$

In [1]:
n = 1000; x = runif(n, 0, 1); y = runif(n, 0, 1)
mean(y < x^3)
Out[1]:
0.247
In [3]:
xx = seq(0,1,length.out=100)
plot(x,y, col=1 + (y<x^3), pch=16) + lines(xx, xx^3, col="blue", lwd=3)
Out[3]:

Interface

Getting started

The first screen you see is the dashboard

Here you can load a notebook from the file system

Or create a new notebook: File -> New Notebook -> R

More details are in the Jupyter basics documentation

Features

  • Code highlighting
  • Tab completion
  • Keyboard shortcuts
  • Cells with large code output can be hidden

Sharing notebooks

Exporting

The File -> Download as menu has lots of export options including:

  • PDF
  • HTML
  • Markdown
  • R code (just exports code)

More options are available (e.g. latex) by using the nbconvert command line tool

Sharing

  • Notebooks can be viewed through a browser without installing any software locally

  • Using nbviewer

  • Notebooks on github are automatically displayed using this

  • Also a multi-user Jupyter server exists, jupyterhub

Export to slides

  • A notebook can be directly exported to slides via nbconvert
  • This is based on reveal.js
  • Unfortunately my experience from writing these slides is that this is slightly buggy, at least for R
    • e.g. axis labels missing on earlier graph

Advance features

(aka features I don't understand yet!)

  • Possible to mix cells using different languages
  • RISE extension allowing live code execution within a slideshow

Conclusion

Pros

  • Great way to write up an analysis as a single document with code, explanation and plots
  • Also a nice interface for simple interactive analysis
  • Browser based, so both interface and documents are highly portable
  • Easy to export notebook to other formats

Cons

  • Jupyter still quite new
    • Installation can take some effort
    • Documentation and online support for R kernel limited
  • Browser interface not as powerful as IDEs like RStudio
  • Version control a bit complicated for this file format

Limitations

  • Traditional editor better for R programming (developing packages etc)
  • knitr better for automatic report generation

Further resources

These slides are at https://github.com/dennisprangle/R-North-East-Talk-2016