Literate, linear programming and modularity
Humboldt-Universität zu Berlin
Fri Aug 23, 2024
here
scripts
folder (which you might’ve named analysis
or something else)how you write your code is the first step in making it reproducible
the first principle is that your code must be linear
Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do.
— Knuth (1984), p. 97
# Analysis script for phoneme paper
# author: Joe DiMaggio
# date: Feb. 29, 2024
# purpose: analyse cleaned dataset
# Set-up ###
# load required packages
library(dplyr) # data wrangling
library(readr) # loding data
library(here) # project-relative file path
# Load-in data
df_phon <- read_csv(here("data", "phoneme_tidy_data.csv"))
# Explore data ###
summary(df_phone)
###
)
.Rmd
file extension) and Quarto (.qmd
) are extensions of markdown
Task: New Quarto document
File > New file > Quarto document
scripts
and save it as 01-literate-programming.qmd
..R
files contain (R) source code only.Rmd
files are dynamic reports that support
.qmd
files are dynamic reports (RStudio v2022.07 or later
Check your RStudio version
Run the following in the Console: RStudio.Version()$version
2022.07
or higher you can use QuartoHelp > Check for updates
---
YAML
title
)author: ‘firstname lastname’
(see example below)toc
) by changing format
so that it looks like this:# Set-up
)Code > Insert Chunk
Cmd+Opt+I
(Mac) / Ctrl+Alt+I
(Windows)Adding structure and code chunks
#
) and subheadings (##
) accordinglydplyr
version 1.1.0
or later, which introduced the .by
per-operation grouping argumentdplyr
tries to run your code?
Session > Restart R
or Cmd/Ctrl+Shift+0
Follow the instructions on the workshop website: Hands-on: working with Quarto
My session info.
R version 4.4.0 (2024-04-24)
Platform: aarch64-apple-darwin20
Running under: macOS Ventura 13.2.1
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: Europe/Berlin
tzcode source: internal
attached base packages:
[1] stats graphics grDevices datasets utils methods base
loaded via a namespace (and not attached):
[1] vctrs_0.6.5 cli_3.6.3 knitr_1.48 rlang_1.1.4
[5] xfun_0.47 renv_1.0.7 jsonlite_1.8.8 glue_1.7.0
[9] rprojroot_2.0.4 htmltools_0.5.8.1 hms_1.1.3 fansi_1.0.6
[13] rmarkdown_2.28 evaluate_0.24.0 tibble_3.2.1 tzdb_0.4.0
[17] fastmap_1.2.0 yaml_2.3.10 lifecycle_1.0.4 compiler_4.4.0
[21] pkgconfig_2.0.3 here_1.0.1 rstudioapi_0.16.0 digest_0.6.36
[25] R6_2.5.1 readr_2.1.5 utf8_1.2.4 pillar_1.9.0
[29] magrittr_2.0.3 tools_4.4.0