Computational Mathematics

Adventures in Computing, Statistics, and R

Installing Rmpi with OpenMPI on Mac OS X Lion

9 Comments »

For whatever reason, Apple decided not to include OpenMPI in Mac OS X Lion (it was supported in Leopard and Snow Leopard). I found this out the hard way after doing a clean install of Lion.

Here are steps to install OpenMPI and get it working with the Rmpi package in R. One benefit of doing it this way is that you get the latest version of OpenMPI.

Step 1 – Install MacPorts
MacPorts is a great tool to compile and install many packages with ease, including OpenMPI. Install MacPorts by following the instructions on this page. Note that you will need to install Xcode off the App Store first.

Step 2 – Install OpenMPI on MacPorts
At the time of this posting, according to this ticket there is a problem compiling OpenMPI on Lion. To get around this, append the following block of code to: /opt/local/var/macports/sources/rsync.macports.org/release/ports/science/openmpi/Portfile

if {${configure.compiler} == "llvm-gcc-4.2"} {
configure.compiler clang
}

Update: This issue has been fixed.
then in Terminal, run:

sudo port install openmpi

Note that gcc44 and openmpi will take a while to build (several hours on my Macbook Air).

Step 3 – Compile and Install Rmpi
It’s best to compile the latest version from source. Download the .tar.gz file from the Rmpi site (at the time of this post, the latest version is 0.6-0 on the developer branch). Then in Terminal, install the package pointing to the proper OpenMPI directory paths with:

R CMD INSTALL --configure-args="--with-Rmpi-include=/opt/local/include/openmpi --with-Rmpi-libpath=/opt/local/lib --with-Rmpi-type=OPENMPI" Rmpi_0.6-0.tar.gz

You can try to load the library in R and run a few test lines to make sure everything is configured properly.

library(Rmpi)
mpi.spawn.Rslaves(nslaves=2)
mpi.remote.exec(runif(5))
mpi.close.Rslaves()

R with Vim on Mac OS X

4 Comments »

The built-in script editor for the Mac OS X R GUI actually isn’t bad. In fact it is much better than its Window’s counterpart. In particular, it has:

  • Syntax highlighting
  • Auto-completion
  • Bracket matching
  • Auto-indent
  • Block code execution (with blocked command history)

However, when coding in R and pretty much any other language, Vim has always been my goto text editor of choice. Once you get past the fairly steep learning curve, nothing comes close to it in terms of coding efficiency and navigation except perhaps Emacs, but let’s not go there.

Michael Bojanowski wrote a blog post on using R with Vim on an Ubuntu machine. One commenter asked about getting it to work on Mac OS X.

First of all, I highly recommend the MacVim port of Vim (not to be confused with macvim.org). You can get the latest version (7.3.53 at time of post) at github:

https://github.com/b4winckler/macvim/downloads

The Vim-R-plugin that Michal mentioned works on OS X with the Screen plugin as well. However, a simpler method is to use one of the following two OS X specific scripts that rely on AppleScript.

R.vim
R.vim uses AppleScript to send selected lines of code to an R buffer. Simply hit <F3> to run the selected lines in R.  If R is not open, it will automatically open a new session. Note that it opens R.app instead of R64.app so if you need to run the 64-bit version you’ll have to tinker with the script a bit.

R-MacOSX
R-MacOSX is also pretty straight forward, although I couldn’t get it working properly. It defaults to <Cmd-E> to run selected lines and <Shift-Cmd-E> to source the entire file.

In either case, key mappings are customizable but personally, I have no trouble with <F3> to execute code using R.vim.  Neither are as full-featured as Vim-R-plugin but I find them sufficient as all I really need is to send blocks of code to R.

Happy Vimming!

How to install R packages from source on Mac OS X

3 Comments »

In my last post about installing the rgdal R package on Mac OS X, I had apparently glossed over the last step of compiling the R package from source.  I hadn’t realized that Mac OS X actually doesn’t come default with the necessary software to do this.  Most times, prepackaged binaries work fine but occasionally, packages may not have a binary available or may need to be customized.  For example, in rgdal, where the default search paths are incorrect for the GDAL frameworks on Mac OS X.

In order to compile a package from the source code, you’ll need the package source (typically a .tar.gz file on Mac OS X) and a compiler (e.g. gcc).  In my opinion, the easiest way to set up the necessary tools is to install Xcode from the Apple Developer site. You’ll need to register (free) for an Apple Developer Connection account to access the download.
(Update, May 20, 2011: Since version 4, Xcode is no longer free for non-developers but can be purchased for $4.99 from the Mac App Store. Xcode 3 remains free from the Apple Developer site and is sufficient if you just need the compilers installed).

Once Xcode has been installed, you’ll then have all the required tools to compile R packages from source.  To do so, simply navigate in Terminal to the folder you saved the source to (the .tar.gz file), and run:

R CMD INSTALL packagename.tar.gz

or some variety of the above with any custom configurations. The R command will automatically run the appropriate commands to compile the package. Happy compiling!

Installing rgdal on Mac OS X

19 Comments »

The rgdal package contains bindings from R to the GDAL (Geospatial Data Abstraction Library) as well as the PROJ library. It is a dependency for numerous spatial statistics packages. Note that the binaries on the rgdal homepage are outdated — the most up-to-date version of the rgdal package can be found on CRAN.

Unsurprisingly, rgdal requires both the GDAL and PROJ framework to run. Both frameworks for Mac OS X are kindly maintained by William Kyngesburye and can be found here:

Unix Compatibility Frameworks

I recommend installing GDAL Complete as it contains both required frameworks in a neat little package.
At the time of writing, there are two versions of GDAL Complete:

  • GDAL 1.7 Complete
  • GDAL 1.6 Complete

Both should work for rgdal but note that currently, the stable Mac OS X version of Quantum GIS (QGIS 1.4) only runs with GDAL 1.6, so if you need to use QGIS as well, I would recommend installing GDAL 1.6.  If you’ve installed GDAL 1.7, then you’ll have to use the developer’s release of QGIS.
(May 19, 2011 update: Install GDAL 1.8 Complete as it works for both standard and developer’s release of QGIS)

Next, assuming you installed GDAL above with the default paths, you’ll need to install the R package from source (get package source here) with modified arguments either with install.packages() or through the terminal:

R CMD INSTALL --configure-args='--with-gdal-config=/Library/Frameworks/GDAL.framework/unix/bin/gdal-config --with-proj-include=/Library/Frameworks/PROJ.framework/unix/include --with-proj-lib=/Library/Frameworks/PROJ.framework/unix/lib' rgdal_0.6-33.tar.gz

If you’re getting this error:

Error: package ‘rgdal’ is not installed for ‘arch=i386′

when trying to load the rgdal package, it is because your version of rgdal was not compiled for the 32-bit version of R. You’ll need to run the 64-bit version of R (R64.app on Macs).

You may also need to update the base version of the sp package as well as any other dependencies.