General Jupyter Notebook Anatomy

Super quickstart:

  1. Up and down arrows take you through each "cell" of the notebook (a cell == a chunk in Rmd)
  • To run a cell, either the text (markdown, as we'll see) or code, it's Shift-Enter (Shift and Enter at same time)

Disclaimers:

  1. Notebooks are no substitute for an IDE for developing apps (yet).
  • Notebooks are not suitable for debugging code (yet).
  • They might be a substitute for r-markdown, unless you want auto-generated reporting.

  1. My main use of notebooks are for interactive teaching mostly and as a playground for some code that I might like to share at some point (I can add useful and pretty markup text, pics, videos, etc).
  • I'm a fan also because github render's ipynb files nicely (even better than r-markdown for some reason).
  • Static versions are still very helpful to follow along to.
  • If the correct latex libraries are installed, beautiful pdfs can be generated latex style.

Intro to usage

In this figure are a few labels of notebook parts I will refer to

This is a little diagram of the anatomy of the notebook toolbar

Try out these steps as you read through this:

  1. You can click in the Notebook Name area and rename your notebook
  2. Your notebook is saved to the directory you started this server from (as a general rule though I download the notebook at intervals in addition to saving it just in case)
  3. To download the notebook, go to the Menu bar -> File -> Download As -> IPython Notebook (.ipynb) (if you do this you will see you can actually download in many different formats)
  4. In the toolbar you can add a new cell with the +, run the notebook (the triangle button - might look different in your notebook like a triangle with a line after it)
  5. Cell type defaults to Code. You can change this to Markdown if you want to take notes (markdown is it's own language for which you can find guides). Markdown is what all the text here is written in, so double click on this text to see the raw markdown. Also, for whatever reason you can embed any html you want (good way to include pics). Aaaaaand, LaTeX equations seem to render just fine with some exceptions.
  6. Code cell types are grey
  7. Most importantly, to run a cell hit the run button in toolbar or just type Shift-Enter (try!)

In [ ]:

Shortcuts!!!

  • A complete list is here, but these are my favorites:
Mode What Shortcut
Command (Press Esc to enter) Run cell Shift-Enter
Command Add cell below B
Command Add cell above A
Command Delete a cell d-d
Command Turn code cell to markdown cell M
Command Turn markdown cell to code cell Y
Command Go into edit mode Enter
Edit (Press Enter to go into a cell) Run cell Shift-Enter
Edit Indent Clrl-]
Edit Unindent Ctrl-[
Edit Comment/uncomment section toggle Ctrl-/
Edit Function introspection Tab

Try some below

Convert the next cell (a code cell) into markdown (Esc to make sure you're in command mode and M for markdown) (Don't forget to hit the Shift-Enter shortcut to run a cell):


In [ ]:
# turn this comment from code to a heading 1 (h1)

This sentence is one cell (a markdown cell) so please add a new cell below this one (Esc to make sure you're in command mode and B for below).

Comment/not comment toggle. Run the cell with Shift-Enter. Next, uncomment the second line of code in the following code cell (Ctrl-/ which is Ctrl and / together):


In [ ]:
library(ggplot2)

# mtcars$am <- factor(mtcars$am)

ggplot(mtcars, aes(x = mpg, y = hp, color = am)) + geom_point()

Latex/markdown in notebooks


In [ ]:
#### OK, change this cell to markdown to see some examples (you'll recognize this if you speak markdown)
# This will be Heading1
1. first thing
* second thing
* third thing

A horizontal rule:

---
> Indented text

Code snippet:

```r
df <- data.frame()
```

LaTeX inline equation:

$\Delta =\sum_{i=1}^N w_i (x_i - \bar{x})^2$

LaTeX table:

First Header  | Second Header
------------- | -------------
Content Cell  | Content Cell
Content Cell  | Content Cell

HTML:

<img src='http://www.elm.org/wp-content/uploads/2014/05/gold-star.jpg' alt="You get a gold star" width="42" height="42" align="left">

Try out a code cell (run shortcut - Shift-Enter)


In [ ]:
paste0(1:10)
print(sprintf("Your current R version is %s", R.version.string))

Open/upload/new

  1. As you can see on your jupyter homepage you can open up any notebook

    NB: You can return to the homepage by clicking the Jupyter icon in the very upper left corner at any time

  2. You can also Upload a notebook (button on upper right)



  3. As well as start a new notebook with a specific kernel (button to the right of Upload)

Note about In/Out

So, what's that number after In or Out? That's the order of running this cell relative to other cells (useful for keeping track of what order cells have been run). When you save this notebook that number along with any output shown will also be saved. To reset a notebook go to Cell -> All Output -> Clear and then Save it.

Viewing

You can do something like this to render a publicly available notebook on github statically (this I do as a backup for presentations and course stuff):

http://nbviewer.jupyter.org/github/<username>/<repo name>/blob/master/<notebook name>.ipynb

like:
http://nbviewer.jupyter.org/github/michhar/rpy2_sample_notebooks/blob/master/TestingRpy2.ipynb


Also, you can upload or start a new interactive, free notebook by going here:
https://tmpnb.org





Free tier account with Azure Machine Learning Studio (now has R-flavored notebooks)

The nifty thing about Jupyter notebooks (and the .ipynb files which you can download and upload) is that you can share these. They are just written in JSON language. I put them up in places like GitHub and point people in that direction.

Some people (like this guy who misses the point I think) really dislike notebooks, but they are really good for what they are good at - sharing code ideas plus neat notes and stuff in dev, teaching interactively, even chaining languages together in a polyglot style. And doing all of this on github works really well (as long as you remember to always clear your output before checking in - version control can get a bit crazy otherwise).

Some additional features

  • tab completion
  • function introspection
  • help

In [ ]:
# first, let's install some packages to play with
# replace lib string with your local library path
install.packages(c("ggplot2"),
                repos = "http://cloud.r-project.org",
                lib = "C:/Users/michhar/Documents/R/win-library/3.2")

In [ ]:
# tab completion and introspection

ggplot2::q # hit tab at end
plot() # place cursor in () and tab

In [ ]:
# help!
library(rjson)

?fromJSON

In [ ]:
# My feeble attempt at debugging - your mission if you choose to accept it is to figure this out
#library(htmlwidgets)
#fname = 'tmp.html'

options(error = browser())

f <- function(a) g(a)
g <- function(b) h(b)
h <- function(c) i(c)
i <- function(d) "a" + d
f(10)
t <- traceback()

#saveWidget(t, file = fname, selfcontained = F)
#IRdisplay::display_html(paste("<iframe src=' ", fname, " ' width = 100% height = 400>"))

Created by a Microsoft Employee.

The MIT License (MIT)
Copyright (c) 2016 Micheleen Harris


In [ ]: