Literate, linear programming and modularity
Leibniz-Zentrum Allgemeine Sprachwissenschaft
Thu Oct 17, 2024
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
phoneme_analysis.R
# [example script]
# 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)
# scatterplot: phoneme duration by mean f0
plot(df_phon$duration, df_phon$mean_f0)
###
) –> creates structured Outline
phoneme_analysis.R
make the second script much easier to followscripts
folder (which you might’ve named analysis
or something else).Rmd
and .qmd
.Rmd
file extension) and Quarto (.qmd
) are extensions of markdown
phoneme_analysis.R
, but we have no idea what the scatterplot it produced looks likeTask: 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_info
or something of the like)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.1 (2024-06-14)
Platform: aarch64-apple-darwin20
Running under: macOS Sonoma 14.6
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.2 knitr_1.47 rlang_1.1.4
[5] xfun_0.45 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.27 evaluate_0.24.0 tibble_3.2.1 tzdb_0.4.0
[17] fastmap_1.2.0 yaml_2.3.8 lifecycle_1.0.4 compiler_4.4.1
[21] pkgconfig_2.0.3 here_1.0.1 rstudioapi_0.16.0 digest_0.6.35
[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.1
Reproducible code