A package can be seen as a coherent group of functions available for future projects. Building your own package enables you to reuse and share your statistical procedures. Function parameters and examples can be documented with Roxygen to facilitate digging back into the code later on. I created my second package based on instructions from Hadley. My package structure is composed of the following folders:
- R/ contains R code
- test/ contains tests
- inst/ contains files that will be exported with the package
- docs/ contains .Rmd documents illustrating code development steps and data analysis.
- data/ contains data sets exported with the package
- data-raw/ contains raw dataset and R code to extract raw data from disk and from web resources.
CodeCreate a directory containing a package skeleton
devtools::create("packagename")RStudio has a menu build / configure build tools where devtools package functions and document generation can be linked to keyboard shortcuts:
- document CTRL + SHIFT + D
- build and reload CTRL + SHIFT + B
devtools::load_all()or Cmd + Shift + L, reloads all code in the package.
Add packages to the list of required packages
DataFor data I followed his recommendations in r-pkgs/data.rmd
devtools::use_data_raw()# Creates a data-raw/ folder and add it to .Rbuildignore
TestsExample of testing for the devtools package
Bash command to build and check a packageBash command to build a package directory:
R CMD build packagenameBash command to check a package tarball:
An error log (good luck for understanding it) is visible at:
R CMD check packagename_version.tar.gz
packagename.Rcheck/00check.logGenerate the documentation and check for documentation specific errors
--no-clean option keeps among other files a temporary Latex which can be inspected under:
Alternatively the build and check procedure can be run in RStudio as explained above.