Consistently Infrequent

December 7, 2011

Installing Rcpp on Windows 7 for R and C++ integration

Filed under: R — Tags: , , , , — Tony Breyal @ 5:49 pm

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.
    (i)  download R from: http://cran.r-project.org/bin/windows/base/
    (ii) double click the downloaded .exe file
    (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/
    (ii) choose the latest version (I chose batchfiles_0.6-6.zip)
    (iii) when downloaded, right click on file and click Extract All.
    (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:
    (i) go to: http://download.cnet.com/Redmond-Path/3000-2094_4-10811594.html
    (ii) click on “Download Now CNET Secure Download”
    (iii) double click the downloaded .exe file
    (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"))
    
    # load main two packages
    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 <- add(c(x, y))
    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/

http://cran.r-project.org/doc/manuals/R-admin.html#The-Windows-toolset

 

About these ads

33 Comments »

  1. Not a bad list, but I usually 3) and 4) as it isn’t all that hard to edit a PATH by hand via the control panel.

    Glad to know you have it all working.

    Comment by Dirk Eddelbuettel — December 8, 2011 @ 12:58 am

    • I used to not do numbers 3 and 4 also and just directly edit the PATH variable manually. However I found it annoying to have to update it every time I installed a new version of R hence why I use batchfiles to always point to the latest version of R on my system. I wanted the guide to be as easy as possible and that’s why I included the Redmond Path Utility :)

      Comment by Tony Breyal — December 8, 2011 @ 7:41 am

      • My answer to that to install to, say, c:\opt\R\Rcurrent, ie a path without the embedded version. That way the PATH settings remains constant, which makes for *much* easier upgrades :)

        Comment by Dirk Eddelbuettel — December 8, 2011 @ 4:49 pm

        • That’s an interesting idea, I hadn’t thought of that.

          Comment by Tony Breyal — December 8, 2011 @ 5:03 pm

  2. PS Another things I find particularly useful, especially in combination with Rtools already bringing so many Unix tools to Windows, is the MSys shell. See this blog post about compiling a particular Rcpp-using library on Windows via Rcpp. [And there is a “not” missing in my previous comment, right before 3) and 4). ]

    Comment by Dirk Eddelbuettel — December 8, 2011 @ 1:26 am

    • That’s a really interesting link, cheers! Of particular interest is that it’s wrapping a C library to R via Rcpp. I wasn’t aware that Rcpp also works with C libraries in addition to C++ libraries too.

      Comment by Tony Breyal — December 8, 2011 @ 7:54 am

  3. Thanks a lot for the information! I could not run Rcpp in Windows and now it works perfectly after following your steps. Thanks for taking a time writing this mini-guide, many people will find it useful.

    Comment by Eduardo — December 29, 2011 @ 7:40 pm

  4. Is there an update for step 5? There is no “C:\R\Rtools\MinGW\bin” so I am not sure which path to use to link Rtools. Thus I cannot get Rcpp and the test code to work on my Windows machine. Someone said you can edit the PATH by hand via the control panel, how?

    Thanks.

    Comment by Fred — January 24, 2012 @ 2:54 am

  5. Seems there was a problem with the install of Rtools. After re-installing it now works. Thanks!!!!

    Comment by Fred — January 24, 2012 @ 6:12 am

    • Awesome, glad you got it working! :)

      Comment by Tony Breyal — January 24, 2012 @ 9:32 am

    • I am having the same problem. How did you fix the problem with the Rtools installation? I’ve tried re-installing but it doesn’t seem to ever create the “C:\R\Rtools\MinGW” folder.

      Comment by Daniel Hocking — October 3, 2012 @ 7:11 pm

      • That’s strange. Sorry, I can’t reproduce that situation so don’t know how to solve it :(

        Comment by Tony Breyal — October 14, 2012 @ 3:24 pm

  6. Thank you very much for this very detailed procedure. I lost almost a week in searching different ways in order to run RInside on XP,(I work also on Ubuntu 11.04 and yes it works at the first attempt nothing to do)
    Your procedure helps me a lot to clarrify certain key points. I have still certain problems in order to compile RInside. what IDE do you use?
    pascal

    Comment by pascal — February 21, 2012 @ 9:30 am

  7. Thanks a lot for this detailed description!

    I still have some trouble in step 5 though: I get an (0xc000135) error when running the Redmond Path Utility.. any ideas?

    Comment by daniel — June 5, 2012 @ 12:51 pm

    • I’ve no idea about that error I’m afraid. The alternative to using the Redmond Path Utility is to add the components manually to Windows Path. I don’t have Windows on this machine but if memory serves this is basically what you do (if not then take a look at this guide http://www.computerhope.com/issues/ch000549.htm ):

      1. Click through: start >> control panel >> system
      2. Click the Advanced tab.
      3. Click Environment variables.
      4: Edit the PATH variable by appending the following (each separated by a semicolon and do not include the quotes):
      “;C:\R\batchfiles_0.6-6;C:\R\Rtools\bin;C:\R\Rtools\MinGW\bin”

      That’s all the Redmond tool is basically doing but in a much more user friendly manner.

      Comment by Tony Breyal — June 5, 2012 @ 9:37 pm

      • thanks a lot!

        Comment by daniel — June 12, 2012 @ 11:01 am

  8. Thanks – still very helpful!

    Comment by Tinu — September 2, 2012 @ 1:50 pm

  9. Thank you so much, this was an imense help, it worked within 20 minutes, you are Awesome !

    Comment by icecoldo — September 19, 2012 @ 9:45 am

  10. Thanks for outlining the steps needed to get Rcpp working under Windows. Very helpful and saved me a lot of time.

    Comment by Petra — October 9, 2012 @ 3:52 am

  11. Hi, thanks for this helpful information. I have followed the instructions for a 64 bit Windows machine. I installed R 2.15.1 and Rtools version 216 (most recent). The only change I had to make in your instructions is for 5) part viii. The PATH “C:\R\Rtools\MinGW\bin” needed to be “C:\R\Rtools\gcc-4.6.3\bin”

    Comment by Elizabeth — October 11, 2012 @ 3:22 pm

    • Awesome, I didn’t have to do that myself but it’s good to konw for next time if I have an issue as this might be the solution :)

      Comment by Tony Breyal — October 14, 2012 @ 3:30 pm

    • Thanks. That “The PATH “C:\R\Rtools\MinGW\bin” needed to be “C:\R\Rtools\gcc-4.6.3\bin”” solves my problem! Thanks you !

      Comment by wen — February 1, 2013 @ 5:11 pm

  12. Note that Rgui.bat and R.bat in batchfiles already add the three Rtools directories to your path on-the-fly so if you put those two batch files anywhere on your path (the Windows PATH command will list the possible directories) then you can omit steps 4, 5 and 6.

    Comment by G. Grothendieck — December 9, 2012 @ 6:33 am

  13. Thank you very very much! This is the best I can ever find for Rcpp installation. I can never figure it out myself without your help. Thank you soooooo much! You are the best!!!!!

    Comment by wen — February 1, 2013 @ 5:10 pm

  14. Thanks for the incredibly useful post, I am now up and running with the latest R toolset as of 2012-02-23:
    – Windows 7 x64
    – R v2.15.2 (x32 and x64 install)
    – batchfiles_0.6-6.zip

    I tried it with the following two R environments:
    – RStudio-0.97.318 (x32 worked, x64 worked)
    – Revolution R v6.1 (x32 worked, x64 did *not* work)

    The root cause of everything not working out of the box was that R v2.15.2 was installed in a path with a space (“C:\Program Files\R”), to fix everything I reinstalling it in “C:\R”. It would be great if this was fixed (how hard can it be?).

    As others have noted, step 5) viii) needs updating, from “C:\R\Rtools\MinGW\bin” to “c:\Rtools\gcc-4.6.3\bin”. However, Rtools inserts this by default, so you can simply skip 5) viii) and everything will work just nicely (you should observe the path “c:\Rtools\gcc-4.6.3\bin” appearing, however).

    Comment by smt52 — February 23, 2013 @ 5:28 pm

  15. I still experience problem installing everything on my window 7 64 bit system. Anybody know why ?

    > library(inline)
    > library(Rcpp)
    > src <- '
    + std::vector s;
    + s.push_back(“hello”);
    + s.push_back(“world”);
    + return Rcpp::wrap(s);
    + ‘
    > hellofun file19d825b81076.cpp.err.txt’ had status 1
    2: In file(con, “r”) :
    cannot open file ‘file19d825b81076.cpp.err.txt': No such file or directory
    > cat(hellofun(), ‘\n’)

    Comment by Eric — July 31, 2013 @ 12:24 pm

  16. […] digging around on the internet, finding some advice on installing Rcpp on Windows (e.g. Installing Rcpp on Windows 7 for R and C++ integration, by Tony Breyal in December 2011), as well as finding a number of frustrated posts with more […]

    Pingback by Installing Rcpp using C++11 on Windows x64 | Items that others may be interested in — November 22, 2013 @ 9:53 pm

  17. it is really cool! but unfortunately it did not help me. i have done everything exactly this way, but have still errros: filebc1d6d3abd.cpp:4:18: fatal error: Rcpp.h: No such file or directory
    compilation terminated. make: *** [filebc1d6d3abd.o] Error 1 … and then : Executing command ‘C:/R/R-3.0.2/bin/x64/R CMD SHLIB filebc1d6d3abd.cpp 2> filebc1d6d3abd.cpp.err.txt’ returned status 1. does anybony know where the problem is?

    Comment by Tet K — January 16, 2014 @ 7:27 pm

  18. I downloaded R 3.1.1 for windows using the link above ( http://cran.r-project.org/bin/windows/base/) and I got a bunch of MALWARE installed on my PC…specifically Arcade Giant was installed & I’m having problems getting it UN-installed. I am really surprised that this software would be infected. Has anyone else had this experience?

    Comment by SR — October 21, 2014 @ 9:57 pm


RSS feed for comments on this post. TrackBack URI

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

The Shocking Blue Green Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 76 other followers

%d bloggers like this: