# Compiling and Cross-compiling R packages for Windows (win32)

September 3, 2009
By

(This article was first published on Super Nerdy Cool » R, and kindly contributed to R-bloggers)

so recently i’m learning how to compile and cross-compile R packages from source for windows machines, which means i have to create windows binaries. the first option is to build on a windows system, and the 2nd is to cross-build on an intel-based linux or mac system. i will outline my experience.

Send it off to a Build Farm If you have the source package ready for install on a linux based system, you can send it off to a server and have it automatically build. This is probably the easiest way.

Building R packages and binaries on a Windows system. References: http://cran.r-project.org/doc/manuals/R-admin.html#Building-from-source http://faculty.chicagobooth.edu/peter.rossi/research/bayes%20book/bayesm/Making%20R%20Packages%20Under%20Windows.pdf http://www.biostat.wisc.edu/~kbroman/Rintro/Rwinpack.html Requirements: Rtools and R source file

At the time of this writing, I am using R 2.9.2 and using Rtools29. 1. First, install Rtools, which includes some basic unix utilities, compilers (MinGW), windows compatible perl, unicode, Tcl, and some bitmap stuff. Install should be in C:Rtools and C:R. Go ahead and install everything, including the Tcl stuff. In the install, go ahead and click modify Path variable if you have admin access. if not, add the subfolders of Rtools that has bin into the user ‘Path’ variable (either for User or for System) from the Control Panel > System > something > Advanced tab > Environment variable button. The variable should be something like ‘H:Rtoolsbin;H:RtoolsMinGWbin;H:Rtoolsperlbin’. 2. go to start > run > cmd to open a command window. I placed my R source tar.gz file in H:R. now, use tar zxvf to unarchive it. 3. Move H:RTcl into the R-version folder that we just unarchived. Move the folders ‘unicode’ and ‘bitmap’ from H:Rsrcgnuwin32 into R-versionsrcgnuwin32. Main thing is we need the iconv.dll file. 4. goto R-versionsrcgnuwin32. type make (or make all) to compile. this gave me a couple errors. if u don’t have Tcl in the right location, edit the R-versionsrcgnu32MkRules and change the path for Tcl. I also got an error where it couldn’t find the /tmp/ folder, so i just created H:tmp. R successfully compiled: see R-versionbin. 5. To make the windows binaries, I have a package folder prepared under mac/linux. I copied it to the windows machine, and i did something like in H:R (where the package folder is in, say H:RMyPackage): H:RR-versionbinRcmd.exe build –binary MyPackage The source should be compiled into a dll and i get a zip file where i could install on other windows machines.

Cross building package win32 (windows) binaries on an intel-based mac or linux Since i don’t have a windows system (i did it on my brother’s computer), it could be convenient to build this on my mac or linux system. The R core team used to support cross building, but stripped support as of v 2.9. I first outline my experience with R 2.8 on my Mac OS X. Look at the R-admin help file for cross-building in the 2.8. I also got help from this post on R-dev, and a few inquiries with the poster through email.

Requirements: R source code file MinGW for mac (I installed v.4.3.0 binaries and added the location to my PATH variable) R (mac/linux, your system) binaries (version must match that of source. i had R 2.8 currently installed at the time, so i cross-build this version). R win32 binaries (same version as well): u can install this from source using the cross-build stuff, but i kept getting errors. so, what i did, was used the windows installer and installed it using Wine on my Mac (~/.wine/drive_c/Program Files/R/R-version/ — the space in Program Files isn’t good, so i made a softlink via ln -s to ~/R)

In R-version (extracted somewher), go to /src/gnuwin32, edit MkRules, changed: BUILD=CROSS no CHM, this version picks this up by default with CROSS BINPREF=i386-mingw32-##i586-mingw32- R_EXE=/usr/bin/R###’I failed to read the instructions to set R_EXE in MkRules’

Now, suppose the name of my package is MyPackage and it is locted in /Users/vinh/Downloads/Rwin/ (MyPackage folder is in here)

Instructions here are found in README.package of gnuwin32:

 cd .../src/gnuwin32
make PKGDIR=/mysources RLIB=/R/win/library pkg-mypkg
cd /R/win/library
zip -r9X /dest/mypkg.zip mypkg


in my gnuwin32 folder, i did:

make PKGDIR=/Users/vinh/Downloads/Rwin/ RLIB=/Users/vinh/R/library/ pkg-MyPackage
[it compiled my dll file]
cd ~/R/library
zip -r9X MyPackage MyPackage


Now I have MyPackage.zip that can be installed on windows machines! Remember, ~/R is the softlinked version of where my windows version of R is.

Next, i’m going to attempt cross-compiling on R 2.9.2, where support is no longer supported, but as the post i referenced above described, this should still be do-able. I will update this.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...