Lab #11: R and Jupyter


The objective of this final lab is learn a bit about two popular, related technologies that will complement your newly-acquired Python skillset: (1) the R programming language; (2) Jupyter notebooks. Because Jupyter stands for JUlia + PYthon + R, these new skills might also provide a basis for exploring the popular Julia language, which some of you will likely encounter in your future scientific work.

The R language

This component will be taught by Professor Gregg Whitworth.

Jupyter Notebooks

Jupyter started as a project called iPython, whose goal was to embed code, graphics, and text in a publishable format called a notebook, which runs in a web browser. You can think of a notebook as an alternative to the IDLE editor/shell approach we’ve taken so far in this course. If you’re used to IDLE, Jupyter can seem a bit confusing, because everything (code, graphics, output) appears in the same browser window. To distinguish between your code and the output it produces, Jupyter labels everything as either In: or Out:. Once you’ve adjusted to that convention, Jupyter is straightforward.

Launching Jupyter

Open a terminal window and enter the command


Doing this will cause a web browser (either Chrome or Firefox) to open, with a page showing the contents of your home directory.

Starting a new notebook

In the upper-right corner of the Jupyter page, you should see a little New menu. From this menu select Python3 to launch a new interactive notebook session.

Jupyter magic

To support cool features like plotting directly in your web browser, Jupyter provides some special commands that (in the spirit of rational scientific inquiry) are called magic. These commands begin with a percent sign. To make Jupyter plot in your browser, put your cursor in the In: box and type

    %matplotlib inline

Now you can continue coding as if you were entering a script into the IDLE editor.

For my first Jupyter notebook, I chose the simplest program I could think of that would show me a plot in my browser, namely, the little sine-wave plotting program we wrote for our very first lab. Once you’ve entered this little program into the In: box, you can hit the little skip_next (Run) button at the top to run your code. After displaying your plot, the notebook will provide you with another In: box where you can add more code, run it with the Run button, and repeat. For example, after plotting a sine wave, I added a couple of lines to plot the cosine of the same x values.

Save / test / rename / test / submit / download / re-test

Clicking on the little ? (Save) icon in the upper-left will save your Jupyter notebook to your home directory, automatically named UntitledN.ipynb (where N = 1, 2, 3, …). To load your notebook, open it from the File menu at the top of the Jupyter page, and you should see your code and plots. Once you’re happy with your notebook (just two plots is sufficient), use the Mac Finder (desktop) to rename it to lab11.ipynb. Then re-load it into Jupyter, add the usual header comments (names, date, class) in the topmost In: box, save it out again, reload it, and test it once more before submitting. As usual, take a moment after submitting to download and re-test your work!