introduction | mathematica | jupyter notebook | rstudio | emacs lisp interaction mode

Introduction

A notebook is a code execution format in which the code is organized in a sequence of cells. Each cell shows the code it contains, followed by the output the code produced when it was run.

A notebook looks superficially like a REPL, but in a notebook one can modify the code in a cell and re-run it. The output from the previous run is replaced. Compare this to a REPL, where can only append the new code and its output to the end of the session.

Because mistakes are eliminated, notebooks tend to be closer to something that can be published that a REPL session. Notebooks become even better for publishing results when they are augmented with features for marking up prose and inserting the output latex or plots.

Spreadsheets also have cells which can be subsequently modified. Of course spreadsheets arrange their cells in two dimensions instead of linearly. A disadvantage of spreadsheets is they don't display the code (i.e. the formula) and the output of a cell at the same time. On the other hand, if any of the cells that a cell depends on are updated, a spreadsheet will automatically update the cell. This is not the case for notebooks.

notes on cell identifiers and re-execution

Mathematica

Mathematica has a document format called a notebook. Like a spreadsheet it consists of cells. Whereas spreadsheet cells have a two-dimensional arrangement, notebooks use a one-dimensional arrangement and display the cells one above the other.

The formulas are Mathematica expressions. The Mathematica language is a general purpose language and more expressive than the formula language used by spreadsheets. It is more difficult to learn, however.

Cells can contain multiline expressions. Complete programs really. To indicate that the conents of the cell are done and should be evaluated, use Shift+Enter. Mathematica displays the result of a cell expression underneath the expression. The code and the result are both displayed at all times.

Expressions can refer to the result of other cells using the notation %1, %2, etc. An expression is not re-evaluated when a cell it is dependent upon is re-evaluated, however.

Note that Mathematica has named variables. They don’t strictly speaking correspond to a cell, though there must be a cell which sets the variable. Usually cells will refer to named variables, not cells. Cells can nevertheless get out of date. If you are worried about this, you can re-evaluate the entire notebook by selecting all the cells (⌘A on a Mac) followed by Shift+Enter.

Cells can also get out of date because a cell can be edited without evaluating it.

Each cell has a symbol on the right side called the cell bracket. To the right of that, each pair of input and output cells has a group bracket. If you click on the group bracket and print delete, the pair of cells is deleted. Double clicking on one of the cell brackets makes the other cell bracket in the group collapse. Thus, to hide an output cell that contain a lot of data, double click the matching input cell bracket.

If you cell an output cell, you get a "suggestion bar" below it. The suggestion bar mentions some of the assumptions that Mathematica made when performing the calculation. It also lists related functions.

To stop a Mathematica computation that is taking too long on a Mac, use ⌘..

keystroke mathematical notation
Ctrl+/ fraction
Ctrl+ radical
Ctrl+^ exponent

Jupyter Notebook

Jupyter Notebook

Publishing Online

Jupyter notebooks are JSON. You can save one as a gist and then render it using this website:

nbviewer.jupyter.org

Installation

Downloaded Anaconda distribution.

Supposing it is installed at ~/Local/src/anaconda, create a symlink in your search path to ~/Local/src/anaconda/bin/jupyter-notebook

How to run the Jupyter notebook:

$ jupyter-notebook

This launches the Jupyter notebook webserver and points the default browser to it. The webserver runs in the foreground of the terminal.

Use %pylab or %matplotlib to render plots inline. This commands also import symbols from numpy, scipy, and matplotlib.

If the notebook webserver is launched in a directory (say ~/Notebooks), then any notebooks that are saved there will be listed in the launch page.

I would prefer that Jupyter notebook launched like other GUI apps, even though it also requires a browser. Until that is possible, I wrote some code to install it as a process that launches when I log in: https://github.com/clarkgrubb/home/tree/master/darwin/launchd. It listens on http://127.0.0.1:8888.

Packages

The Anaconda distribution installs a separate python from the system python. To install packages, find the conda command in the Anaconda installation directory:

$ ./bin/conda search seaborn

$ ./bin/conda install seaborn

It is not necessary to restart the Jupyter notebook to use the newly installed package.

Keybindings

Shift-Enter run cell
Ctrl-Enter run cell in place|
Ctrl-m a insert cell above
Ctrl-m b insert cell below
Ctrl-m d d delete cell
Ctrl-m x cut cell
Ctrl-m c copy cell
Ctrl-m v paste cell
Ctrl-m o toggle output
Ctrl-m O toggle output scroll (for output longer than about 10 lines)
Ctrl-m l toggle line numbers
Ctrl-m s save notebook
Ctrl-m j move cell down
Ctrl-m k move cell up
Ctrl-m y code cell (opposite of markdown cell or raw cell)
Ctrl-m m markdown cell
Ctrl-m t raw cell (display as ASCII text)
Ctrl-m 1-6 heading 1-6 cell
Ctrl-m p select previous
Ctrl-m n select next
Ctrl-m i interrupt kernel
Ctrl-m . restart kernel
Ctrl-m h show keyboard shortcuts

Commands

The initial directory is the directory where the webserver was launched?

  • cd
  • ls
  • mkdir
  • pwd

Customizing

Every action which has a button in the toolbar also has a keyboard shortcut.

View | Togger Header
View | Toggle Toolbar

Non-Code

Can make the cell Markdown or Raw. In Markdown the $ $ and $$ $$ syntax is honored for LaTeX.

RStudio

this describes R; update for RStudio

notes on using the markdown mode

When R is run as a GUI application, it has a variety of window types. We mention four:

  • console
  • editor
  • graphics
  • help

On Mac OS X, the windows are full-fledged operating system windows, but on Windows these "windows" are inside a larger application window.

There can be only one console window. It can be used like a REPL: type R commands and see the output. It can also be used to create or modify graphics which are displayed in the graphics window. When R is run from the command line, it is as if the console window were the only window available.

The console window has readline, so commands which were previously entered can be edited and re-run. The console by itself is not a notebook. One can use an editor window and the console window together to get an arrangement that is a bit like a notebook, however.

The editor window is associated with an R source code file. Usually such a file will have an .R suffix. Selecting code in the editor and typing ⌘Enter (on Mac) causes the code to be executed in the console. Multiple statements can be executed. Saving the source code is usually sufficient to preserve the session. If one wants to save the output of the session, one can save select the console window and save the workspace, messy as it may be. R uses a .txt suffix when saving a workspace.

There is usually a single graphics window, and multiple calls to plot and other graphing functions overwrite the results of the previous call. On Mac the graphics window is called the Quartz window. One can create a second window with ⇧⌘N or in the console with a command like this:

quartz("2nd window title")

Each graphics window has a history of the images it has displayed which can be navigated with ⌘→ and ⌘←. You might think that the graphics window is unavailable when R is run at the command line, but on Mac OS X at least it will launch a Quartz window when graphics are needed.

When run as a Mac GUI app, help creates a separate window. On Windows help opens a local page in the browser. When R is running as a command line app, help writes the documentation to standard out.

Emacs Lisp Interaction Mode

Emacs Lisp Interaction Mode is a Lisp Notebook. The state includes global variables which control the editor behavior. In fact, one can define new functions, change key bindings, or modify buffers from Lisp Interaction Mode. It is possible to use the buffer in a self-contained way, however.

One can put a buffer into Lisp Interaction Mode with M-x lisp-interaction-mode. You can save and reload the buffer like any other file. To make Emacs open the file in Lisp Interaction Mode, put this at the top:

;; -*- mode: lisp-interaction; -*-

To evaluate a cell, put the point at the end of the sexp and type C-j. The value will be inserted on the following line.

One can use M-x eval-buffer or M-x eval-region to evaluate multiple sexps, but the values of the sexps is not inserted underneath them. If values were already under the sexps, those values are evaluated (probably to themselves) as well. Thus there is no easy way to re-evaluate the entire "notebook". It seems possible, though challenging, to write Lisp code to do this. The challenge would be in distinguishing the sexps from their values. An inexact heuristic would be to take all lines that start with an open paren.

Running Notebooks

action mathematica ipython notebk. rstudio emacs lisp inter.
evaluate cell ⇧Enter ⇧Enter select stmt. in editor
⌘Enter
C-j
evaluate sheet Evaluation | Evaluate Notebook ⌘A ⌘Enter none
create sheet
save sheet ^M S
abort computation