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.
Code
Create a directory containing a package skeletondevtools::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
devtools::use_package("dplyr")
devtools::use_package("ggplot2", "suggests")
Data
For data I followed his recommendations in r-pkgs/data.rmddevtools::use_data(mtcars)
devtools::use_data_raw()
# Creates a data-raw/ folder and add it to .RbuildignoreTests
Example of testing for the devtools packageBash command to build and check a package
Bash 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
R CMD Rd2pdf tradeflows --no-cleanThe --no-clean option keeps among other files a temporary Latex which can be inspected under:
packagename.Rcheck/packagename-manual.tex
Alternatively the build and check procedure can be run in RStudio as explained above.
No comments:
Post a Comment