Access R from GRASS GIS on Windows

Since I have switch from Windows to Linux, many years ago, things have started to look a lot brighter for those wanting to use GRASS on Windows. I won’t switch back to Windows any time soon, but I recently had to install WinGRASS for somebody else. And it was a whole lot easier than I had feared (or even hoped).

But there is one thing I couldn’t immediately figure out; how to run R from within GRASS. I should add that I installed GRASS using the OSGEO4W installer. When installing GRASS using the stand alone installer, access to R from the GRASS command line should work out-of-the-box (see comment from Helmut in the comment section below).

After a bit of trial and error, I came up with the steps below. It involves editing a file to tell GRASS where to look for executables. In the example below I am adding the path to the R and rstudio executables to this file. Having done that, I can now type R.exe or rstudio.exe on the GRASS command line to open these programs.

Install GRASS GIS using OSGEO4W

Note that I have installed GRASS GIS grass-7.0.0beta3. If you install another version, just substitute the version number with the one you installed in the commands below.

Step 1: Assuming you installed the osgeo4w in the default location (C:\OSGeo4W), go to C:\OSGeo4W\apps\grass\grass-7.0.0beta3\etc

Step 2: Open the env.bat file in your favourite text editor, and look for the line that starts with set PATH (probably the last line). This line tells GRASS where to look for executable files.

Step 3: Ad the following two lines above that line (the paths may differ on your computer):

set RCRAN=C:\Program Files\R\R-3.0.1\bin
set RSTUDIO=C:\Program Files\RStudio\bin

Step 4: Change the line that sets the PATH by adding the references to R and RStudio.

set PATH=%OSGEO4W_ROOT%\apps\msys\bin;%PATH%;%RSTUDIO%;%RCRAN%

Now start GRASS and type on the command line R.exe or rstudio.exe to open R or RStudio respectively (see the GRASS wiki about using R and GRASS).

GRASS installed with the stand alone installer

As mentioned above, if you have installed GRASS GIS with the stand alone installer, things may should work out-of-the-box (see comment from Helmut below). If it for some reason doesn’t (it didn’t for me), you can always edit the grass70.bat file in the c:\Program Files\grass-7.0beta3 folder, using the same steps as above.

2 thoughts on “Access R from GRASS GIS on Windows

  1. Helmut Kudrnovsky

    regarding winGRASS standalone: the “Windows batchfiles for use with R” (https://code.google.com/p/batchfiles/) are included (http://trac.osgeo.org/grass/browser/grass/trunk/mswindows/external), so R should be starting from the command line out of the box (http://grasswiki.osgeo.org/wiki/R_statistics#GRASS_7_Usage); for OSGeo4W(-winGRASS) there is an open ticket (http://trac.osgeo.org/osgeo4w/ticket/413); contribution and testing welcome.

    1. Hi Helmut, thanks for the information. I had seen that R should start from the command line out of the box for the stand alone installation. However, for some reason it did not for me (and as this wasn’t on my computer I did not really have the time or skills to figure out why). However, I edited the text to make clear that normally it should work out of the box.

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