Writing Functions & Packages

A package is R's unit for organising and sharing reusable code — bundling well-written, documented functions so you (and others) can install and reuse them across projects.

Learn Writing Functions & Packages in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll write robust, validated functions, document them with roxygen comments, compose small functions into bigger tools, and understand the workflow that turns them into a sharable package.

What You'll Learn in This Lesson

1️⃣ Robust, Documented Functions

A production-quality function validates its inputs (failing clearly with stop() ), stays vectorized, and carries roxygen #' documentation describing its parameters and return value.

2️⃣ Composing Small Functions

Big tools are built from small ones. Each function does one job; you combine them. Returning a named list lets a function hand back several results at once.

3️⃣ From Functions to a Package

Packages are how R shares code. The usethis + devtools workflow scaffolds, documents, and checks one. And using any package is always the same two steps: install.packages() once, library() each session.

Your turn. Fill in the # TODO blank, run it, and compare with the expected output.

Write both functions from the outline, run them, and check against the example output. Two small, validated, reusable functions are the seed of any package.

📋 Quick Reference — Functions & Packages

Practice quiz

Which function makes a function fail loudly on bad input?

  • warn()
  • stop()
  • print()
  • cat()

Answer: stop(). stop() halts execution with a clear error message for invalid input.

What does install.packages("dplyr") do?

  • Loads it into the session
  • Downloads and installs the package once
  • Documents it
  • Deletes it

Answer: Downloads and installs the package once. install.packages() downloads and installs a package; you do it once.

What does library(dplyr) do?

  • Installs the package
  • Uninstalls it
  • Loads an installed package into the session
  • Publishes it to CRAN

Answer: Loads an installed package into the session. library() loads an already-installed package each session so its functions are available.

What are the #' comment lines above a function for?

  • They are ignored entirely
  • They speed up the function
  • They are normal comments
  • roxygen documentation

Answer: roxygen documentation. #' lines are roxygen2 comments that generate the function's help page.

Which function scaffolds a new package skeleton?

  • usethis::create_package()
  • make_package()
  • new.package()
  • devtools::build()

Answer: usethis::create_package(). usethis::create_package() sets up the package folder structure.

Which command builds help files from #' comments?

  • library()
  • devtools::document()
  • install.packages()
  • use_r()

Answer: devtools::document(). devtools::document() reads roxygen comments and generates the help files.

What does the @param tag describe in a roxygen comment?

  • The return value
  • The package name
  • A function argument
  • The author

Answer: A function argument. @param documents one of the function's arguments.

What causes 'could not find function' for a package's function?

  • The package isn't loaded with library()
  • Too many arguments
  • A typo in numbers
  • The function returns NULL

Answer: The package isn't loaded with library(). You must load the installed package with library(pkg) before using its functions.

Why return a named list from a function?

  • Lists are required
  • To bundle several results in one object
  • To make it run faster
  • Lists print prettier

Answer: To bundle several results in one object. A named list lets a function hand back several results at once.

How can others install your package straight from GitHub?

  • library(github)
  • install.packages(github)
  • use_github()
  • devtools::install_github("user/repo")

Answer: devtools::install_github("user/repo"). devtools::install_github() installs a package directly from a GitHub repo.