How to Create an R Package in Windows

[This article was first published on Quantitative Finance Collector, 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.

There are many reasons to create an R package, such as codes protection, convenient usage, etc. However, creating an R package in Unix is not hard, it IS in Windows, as R is designed in a Unix environment which includes a set of compilers, programming utilities, and text-formatting routines while Windows lacks those. I used to build an R library in Unix and thought it was relatively as the same convenient as in Windows, but it turned out I had to spend several hours on it. Silly me.

Below are the selected steps to create an R package in Windows I summarize:  
1, you have to download Rtools at http://www.murdoch-sutherland.com/Rtools/.  

2, depending on your needs, you may download and install LaTex, Microsoft HTML Help Workshop and the Inno Setup installer, available at http://www.miktex.org, http://msdn.microsoft.com/en-us/library/ms669985.aspx, and http://www.jrsoftware.org/, respectively. For example, Microsoft HTML Help Workshop is for making HTML Help documents, LaTex is for a nicer outlook, especially when your documents have math equations.

3, double click Rtools.exe to install it and its accompanying tools: minGW, perl. Rtools automatically recognizes the paths of those relevant softwares and add them to the environment variables of your computer.
Open in new window

4, check and change your PATH “environment variables” of your computer if incorrect. To set the path, right click on the “My Computer” icon on your desktop, choose properties and click on the “advanced” tab, click the environmental variables button, click on the path variable and select the edit button. Check carefully whether all the paths are correct, special attention needs to be given to the path of R software.

5, double check whether all tools are installed correctly by opening a “command prompt” window and typing the following commands: R; gcc –help; perl –help; Tex –help, respectively. You should see a list of options, otherwise re-install or check the path if you see “… is not a recognized as an internal or external command …”

6, start R and run the command package.skeleton( ) with suitable arguments, read the help http://stat.ethz.ch/R-manual/R-patched/library/utils/html/package.skeleton.html for detail. For instance, I write a sample code MonteCarloPi.R to estimate PI by Monte Carlo simulation, we use package.skeleton(name = “MonteCarloPi”, code_files = “MonteCarloPi.R”), where MonteCarloPi.R includes function we want to add to the package MonteCarloPi. This function creates the directory tree for the package containing:
DESCRIPTION <<man <<< for documentation
R <<< for R function definitions
src <<< for low-level source code (this is optional for other programming files, such as your c++ codes)
data <<< for package datasets (this is optional)
Open in new window

7, remove the Read-and-delete-me file and modify other files in the above directory with notepad or other tool, for instance, to add the author information and Help explanation.

8, open a “command prompt” window, change the directory to where your package is, type the command “R CMD build MonteCarloPi” to build the package, this will generate a file called MonteCarloPi_1.0.tar.gz. You must run the command in a “command prompt” window instead of in R, otherwise you are expected to see “Error: unexpected symbol in “R CMD””.

9, test the package by typing “R CMD check MonteCarloPi_1.0.tar.gz”, go back to step 8 if you see errors.

10, now you are able to install the package typing “R CMD INSTALL MonteCarloPi_1.0.tar.gz”. Congratulations if you see “…done…” and you will notice there is one more sub-folder “MonteCarloPi” in your R library folder.

#MonteCarloPi.R — PI estimation by Monte Carlo simulation

MonteCarloPi <- function(nsim){
  x <- 2*runif(nsim)-1
  y <- 2*runif(nsim)-1
  inx <- which((x^2+y^2)<=1)
  return ((length(inx)/nsim)*4)
}



Tags – r , package
Read the full post at How to Create an R Package in Windows.

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

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)