Key Points

Introduction


  • R is a programming language and RStudio is an environment for working with R.
  • Scripts make analyses easier to repeat, share, and maintain.
  • Reproducibility also depends on managing packages and computing environments.

Reproducibility


  • Reproducibility: Same data and analysis, same results.
  • Reproducible does not necessarily mean correct.
  • Replicability, robustness, and generalisability help us assess findings beyond the original analysis.
  • Good coding practices make research easier to check, maintain, and reuse.

Environments


  • Package versions can affect results, even when code and data stay the same.
  • renv creates an isolated package library for each project.
  • DESCRIPTION declares required packages, and renv.lock records exact versions.
  • Use renv::snapshot() to record installed versions and renv::restore() to recreate the recorded package environment.

Code smells


  • Code smells are warning signs of potential problems, even when the code runs correctly.
  • Common code smells include:
    • Duplicated code
    • Hardcoded paths
    • Magic numbers
    • Commented-out code
    • Lack of modularity
    • Missing tests
    • Poor documentation
    • Unexplained code
    • Overcommenting
  • Code smells can be identified by recognising common patterns.
  • Smells don’t always mean bugs, but they usually reduce readability and maintainability, making code harder to understand, update, and trust.
  • A smell warrants inspection as they often point to places where the code is most likely to break in the future.

Documentation


  • Research software has multiple audiences — users, developers, maintainers, and “future you” — each requiring different documentation.
  • Documentation comes in three types: procedural (step-by-step guides), conceptual (background, explanations, diagrams), and reference (APIs, parameters, system details)
  • In-code comments should be minimal and purposeful. Over-commenting distracts from the code, increases cognitive load, and often indicates unclear naming or structure.
  • Roxygen comments describe the purpose and behaviour of functions, classes, and modules. They improve readability and can be used to auto-generate reference documentation.

Packaging


  • R packages organise reusable code, documentation, and data in a standard structure.
  • RStudio and usethis can create package structures automatically.
  • Store functions in R/ and declare dependencies in DESCRIPTION.
  • Use devtools::load_all() to test functions during development.
  • Document and export functions using roxygen2, and generate help pages with devtools::document().
  • Use devtools::check() to identify issues before sharing a package.
  • Share packages through platforms such as GitHub and use version numbers to track changes.