I find R Notebooks to be pure joy.
Notebooks are really cool little programming environments that seamlessly combine executable code chunks, inline graphs and tables, Latex, and markdown all in one place. If you like Python, you may already be familiar with IPython or Jupyter notebooks (which support R, in fact). R Notebooks are very similar, although I find them even easier to use. If you like, play along with a basic tutorial notebook I threw together.
Here is a brief, giddy introduction to some of the things I love about R Notebooks, in no particular order.
1. Inserting Code
It’s easy, and not even limited to R–you can insert Python, SQL, Stan, and a few others, too. R Notebooks will output everything inline (printing, tables, plots, etc.) and handles some automatic formatting. For instance, if you print a dataframe, it will automatically put it in this pretty format for you:
You can write R code just as you’re accustomed to. Load in libraries (as seen above), use other packages–anything you want. You can also specify a bunch of arguments in the header of a chunk to customize behavior, like including (or not) that code chunk and its results in the finished file, for example.
You can also do inline code with the `
r` wrapper, which shows results inline but not the code. This is useful for referencing a variable that’s subject to change, for example; you can reference the variable name, and R will supply the data and format the text.
A code chunk executes in the global environment, so other chunks all have access to anything defined by the others. You can even define global parameters in the header that get used throughout the document (you can also set these values when you knit if you knit with parameters).
2. Markdown and Latex
The text outside of code snippets is Markdown text. This is a handy reference for basic Markdown syntax. Markdown can take some getting used to, but the results look amazing.
You can also do inline Latex formatting just wrap it in mathmode (
$ ) tags, and format away. R Notebooks even support hover-previewing for single-line expressions.
Toil no more with Word’s equation editor, good people.
3. Plots and tables
We’ve all experienced the nightmare that is trying to put a figure in a Word or Pages document. Not only does it end up in the wrong place, but it splits your paragraph apart and adds eight blank pages.
We can all wake up from this nightmare. In R Notebooks, plots are generated under their code chunk and stay there, right where you want them. To clean up the report, you can even hide the code chunk that generated it. No need to save the figures separately–you can just make them on the fly each time.
You can also format and out put tables using a variety of packages, which also end up where they ought to.
4. Endless possibilities
There is so much nuance and customization you can add to these notebooks.
You can add a bunch of different parameters to the YAML header (including cool stuff like tables of contents), you can make or use templates, you can customize code chunk behavior globally or locally, you can set a bunch of global or local
knitr options–you can do HTML tabsets to turn sections into tabs.
Citations and bibliographies are supported, although I have never used them!
If you’re just getting started, or aren’t looking for anything too fancy, the basic notebook functionality will more than meet your needs. If you’re really savvy, you can get a remarkable amount of mileage out of the more hidden features of the notebooks. Either way, something for everybody.
5. Look how pretty!
Even if you’re a total scrub like me, you get really lovely results from these notebooks:
There are a ton of supported file formats, and they all look great. I think the HTML notebooks look the best, myself, but it also knits really nicely to PDF and other formats.
R Notebooks are extraordinary useful. They’re great if you need to share results with collaborators or want to throw together a quick report on some data. They are super useful for code tutorials and walkthroughs, since you can show the raw code, the results, and also link to helpful resources within the text. If you’re still taking classes, they make for lovely statistics homework. Plus, because the .Rmd files are just text, they’re small and so are easy to version with Git.
They really are a great tool. Easy to use if you just want the basics, but with a lot of customization available if you want to go deeper. The results look great, and as it becomes more important to have a clear, transparent, and reproducible workflow, R Notebooks are a great tool to have in your arsenal.