Notebooks versus Scripts

When writing code, either scripts (e.g., .R, .py, .sh, etc.) or notebook files (e.g., .Rmd, .ipynb, .qmd, etc.) can be viable options but they have different advantages and disadvantages that we will cover below.

Scripts

The greatest strength of scripts is their flexibility. They allow you to format a file in whatever way is most intuitive to you. Additionally, scripts can be cleaner for iterative operations (i.e., for loops) insofar as they need not be concerned with staying within a given code chunk (as would be the case for a notebook file). Developing a new workflow can be swiftly accomplished in a script as some or all of the code in a script can be run by simply selecting the desired lines rather than manually running the desired chunks in a notebook file. Finally, scripts can also be a better home for custom functions that can be sourced by another file (even by a notebook!) for making repeated operations simpler to read.

The benefit of extreme flexibility in scripts can sometimes be a disadvantage however. We’ve all seen (and written) scripts that have few or no comments or where lines of code are densely packed without spacing or blank lines to help someone new to the code understand what is being done. Scripts can certainly be written in a way that is accessible to those without prior knowledge of what the script accomplishes but they do not enforce such structure. This can make it easy–especially when we’re feeling pressed for time–to exclude structure that helps our code remain reproducible and understandable.

Notebooks

Notebook files’ ability to “knit” or “rendered” as HTML or PDF documents makes them extremely useful in creating outward-facing reports. This is particularly the case when the specific code is less important to communicate than visualizations and/or analyses of the data but notebook files do facilitate echoing the code so that report readers can see how background operations were accomplished. The code chunk structure of these files can also nudge users towards including valuable comments (both between chunks and within them) though of course notebook files do not enforce such non-code content.

Notebook files can fail to knit/render due to issues even when the code within the chunks works as desired. Duplicate code chunk names or a failure to install LaTeX (or related support software) can be a frustrating hurdle to overcome between functioning code and a rendered output file. When code must be re-run repeatedly (as is often the case when developing a new workflow) the stop-and-start nature of running each code chunk separately can also be a small irritation.

Script vs. Notebook Summary

Taken together, both scripts and notebooks can empower users to write reproducible, transparent code. However, both file types have some key limitations that should be taken into consideration when choosing which to use as you set out to create a new code product.

Photo of people walking sampling insects at a farm where a wildflower strip has been planted between two row-cropped areas