Thursday, February 7, 2013

My R-Package Development Cheat Sheet

In case you have no experience in writing an R-package yourself  but would like to start developing one right away, this post might be helpful.

I'm about to finish my first own (serious) R-package these days (more on the package itself later). While writing my package, I collected a handful of commands and notes etc. that proofed to be helpful, and saved them in a R-script. I usually had that script open in one window when writing/testing some parts of my package in RStudio. I figured that it might help someone in a similar situation. Note, though, that this little code collection has no ambitions whatsoever to be anything like a complete guide to develop your own R-package. Having that said, here it is (you can also download it from my github repo):


#######################################################
# This was contributed by giventhedata.blogspot.com   #
#######################################################
 
 
# I. Very useful tools when writing a R-package:
#------------------------------------------------
install.packages("devtools", "roxygen2")
library(devtools)
library(roxygen2)
 
 
# II. getting started
#--------------------
 
# assuming your package is to be called 'MyRpackage' and
# all the scripts that contain functions that should be part
# of your package are in your current working directory and
# and there are no functions loaded in the workspace of your 
# current R-session...
 
# source all scripts:
myscripts <- c("script1.R", "script2.R", "script3.R") #...
 
for (i in myscripts) source(i)
 
 
# get all the names of the functions in the workspace
fs <- c(lsf.str()) 
 
# create package skeleton:
package.skeleton("MyRpackage", fs)
 
 
# III. While working on your package...
#--------------------------------------
 
# renew documentation
MyRpackage_package <- as.package("MyRpackage")
document(MyRpackage_package)
 
# build and check
system("R CMD build MyRpackage")
system("R CMD check MyRpackage")
system("R CMD Rd2pdf MyRpackage") # update/check manual.pdf (while working on the documentation)
 
# install your package from the local directory after successfully building it
install.packages(paste(getwd(),"/MyRpackage_0.1.tar.gz",sep=""), repos=NULL, type="source")
 
# load it for tests
library(MyRpackage)
 
# unload old version of package after changes (in order to install new built)
detach(package:MyRpackage, unload=TRUE)
 
 
#Note:
# For internal functions: delete Rd-file, leave out export-command in Namespace
Syntax highlighting created by Pretty R at inside-R.org

3 comments:

  1. You can make this line simpler:
    install.packages(paste(getwd(),"/MyRpackage_0.1.tar.gz",sep=""), repos=NULL, type="source")

    install.packages("./MyRpackage_0.1.tar.gz", repos=NULL, type="source")

    ReplyDelete
  2. Why load up devtools and then hardly use it? Create a new package with create("foo"), edit code in the .R folder, then load it with load_all("foo"), edit, load_all, edit load_all. That's my development loop these days. No need to run an external build or detach. Release time, use devtool's assorted build and check tools, and then its function to push to CRAN.

    ReplyDelete
    Replies
    1. Thanks for your feedback. As I've pointed out in this post, this is not at all supposed to be a complete guide... Obviously there are things to add here and alternative ways of doing it. Of course, if you start from scratch (working on different functions/scripts etc.) devtool's load_all is more comfortable to frequently update. However, the code above is meant for the situation where you already wrote all the functions in different scripts and want to bundle and document them in a package (and have no experience with writing a package). I guess I havn't pointed that out clearly and my use of the word "Development" is not very wise in this context. Anyway, thanks for adding this!

      ps: I'm not sure what you meant with "create(...)". ??create is rather puzzling. Did you mean create.project {ProjectTemplate}?

      Delete