21  Code Check

21.1 What it checks

The code_check module analyses the code files shared with a paper. Building on the file list from repo_check, it identifies code files (R, SAS, SPSS, Stata), then checks each for signs of reproducibility problems: whether files parse without errors, whether they contain comments, whether they use absolute file paths (which break on other machines), whether files they try to load are actually present in the repository, and whether libraries are loaded in multiple places rather than in a single block near the top of the script (loading imports in one place makes a script’s dependencies easy to see).

Note

This module makes live network calls to retrieve and inspect code files from the linked repositories. You need an internet connection to run the code below.

21.2 Running the module

paper <- demopaper()
mo <- module_run(paper, "code_check")
mo$traffic_light
#> [1] "yellow"
cat(mo$summary_text)
#> 
#> -  We found 4 R, 0 SAS, 0 SPSS, and 0 Stata code files.
#> -  All your code files had comments.
#> -  4 files loaded in the code were missing in the repository.
#> -  Absolute file paths were found.
#> -  Libraries/imports were loaded in multiple places.
#> -  No parsing issues of R-type files were found.

The table has one row per code file, with the reproducibility checks as columns:

mo$table[, c("file_name", "language", "parse_error", "absolute_paths",
             "percentage_comment", "loaded_files_missing")] |>
  knitr::kable()
file_name language parse_error absolute_paths percentage_comment loaded_files_missing
test_script.R R FALSE /lisa/file.csv
C:/lisa/file.csv 0.1666667 1
bad.R R FALSE C://Lakens/files/example.csv 0.0714286 1
bad.Rmd R FALSE C://Lakens/files/example.csv
C://Lakens/files/example2.csv 0.0666667 1
Code/Study 1.r R FALSE /lisa/file.csv
C:/lisa/file.csv 0.1666667 1

21.3 A clean example and one with problems

Common problems the module surfaces:

  • Absolute paths (absolute_paths == TRUE) — e.g. read.csv("C:/Users/me/data.csv") — which will not run on anyone else’s computer.
  • Missing loaded files (loaded_files_missing > 0) — the script reads a file that was not shared.
  • Parse errors (parse_error == TRUE) — the file is not valid code.
mo$table[, c("file_name", "absolute_paths", "loaded_files_missing", "parse_error")] |>
  knitr::kable()
file_name absolute_paths loaded_files_missing parse_error
test_script.R /lisa/file.csv
C:/lisa/file.csv 1 FALSE
bad.R C://Lakens/files/example.csv 1 FALSE
bad.Rmd C://Lakens/files/example.csv
C://Lakens/files/example2.csv 1 FALSE
Code/Study 1.r /lisa/file.csv
C:/lisa/file.csv 1 FALSE

21.4 Options

code_check shares repo_check’s local-file options, plus a limit on how many files to inspect:

# inspect at most 5 files (default is 20)
module_run(paper, "code_check", file_limit = 5)

# check code in a local folder only, no online lookups
module_run(paper, "code_check", local_path = "path/to/files", local_only = TRUE)

See the Local Files chapter for a full walkthrough of checking local code, including cloud-synced folders.

21.5 Notes

The module reports on potential problems. An absolute path or a missing file is not always an error — but it is usually worth a second look if you want the analysis to be reproducible.