Ranae Dietzel and Andee Kaplan
R
package!Content for today is from Hadley’s book and Eric’s ISU Graphics Group talk.
R
scriptsdplyr
requires magrittr
for the pipe operator, etc.)install.packages("devtools")
)install.packages("roxygen2")
)Some other potentially useful packages include:
install.packages("testthat")
) - Allows the writing of unit tests for individual functionsinstall.packages("knitr")
) and rmarkdown (install.packages("rmarkdown")
) for building vignettesWe can check:
library(devtools)
has_devel()
## '/usr/local/Cellar/r/3.3.2/R.framework/Resources/bin/R' --no-site-file \
## --no-environ --no-save --no-restore --quiet CMD SHLIB foo.c
##
## [1] TRUE
From http://r-pkgs.had.co.nz/package.html:
R
This is the overall descriptor of the package from which metadata is obtained. Fields include:
And more…
Defines the imported functions (dependencies), and exported functions
What are exported functions?
::
vs :::
Contains the R
code for the package. This can be done in a few ways:
The third is recommended by Hadley and is generally the approach that should be used, although CRAN gatekeepers prefer the second option at least compared with the first.
R
Documentation) which define the help files we know and loveroxygen2
It allows automatic generation of:
It creates the NAMESPACE and Rd files automatically for us. We just have to add special comments above each function.
Let’s take a look…
There will be three general routines we use devtools
for: checking (verifying that the package is in the proper format), installing (installing it on our own computer), and building (building it for use by other people)
# With your working directory set to the root package folder
devtools::check()
devtools::install()
devtools::build()
Sometimes we need to depend on other packages. To do so, we use the @import or @importFrom directive in roxygen. Let’s look at an example.
Add a new function of your choosing to the package. The function must:
ggplot2
package (@import ggplot2
)
When you have created this function, check and install your package to make sure that it works!
In the real world, we’re usually developing packages with other developers. Collaborative coding tools like GitHub make this much easier.
As a side benefit, putting your package on GitHub makes installing the package much easier as well:
devtools::install_github("andeek/agron590demo")