<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/why-i-love-r-notebooks/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.rstudio.com/rviews/2017/03/15/why-i-love-r-notebooks/"> RStudio</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>

<p>by Nathan Stephens</p>
<p><em>Note: <a target="_blank" rel="nofollow" href="https://blog.rstudio.org/2016/10/05/r-notebooks/">R Notebooks</a> requires <a target="_blank" rel="nofollow" href="https://www.rstudio.com/products/rstudio/download/">RStudio Version 1.0</a> or later</em></p>
<p>I’m a big fan of the R console. During my early years with R, that’s all I had, so I got very comfortable with pasting my code into the console. Since then I’ve used many code editors for R, but they all followed the same paradigm – script in one window and get output in another window. Notebooks on the other hand combine code, output, and narrative into a single document. Notebooks allow you to interactively build narratives around small chunks of code and then publish the complete notebook as a report.</p>
<p><img alt="R Notebooks are a method of Literate Programming that allows for direct interaction with R while producing a reproducible document with publication-quality output." src="https://i2.wp.com/www.rstudio.com/wp-content/uploads/2017/03/notebook-demo.png?w=456" data-recalc-dims="1"></p>
<p>R Notebooks is a new feature of RStudio which combines the benefits of other popular notebooks (such as Jupyter, Zeppelin, and Beaker) with the benefits of R Markdown documents. As a long time R user I was skeptical that I would like this new paradigm, but after a few months I became a big fan. Here are my top three reasons why I love R Notebooks.</p>
<div>
<h2>Number 3: Notebooks are for doing science</h2>
<p>If scripting is for writing software then notebooks are for doing data science. In high school science class I used a laboratory notebook that contained all my experiments. When I conducted an experiment, I drew sketches and wrote down my results. I also wrote down my ideas and thoughts. The process had a nice flow which helped me improve my thinking. Doing science with physical notebooks is an idea that is centuries old.</p>
<p><img alt="Leonardo da Vinci was a prolific user of notebooks, creating thousands of pages that are still around today. This page from the Codex Atlanticus shows notes and images about water wheels and Archimedean Screws." src="https://i1.wp.com/www.rstudio.com/wp-content/uploads/2017/03/labnotebook4.png?w=456" data-recalc-dims="1"></p>
<p>Electronic notebooks follow the same pattern as physical notebooks but applies the pattern to code. With notebooks you break your script into manageable code chunks. You add narrative and output around the code chunk which puts it into context and makes it reproducible. When you are done, you have an elegant report that can be shared with others. Here is the thought process for doing data science with notebooks:</p>
<ul>
<li>I have a chunk of code that I want to tell you about.</li>
<li>I am going to execute this chunk of code and show you the output.</li>
<li>I am going to share all chunks of code with you in a single, reproducible document.</li>
</ul>
<p>If you do data science with R scripts, on the other hand, you develop your code as a single script. You add comments to the code, but the comments tend to be terse or nonexistent. Your output may or may not be captured at all. Sharing your results in a report requires a separate, time consuming process. Here is the thought process for doing data science with scripts:</p>
<ul>
<li>I have a thousand lines of code and you get to read my amazing comments!</li>
<li>Hold onto your hats while I batch execute this entire script!</li>
<li>You can find my code and about 50 plots under the project directory (I hope you have permissions).</li>
</ul>
</div>
<div>
<h2>Number 2: R Notebooks have great features</h2>
<p>R Notebooks are based on <a target="_blank" rel="nofollow" href="http://rmarkdown.rstudio.com/">R Markdown</a> documents. That means they are written in plain text and work well with version control. They can be used to create elegantly formatted output in multiple formats (e.g. HTML, PDF, and Word).</p>
<p>R Notebooks have some features that are not found in traditional notebooks. These are not necessarily inherent differences, but differences of emphasis. For example, R Markdown documents gives you many options when selecting graphics, templates, and formats for your output.</p>
<table>
<thead>
<tr>
<th><strong>Feature</strong></th>
<th align="center"><strong>R Notebooks</strong></th>
<th align="center"><strong>Traditional Notebooks</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>Plain text representation</td>
<td align="center">✓</td>
<td align="center"></td>
</tr>
<tr>
<td>Same editor/tools used for R scripts</td>
<td align="center">✓</td>
<td align="center"></td>
</tr>
<tr>
<td>Works well with version control</td>
<td align="center">✓</td>
<td align="center"></td>
</tr>
<tr>
<td>Create elegantly formatted output</td>
<td align="center">✓</td>
<td align="center"></td>
</tr>
<tr>
<td>Output inline with code</td>
<td align="center">✓</td>
<td align="center">✓</td>
</tr>
<tr>
<td>Output cached across sessions</td>
<td align="center">✓</td>
<td align="center">✓</td>
</tr>
<tr>
<td>Share code and output in a single file</td>
<td align="center">✓</td>
<td align="center">✓</td>
</tr>
<tr>
<td>Emphasized execution model</td>
<td align="center">Interactive &amp; Batch</td>
<td align="center">Interactive</td>
</tr>
</tbody>
</table>
<p>When you execute a code chunk in an R Notebook, the output is cached and rendered inside the IDE. When you save the notebook, the same cache is rendered inside a document. The HTML output of R Notebooks is a dual file format that contains both the HTML and the R Markdown source code. The dual format gives you a single file that can be viewed in a browser or opened in the RStudio IDE.</p>
<p><img alt="Notebooks store output in a local cache. The cache is used for the IDE and for saved output. Notebooks save their output to a dual file format that contains both HTML and the source code." src="https://i2.wp.com/www.rstudio.com/wp-content/uploads/2017/03/nbWorkings.png?w=456" data-recalc-dims="1"></p>
</div>
<div>
<h2>Number 1: R Notebooks make it easy to create and share reports</h2>
<p>My favorite part of R Notebooks is having the ability to easily share my work. With R notebooks a high quality report is an automatic byproduct of a completed analysis. If I write down my thoughts while I analyze my code chunks, then all I have to do is push a button to render a report. I can share this report by publishing it to the web, emailing it to my colleagues, or presenting it with slides. This video shows how easy it is to create a report from an R Notebook. </p>
<p><center><br>
<iframe width="450" allowfullscreen src="https://player.vimeo.com/video/208170015?loop=1&color=ffffff&byline=0&portrait=0"><br>
</iframe><br>
</center>
</div>
<div>
<h1>R Notebooks for data science</h1>
<p>The following table summarizes the differences between notebooks and scripts.</p>
<table>
<colgroup>
<col width="27%">
<col width="27%">
<col width="27%">
</colgroup>
<thead>
<tr>
<th><strong>Activity</strong></th>
<th><strong>R Notebook</strong></th>
<th><strong>R Script</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>Building a narrative</td>
<td>Rich text is added throughout the analytic process describing the motivation and the conclusions for each chunk of the code.</td>
<td>Comments are added to the script, and a report that describes the entire analysis is drafted after the script is completed.</td>
</tr>
<tr>
<td>Organizing plots, widgets, and tables</td>
<td>All output is embedded in a single document and collocated with the narrative and code chunk to which it belongs.</td>
<td>Each individual output is sent to file and is collected later into a report.</td>
</tr>
<tr>
<td>Creating reports</td>
<td>Rendering the final report is instant. The same document can be published to multiple formats (e.g. HTML, PDF, Word). Since the document is based on code, future changes are easy to implement and the document is reproducible by others.</td>
<td>Creating a report is a separate, time consuming step. Any changes to the report can be time consuming and prone to error. Since the report is not tied to code, it is not reproducible.</td>
</tr>
</tbody>
</table>
<p>R Notebooks are not designed for all the work you do in R. If you are writing software for a new package or building a Shiny app you will want to use an R script. However, if you are doing data science you might try R Notebooks. They are great for tasks like exploratory data analysis, model building, and communicating insights. Notebooks are useful for data science because they organize narrative, code, and text around manageable code chunks; and creating quality, reproducible reports is easy.</p>
<p><em>References: For an introduction to R Notebooks see this <a target="_blank" rel="nofollow" href="https://youtu.be/zNzZ1PfUDNk">video</a> or <a target="_blank" rel="nofollow" href="https://blog.rstudio.org/2016/10/05/r-notebooks/">blog post</a>. For more detailed information, see this <a target="_blank" rel="nofollow" href="https://github.com/rstudio/rstudio-conf/blob/master/2017/R_Notebook_Workflows-Jonathan_McPherson/r-notebook-workflows.Rmd">workflow</a> presentation or the <a target="_blank" rel="nofollow" href="http://rmarkdown.rstudio.com/r_notebooks.html">reference site</a>.</em></p>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.rstudio.com/rviews/2017/03/15/why-i-love-r-notebooks/"> RStudio</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/pygnzJB-17U"></a></div>