There's not much to a package. Here are the steps to making one:
-
Clone this repo.
-
Add R functions and their
roxygen2-style doccumentation to theRsubdirectory. Themansubdirectory has a README.md with an example ofroxygen2syntax that's adequate for most simple functions. -
Update the
DESCRIPTIONfile with the package name, descriptions, author(s), today's date, and any neededDepends/Suggests/Importsor other comments -
Go to the package root directory (the one with the
DESCRIPTIONfile) and run Rscriptroxygen2::roxygenize(".")or whatever the RStudio equivalent is. -
When
roxygenizeruns, it should generate aNAMESPACEfile in the root directory based on instructions in the R-file comments and it should also generate documentation in themansubdirectory based on the same comments. -
Update
tests/testhat.Rwith the name of the package so thattestthatloads the package before trying to test it. Writing and running tests is described in the R packages book,If you scroll down there are some
Each individual test should run a small function and check that it has the correct output.
-
Then you can go to the directory above the package root and run:
R CMD INSTALL <package-root>
-
Use the
checkmatepackage for checking inputs, especially on user-facing functions. This package has a lovely vignette here: https://cran.r-project.org/web/packages/checkmate/vignettes/checkmate.html -
Write unit tests when they add value, it's more often than you would think. They can add value when:
- a function might fail silently and produce a cascade of failures that will require time to trace back to the failure point to debug.
- a function might involve formulas that could easily be modified down the line to produce slightly incorrect results. Even checking function output for a single value can save a lot of time.
- you discovered a bug in a function and you'd like to not have to trace that same bug again. This is called a "regression test".
-
Don't add dependencies unless you need them. Many packages have simple alternatives in base R so check with the group.
-
Use the
styleRpackage to format your code. It can be run viausethis::use_tidy_style()