R package building automation

[This article was first published on StaTEAstics., and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Title: R package building automation

Inspired by the post at http://giventhedata.blogspot.tw/2013/02/my-r-package-development-cheat-sheet.html. I have decided to publish my cheat script for package development as well.

Building package used to be a nightmare, filling in all those Rdfiles manually can cause some serious brain damage. Thanks to the roxygen2 team and knitr, building a package with nice documentation is merely running a script like what I have below. Just modify and change pkgName to the name of your package then your package should be ready in 5 minute.

<br />## Build the package<br />## ---------------------------------------------------------------------<br /><br />## Remove the folder if it exists<br />if(file.exists("./pkgName"))<br />    unlink("pkgName", recursive = TRUE)<br /><br />## Build the package<br />package.skeleton("pkgName", code_files = paste("./Codes/",<br />                           dir("./Codes/", pattern = "\\.R$"), sep = ""),<br />                 force = TRUE)<br /><br />## Include the data<br />dir.create("pkgName/data")<br />file.copy(from = "./FAOcountryProfile.RData",<br />          to = "pkgName/data/", overwrite = TRUE)<br />file.copy(from = "./FAOregionProfile.RData",<br />          to = "pkgName/data/", overwrite = TRUE)<br />file.copy(from = "./FAOmetaTable.RData",<br />          to = "pkgName/data/", overwrite = TRUE)<br />file.copy(from = "./DESCRIPTION", to = "pkgName/",<br />          overwrite = TRUE)<br /><br />## Include Demo<br />dir.create("pkgName/demo")<br />file.copy(from = "./pkgNamedemo.R",<br />          to = "pkgName/demo/", overwrite = TRUE)<br />cat("pkgNamedemo Demonstration for the pkgName package\n",<br />    file = "pkgName/demo/00Index")<br /><br />## Use roxygen to build the documentation<br />library(roxygen2)<br />roxygenize("pkgName")<br /><br />## Include vignette<br />dir.create("./pkgName/inst/doc/")<br />file.copy(from = "../Documentation/pkgName/pkgName.pdf",<br />          to = "./pkgName/inst/doc/", overwrite = TRUE)<br /><br />## Create the vignette hack from (http://yihui.name/knitr/demo/vignette/)<br />## This is not required for R 3.0.0<br />cat("%\\VignetteIndexEntry{General Manual}\n\\documentclass{article}\n\\begin{document}\n\\end{document}", file = "./pkgName/inst/doc/pkgName.Rnw")<br /><br />## Build and check the package<br />system("R CMD INSTALL --build pkgName")<br />system("R CMD build pkgName")<br />system("Rcmd check pkgName")<br />system("Rcmd check --as-cran pkgName")<br /><br />

PS: This is my first post using Rmd, the highlight for R is defintely much better than plain text. However, I think some twick is required for my blog theme.

To leave a comment for the author, please follow the link and comment on their blog: StaTEAstics..

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)