-
Notifications
You must be signed in to change notification settings - Fork 0
/
tip_notebook-vs-script.qmd
41 lines (23 loc) · 3.29 KB
/
tip_notebook-vs-script.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
---
title: "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
:::{.panel-tabset}
### Strengths
**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 `source`d by another file (even by a notebook!) for making repeated operations simpler to read.
### Potential Weaknesses
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
:::{.panel-tabset}
### Strengths
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 `echo`ing 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.
### Potential Weaknesses
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.
<p align="center">
<img src="images/lter-photos/patches.jpg" alt="Photo of people walking sampling insects at a farm where a wildflower strip has been planted between two row-cropped areas" width="100%"/>
</p>