Develop R Packages Using "roxygen2"
Posted on Jun 27, 2012 in Programming
Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives.
You have to install the R package roxygen2 first.
install.packages('roxygen2')
Roxygenize the package for compiling.
library(roxygen2)
roxygenize(path_to_package)
The following are some roxygen2 tags that I use frequently for writing R packages.
-
Some R example code can take a long time to run. To prevent the illustration code from running when checking the package, you can surround the example code with
\dontrun{}. The following is such an example.#' @examples #' \dontrun{ #' rnorm(100000) #' }However, be careful that it is
@examplesnot@example. If you accidentally use@example, then you will get errors likefile cannot be open (or doesn't exist) ...when you check the R package. -
By default roxygen2 does not export a function. To export a function so that package users can use it, add tag
@exportbefore the definition of the function. -
Use the tag
@titleto specify the title of a help document. -
Use the tag
@descriptionto start the description part of a help document. -
To import a package, use the tag
@import pkg_name. To importfun1andfun2from a package, use the tag@importFrom pkg_name fun1 fun2. -
By default roxygen2 creates a Rd document for each exported function with the same name as the function name. You can use the tag
@rdnameto override the name of the Rd document. -
Roxygen parses comments that start with
#'. Comment start with#but not#'won't be parsed. You can take advantage of this to put comment that do not need to parsed into lines starting with#but not#'.
Compile and Install R Packages
-
Compile and install package on Windows.
R CMD INSTALL --build package_dirNotice that
INSTALLmust be in upper case. -
The following LaTex packages
texlive,texinfoandtexlive-fonts-extra(on Linux) are need to compile R packages if you want to generate and check PDF manuals.wajig install texlive texinfo texlive-fonts-extraIf you do not want (or have permissions) to install them, you can compile R packages without generating PDF manuals.
R CMD build --no-manual ... R CMD check --no-manual ...For more details on R package build options, please refer to the post Customizing Package Build Options.