Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

Romain Francois presented an Rcpp solution on his blog to an old r-wiki optimisation challenge which I had also presented R solutions for previously on my blog.

The Rcpp package provides a method for integrating R and C++. This allows for faster execution of an R project by recoding the slower R parts into C+ and thus providing potential performance enhancements.

The two main attractions, for me personally, of the Rcpp package are (a) help me to re-learn C++ because I’ve not used it in over 10 years and (b) write R packages which interface to existing C++ libraries for the purposes of natural language processing or some other library which does something cool.

The objective

Install Rcpp on Windows 7 Pro x64 to test out Romain’s Rccp solution to the old r-wiki optimisation challenge

The Problem

I had zero problems installing and running Rcpp code on Ubuntu Linux 11.10 but just could not get the same to work on Windows 7 Pro x64, often getting the following warning and errors:

Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created!

cygwin warning:
MS-DOS style path detected: C:/PROGRA~1/R/R-214~1.0/etc/x64/Makeconf
Preferred POSIX equivalent is: /cygdrive/c/PROGRA~1/R/R-214~1.0/etc/x64/Makeconf
CYGWIN environment variable option “nodosfilewarning” turns off this warning.
Consult the user’s guide for more details about POSIX paths:

http://cygwin.com/cygwin-ug-net/using.html#using-pathnames

x86_64-w64-mingw32-g++.exe: C:/Program: No such file or directory
x86_64-w64-mingw32-g++.exe: Files/R/R-2.14.0/library/Rcpp/lib/x64/libRcpp.a: No such file or directory

The Solution

It turns out that there’s two issues here:

(1) the cygwin warning which we can ignore as it has nothing to do with Rcpp and is caused by using the 64bit version of MinGW from Rtools when adding it to our PATH variable (easily solved by choosing the 32bit version instead, shown below in step 5 part viii)

(2) the actual Rcpp issue itself which happened because I had originally installed R on a path with a space in it. In fact (and this is something I didn’t previously know) it’s officially recommended that one does not install R on Windows with a path containing a space (see rw-FAQ 2.2).

So armed with this knowledge, here is how I got Rcpp working from beginning to end (a lot of the sub-steps below are blindingly obvious but I included them anyway for the sake of completeness in case other people with similar problems come across this post):

1. Install R on a path that does not contain a space.
(iii) press Run when the security warning appears
(iv) press Yes when the User Account Control message box appears
(v) Choose your language (I choose English)
(vi) when prompted for where to install R, choose a location without a space. I choose “C:\R\R-2.14.0″
(vii) click Next on all following screens to finish installation (I left the defaults unaltered).
.
2. Install Rtools for components necessary to buld R (this has the tool chain require for C++ code compilation)
(i) Go to this webpage: http://www.murdoch-sutherland.com/Rtools/
(ii) In the downloads section, choose a compatible version for your version of R. I’m using R-2.14.0 and thus chose Rtools214.exe
(iii) press Run when the security warning appears
(iv) press Yes when the User Account Control message box appears
(v) choose your language (I choose English)
(vi) choose where to install (I chose “C:\R\Rtools”)
(vii) click Next on all following screens to finish installation (I left the defaults unaltered).
.
3. Download batchfiles to always point to the latest version of R on your system when running R from the command line (I have found that this saves a lot of time in the long run)
(i) go to: http://cran.r-project.org/contrib/extra/batchfiles/
(iv) click Browse and choose where you want to put the extracted folder (I chose “C:\R\”)
.
4. Download Redmond Path Utility to alter PATH variables in a very user friendly fashion:
(iv) press Run when the security warning appears
(iv) press Yes when the User Account Control message box appears
(v) press Next
(vi) press Decline (unless you want the annoying advertising extra).
(vii) click open
(viii) An explorer window will open with RedmondPath.zip
(ix) click Extract All.
(x) click Browse and choose where you want to put the extracted folder (I chose “C:\R\”)
.
5. Edit PATH variable to allow system wide access to the current version of R on the computer and components of Rtools
(i) double click the Redmond Path Utility from step 4 above (mine is in: “C:\R\RedmondPath\Redmond Path.exe”)
(ii) click Yes when the User Account Control message box appears
(iii) click the green “+” icon in the top left corner so we can add elements to the PATH variable
(iv)  In the window which pops open, navigate to “C:\R\batchfiles_0.6-6″ and click OK
(v) click the green “+” icon in the top left corner so we can add elements to the PATH variable
(vi)  In the window which pops open, navigate to “C:\R\Rtools\bin” and click OK
(vii) click the green “+” icon in the top left corner so we can add elements to the PATH variable
(viii)  In the window which pops open, navigate to “C:\R\Rtools\MinGW\bin” and click OK
.
6. Restart your computer (this solved an issue where the edits to the PATH variable above had not taken immediate affect)
.
7. Open R and run the following code
# install packages
install.packages(c("Rcpp", "rbenchmark", "inline", "Runit"))

library(Rcpp)
library(inline)

# do something with Rcpp to quickly check that it works
body <- '
NumericVector xx(x);
return wrap( std::accumulate( xx.begin(), xx.end(), 0.0));'

add <- cxxfunction(signature(x = "numeric"), body, plugin = "Rcpp")

x <- 1
y <- 2
res
#[1] 3



And there you have it, Rcpp working on Windows! I was able to run Romain’s Rcpp code (see his blog for the code) without any problems. Awesome stuff.

Useful resources I read in order to work out how to correctly install Rcpp:

Rcpp-FAQ

http://lists.r-forge.r-project.org/pipermail/rcpp-devel/