RcppArmadillo Test Notes – A Step-by-Step Tutorial to Create Your Own R Package Based on RcppArmadillo


Test environment:
R version 3.2.2
Rtools 33
Windows 7 64-bit

  1. Install the packages “Rcpp”, “RcppArmadillo” and “inline” for R.

  2. 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”.

  3. 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:

  1. Use the following R code:
    library("RcppArmadillo");
    RcppArmadillo.package.skeleton(name="testRcppArPackage")

    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.

  2. 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
    “\examples{
    ~~ simple examples of the most important functions ~~
    }”
    and delete the content line inside the block and save, so it becomes
    “\examples{
    }”. 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.

  3. 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:

    require("Rcpp");
    compileAttributes("C:/Users/hp/Documents/testRcppArPackage");

    See https://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-package.pdf for details.

  4. 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

    library("testRcppArPackage");
    

    Then you can call your function in R by the same function name defined in the C/C++ source code.

  5. 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)

Links:
https://github.com/petewerner/misc/wiki/RcppArmadillo-cheatsheet
http://arma.sourceforge.net/docs.html#syntax
Seamless R and C++ Integration with Rcpp (Use R!)
http://web.mit.edu/insong/www/pdf/rpackage_instructions.pdf

Advertisements
This entry was posted in programming and tagged , , . Bookmark the permalink.

5 Responses to RcppArmadillo Test Notes – A Step-by-Step Tutorial to Create Your Own R Package Based on RcppArmadillo

  1. Hi,

    I am trying to create a package with RcppArmadillo using RcppArmadillo.package.skeleton(name=”testRcppArPackage”)

    but I met an error when I run the “R CMD check testRcppArPackage” in windows CMD

    RcppExports.cpp:4:27: fatal error: RcppArmadillo.h: No such file or directory
    #include
    ^

    • Bruce Zhou says:

      It’s a common compilation problem but there could be various causes. For example, have you installed Rtools [https://cran.r-project.org/bin/windows/Rtools/] on Windows? You may try Step 3. (3. Simple test code:) first, see if you can run these code in R console without a problem.

  2. Thank you for your reply. I am becoming crazy with this error.

    I installed everything following the blog :
    http://www.r-bloggers.com/installing-rcpp-on-windows-7-for-r-and-c-integration/

    Here, I attached the output of 00install.out
    I used the simplest code to start given by the Rcpp.package.skeleton(“foobar”)
    but it seems that I have not the link with Rcpp.h

    I wrote:
    system.file(“lib”, “Rcpp.h”, package=”Rcpp”)
    and the output was blank

    * installing *source* package ‘foobar’ …
    ** libs

    *** arch – i386
    c:/Rtools/mingw_32/bin/g++ -I”C:/PROGRA~1/R/R-33~1.0/include” -DNDEBUG -I”\\icnas4.cc.ic.ac.uk/scacciat/R/win-library/3.3/Rcpp/include” -I”d:/Compiler/gcc-4.9.3/local330/include” -O2 -Wall -mtune=core2 -c RcppExports.cpp -o RcppExports.o
    RcppExports.cpp:4:18: fatal error: Rcpp.h: No such file or directory
    #include
    ^
    compilation terminated.
    make: *** [RcppExports.o] Error 1
    Warning: running command ‘make -f “C:/PROGRA~1/R/R-33~1.0/etc/i386/Makeconf” -f “C:/PROGRA~1/R/R-33~1.0/share/make/winshlib.mk” SHLIB_LDFLAGS=’$(SHLIB_CXXLDFLAGS)’ SHLIB_LD=’$(SHLIB_CXXLD)’ SHLIB=”foobar.dll” OBJECTS=”RcppExports.o rcpp_hello_world.o”‘ had status 2
    ERROR: compilation failed for package ‘foobar’
    * removing ‘C:/Users/scacciat/Desktop/foobar.Rcheck/foobar’

    • Bruce Zhou says:

      I think the r-bloggers post is somehow outdated. system.file(“lib”, “Rcpp.h”, package=”Rcpp”) also gives me a blank output so I guess it doesn’t matter. The “Rcpp.h” file in my installation is located at “D:\Program Files\R\R-3.2.2\library\Rcpp\include\Rcpp.h”, I think yours should be “\\icnas4.cc.ic.ac.uk/scacciat/R/win-library/3.3/Rcpp/include/Rcpp.h”, and please check that path, is that a local path on your computer?

  3. Thank you sooooooooooooo much.
    The problem was the access to the Rcpp library. I usually installed the R library in a server but Rcmd was not able to link the Rcpp.h file.

    So I re-installed R and Rstudio in other address and now everything works.

    Thank you again

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s