Quiz: R Packages

Week 6 · Advanced Statistical Programming using R

Recap: Quarto & Collaborative Coding

Q1

What is the difference between git fetch and git pull?

  1. fetch only downloads remote commits; pull downloads and merges them
  2. fetch is for branches; pull is for files
  3. They are aliases for the same command
  4. fetch requires admin permissions; pull does not

Answer: 1) pull = fetch + merge. Use fetch if you want to inspect remote commits before integrating them.

Q2

Two students edit report.qmd at the same time and get a merge conflict. Which strategy reduces this?

  1. Push to main faster than the other student
  2. Use {{< include _eda.qmd >}} in report.qmd so each student owns a separate file
  3. Always use git push --force
  4. Delete the file and recreate it after each conflict

Answer: 2) Splitting into includes lets each person own a file. report.qmd only changes when structure changes.

R Packages

Q3

Which file holds your package’s name, version, licence, and dependencies?

  1. NAMESPACE
  2. README.md
  3. DESCRIPTION
  4. .Rbuildignore

Answer: 3) DESCRIPTION is the metadata file. NAMESPACE is auto-generated from your roxygen2 comments.

Q4

Which file should you NEVER edit by hand?

  1. DESCRIPTION
  2. NAMESPACE
  3. README.md
  4. R/myfunction.R

Answer: 2) NAMESPACE is managed by devtools::document(). Manual edits get overwritten next time you regenerate docs.

Q5

Which package is for setting up a project, and which is for doing development (build, load, test)?

  1. usethis for setup, devtools for doing
  2. devtools for setup, usethis for doing
  3. Both do the same thing
  4. roxygen2 for both

Answer: 1) usethis scaffolds files (use_r, use_package, use_data_raw); devtools runs the dev loop (load_all, document, test, check).

Q6

Why should you never write library(ggplot2) inside a package function?

  1. R does not allow library() calls inside functions
  2. It modifies the user’s session. Declare ggplot2 in DESCRIPTION Imports and call ggplot2::aes() with ::
  3. library() is slower than require()
  4. Packages cannot use ggplot2

Answer: 2) Packages declare dependencies in DESCRIPTION and call imported functions with ::.

Q7

What does devtools::load_all() do?

  1. Installs the package permanently into your R library
  2. Loads your in-development package into the current session without install step
  3. Loads all R scripts in your home directory
  4. Downloads all packages listed in DESCRIPTION

Answer: 2) load_all() is the dev equivalent of library(). It instantly reflects code changes without installing.

Q8

What does the @export roxygen tag do?

  1. Marks the function for inclusion in the next R release
  2. Saves the function as a .rda file
  3. Makes the function callable from outside the package after library(yourpkg)
  4. Sends a copy of the function to CRAN

Answer: 3) Without @export, a function stays internal — usable inside the package but not after loading it (unless called via yourpkg:::fn()).

Q9

Roxygen2 documentation comments must start with which prefix?

  1. #
  2. #'
  3. ##
  4. //

Answer: 2) #' tells devtools::document() to parse the line. Just # is a code comment and ignored.

Q10

In a data package, where do raw data scripts go vs the cleaned data they produce?

  1. Both go in R/
  2. Scripts in data-raw/; cleaned .rda files in data/
  3. Scripts in data/; cleaned files in data-raw/
  4. Both go in tests/

Answer: 2) usethis::use_data_raw() sets up the script folder; the script ends with usethis::use_data(your_object) to save the cleaned .rda into data/.