Skip to Content
R Packages, 2nd Edition
book

R Packages, 2nd Edition

by Hadley Wickham, Jennifer Bryan
June 2023
Intermediate to advanced content levelIntermediate to advanced
381 pages
9h 2m
English
O'Reilly Media, Inc.
Content preview from R Packages, 2nd Edition

Chapter 6. R Code

The first principle of making a package is that all R code goes in the R/ directory. In this chapter, you’ll learn about organizing your functions into files, maintaining a consistent style, and recognizing the stricter requirements for functions in a package (versus in a script). We’ll also remind you of the fundamental workflows for test-driving and formally checking an in-development package: load_all(), test(), and check().

Organize Functions Into Files

The only hard rule is that your package must store its function definitions in R scripts, i.e., files with extension .R, that live in the R/ directory.1 However, a few more conventions can make the source code of your package easier to navigate and relieve you of re-answering “How should I name this?” each time you create a new file. The Tidyverse Style Guide offers some general advice about filenames and also advice that specifically applies to files in a package. We expand on this here.

The filename should be meaningful and convey which functions are defined within. While you’re free to arrange functions into files as you wish, the two extremes are bad: don’t put all functions into one file and don’t put each function into its own separate file. This advice should inform your general policy, but there are exceptions to every rule. If a specific function is very large or has lots of documentation, it can make sense to give it its own file, named after the function. More often, a single .R file will contain ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

R Packages

R Packages

Hadley Wickham
R Cookbook, 2nd Edition

R Cookbook, 2nd Edition

JD Long, Paul Teetor
Advanced R

Advanced R

Hadley Wickham

Publisher Resources

ISBN: 9781098134938Errata Page