R Package
Structure
Learn how R packages are organized, what each file and folder does, and why understanding package structure makes you a better R programmer.
What is an R Package?
An R package is a standardized collection of R code, data, documentation, and tests bundled together into a single, shareable unit. Think of it like a toolbox: each tool (function) has a specific purpose, and they are all organized in labeled compartments.
Why Packages Matter
The Big Picture: Package Anatomy
The DESCRIPTION File
The DESCRIPTION file is the identity card of your package. It tells R (and the world) everything about who made it, what it does, and what it depends on.
The R/ Directory
The R/ folder is where all your R source code lives. Every .R file inside this folder contains the functions that make your package useful.
The NAMESPACE File
The NAMESPACE file controls what goes in and out of your package. It determines which of your functions users can access, and which functions you borrow from other packages.
man/ : Documentation
The man/ directory holds .Rd files: special documentation files that produce the help pages you see when you type ?function_name in R. These are generated from roxygen2 comments in your R code.
tests/ : Automated Testing
The tests/ directory holds automated tests that verify your functions work correctly. The standard tool is the testthat package.
Imports vs Suggests
| Imports | Suggests | |
|---|---|---|
| When installed? | Always (auto-installed) | Only if user wants them |
| Used for? | Core functionality | Tests, vignettes, examples |
| If missing? | Package fails to load | Package still works |
| Example | dplyr, ggplot2 | testthat, knitr |
Scripts vs Packages
| R Script | R Package | |
|---|---|---|
| Structure | Single .R file | Organized directory tree |
| Loading | source("file.R") | library(pkg) |
| Documentation | Comments (informal) | Help pages (formal) |
| Testing | Manual | Automated (testthat) |
| Sharing | Email/copy files | install.packages() |
How to Create a Package
Example: Minimal Package
Let us build a tiny package called greetR that has one function: say_hello().
Example: Adding Dependencies
Now we want greetR to use stringr for more advanced string operations. We need to declare it properly.
Example: The Full Workflow
Here is the complete sequence of commands to build, document, test, and check the greetR package from start to finish.
Common Mistakes
Question 1 of 5
Which file serves as the "identity card" of an R package, containing its name, author, version, and dependencies?
Question 2 of 5
Where should you place your R function files inside a package?
Question 3 of 5
A package uses testthat only for running tests, not in its core functions. Where should testthat be listed in DESCRIPTION?
Question 4 of 5
What is the correct way to call a function from another package inside your own package code?
Question 5 of 5
Which roxygen2 tag must you include above a function definition to make it accessible to users of your package?