Quantitative finance collector
C++ Matlab VBA/Excel Java Mathematica R/Splus Net Code Site Other
Jun 4

How to Create an R Package in Windows

Posted by abiao at 13:53 | Code » R/Splus | Comments(5) | Reads(21939)
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: ,
Hi, just a small question about R and Splus, since I begin to learn one of them apart of Matlab, and really in rookie's level, what's the major diff between them and which is frequently used in the industry now?

no much difference between R and S+ based on my experience, the major diff is R is an open source while S+ is commercial, so there r lots of packages for R. R is frequently used in the industry.
thx dude...
I Want to building package R for windows, I did steps 1.6 but When I was doing R CMD INSTALL build I got an error , Would you please help me?
1.Install Rtools, Miktext
2. Set PATH enviroment variables
3. setwd("C:/Users/nahid/My Document")
4. package.skeleton(name="Confidence Interval",code_files="E:/R/confintv.R")
This function created the directory tree for the package containing :
5. Then in “cmd prompt” I changed directory to where my package is,
>cd C:\Users\nahid\My Document
6. Then I typped this command
> R CMD build Confidence Interval
“Cannot change to directory ‘Confidence’ “
> R CMD INSTALL build Confidence Interval
Warning: Invalid package ‘build’
Warning: Invalid package ‘Confidence’
Warning: Invalid package ‘Interval’
ERROR: no package specified
Really cool and accurate, all things are in the right place.
nova legacy hack
Pages: 1/1 First page 1 Final page
Add a comment
Enable HTML
Enable UBB
Enable Emots
Nickname   Password   Optional
Site URI   Email   [Register]