QGIS with R: Working with the SEXTANTE plugin

SEXTANTE toolboxI’ve tried to get QGIS and R playing together in the past, with only occasional success. The new SEXTANTE plugin for QGIS, which provides links into a number of applications including R, SAGA and GDAL, looks really promising.

Getting it working on Mac OSX was a little tricky, as there is a (known) bug in the plugin that hasn’t been resolved yet.

The fix:

  1. Open up the python plugin directory for SEXTANTE: ~/.qgis/python/plugins/sextante/r/
  2. Make a backup copy of RUtils.py (command-D in the Finder);
  3. Open up RUtils.py in a text or code editor (I’m using Komodo edit these days and it’s working really well);
  4. On line 58, change:
    command = ["R", "CMD", "BATCH", "--vanilla", RUtils.getRScriptFilename(), RUtils.getConsoleOutputFilename()
    to
    command = "R CMD BATCH --vanilla " + RUtils.getRScriptFilename() + " "+ RUtils.getConsoleOutputFilename()
  5. Save and restart QGIS.

While you can’t simply run an R script directly, you can create a .rsx file either in an editor or directly with the SEXTANTE toolbox and use it. This is the Raster_histogram.rsx file from the examples:

##[Example scripts]=group
##layer = raster
##showplots
hist(as.matrix(layer))

Giving, from a DEM layer in QGIS: