R version 3.2.2
Windows 7 64-bit
- Install the packages “Rcpp”, “RcppArmadillo” and “inline” for R.
Download and install Rtools at https://cran.r-project.org/bin/windows/Rtools/, which is needed for building packages for R under Microsoft Windows. When installing, check the option “Edit the System PATH”.
Simple test code:
#See https://github.com/petewerner/misc/wiki/RcppArmadillo-cheatsheet for more examples. library('Rcpp') library('inline') rcpp_inc <- ' using namespace Rcpp; using namespace arma; ' m1 <- matrix(1:16, nr=4) m2 <- matrix(17:32, nr=4) v1 <- 1:10 v2 <- 11:20 src <- ' mat m1 = as<mat>(m1in); mat m2 = as<mat>(m2in); mat out = join_cols(m1, m2); return(wrap(out)); ' fn <- cxxfunction(signature(m1in="numeric", m2in="numeric"), src, plugin='RcppArmadillo', rcpp_inc) res <- fn(m1, m2) test <- rbind(m1, m2) all.equal(test, res)
To write an R package based on RcppArmadillo:
- Use the following R code:
You will get a folder “C:\Users\hp\Documents\testRcppArPackage” (here “testRcppArPackage” is your package name, you can change it to something more meaningful) in your R working directory, which contains all the basic files you need to build an R package.
Install the example package:
In windows CMD, “cd” to your R working directory, which contains the folder “testRcppArPackage”, and run the following commands
"D:\Program Files\R\R-3.2.2\bin\x64\Rcmd" check testRcppArPackage "D:\Program Files\R\R-3.2.2\bin\x64\Rcmd" INSTALL testRcppArPackage
where “D:\Program Files\R\R-3.2.2\bin\x64\Rcmd” is the path of your R installation.
If you get the error that “Error: unexpected symbol in ’’‘~~simple examples’’”, open the file “C:\Users\hp\Documents\testRcppArPackage\man\testRcppArPackage-package.Rd” with your text editor, find the block
~~ simple examples of the most important functions ~~
and delete the content line inside the block and save, so it becomes
}”. Later on you may add example code in that block.
See http://howtomakeanrpackage.pbworks.com/f/How_To_Make_An_R_Package-v1.14-01-11-10.pdf Page 32 for more information.
Add your own functions in your package:
Just add your new functions in the file “C:\Users\hp\Documents\testRcppArPackage\src\rcpparma_hello_world.cpp”.
After that, you need to re-generate the “RcppExports.cpp” and “RcppExports.R” files in order to expose your own functions to R (so that you can call your defined functions in R after you load your package). To do this, use the following R code:
Finally, re-build and re-install your package using the same commands in step 5. If you encounter any error during the building process, you can check the file “C:\Users\hp\Documents\testRcppArPackage.Rcheck\00install.out” to find the details of the errors. You can test your package by loading it using
Then you can call your function in R by the same function name defined in the C/C++ source code.
In addition, use the command
"D:\Program Files\R\R-3.2.2\bin\x64\Rcmd" build testRcppArPackage
to create a packed package file “testRcppArPackage_1.0.tar.gz” for distributing. You can install your package from this “.tar.gz” file locally using the following R command:
install.packages("testRcppArPackage_1.0.tar.gz", repos = NULL)
However, the above package contains source code only and requires users have compilers (Rtools) to install. To build a binary package that can install directly, use the command
"D:\Program Files\R\R-3.2.2\bin\x64\Rcmd" --build testRcppArPackage_1.0.tar.gz
And to build the binary package for both (Windows) 32-bit and 62-bit,
"D:\Program Files\R\R-3.2.2\bin\x64\Rcmd" --build --compile-both testRcppArPackage_1.0.tar.gz
You’ll get a new binary package file “testRcppArPackage_1.0.zip”, which can be installed directly (for Windows) without compiling. To install,
install.packages("testRcppArPackage_1.0.zip", repos = NULL)
Seamless R and C++ Integration with Rcpp (Use R!)