NINAnor/oneimpact

Connecting to GRASS from R in Windows

bniebuhr opened this issue · 0 comments

There might be a few issues when trying to connect to GRASS from R or RStudio in Windows operational systems.
A few things are described in the documentation of the initGRASS function from the rgrass7 package or on this and that links that might help.

Here I give a few comments on a few solutions I tried. It seems that what will work depends on how GRASS was installed in your Windows OS. Broadly speaking, GRASS can be installed either by an standalone installer (a compiled version of GRASS) or through OSGeo4W, an installer for a broad set of open source software, besides GRASS. See more information about these two options here. All examples below assume you have both R (and possibly RStudio) and GRASS installed, and that the oneimpact was correctly installed.

If you used a GRASS standalone installer

In this case there are not many restrictions. The important part is to make sure you specify correctly the path to the folder where GRASS executables are located. If you have more than one GRASS installation (and possibly one of them through OSGeo4W, make sure you do NOT use a path like "C:\\OSGeo4W\\apps\\grass\\grass78".

  1. Open a R or RStudio terminal
  2. Load rgrass package and define correctly the GRASS path
library(rgrass7)

# The GRASS folder should be something like:
grassdir <- "C:\\Programs\\GRASS GIS 7.8"
  1. Now we can create a GRASS GIS location and open it from R. The code below should work.
library(oneimpact)
library(terra)

# Load raster data
f <- system.file("raster/cabins.tif", package = "oneimpact")
cabins <- terra::rast(f)

# connect to grass gis 7.8 and create grass location
gisDB <- "." # create location and mapset in the working directory
loc <- "ETRS_33N/" # name of the location
ms <- "PERMANENT" # name of the mapset
rgrass7::initGRASS(gisBase = grassdir,
                   SG = cabins, # use map to define location projection
                   home = tempdir(),
                   override = T,
                   gisDbase = gisDB,
                   location = loc,
                   mapset = ms)

From here you can try the different examples from the oneimpact vignette or from the documentation of the functions.

If you installed GRASS through OSGeo4W: running R or RStudio from the OSGeo4W shell

This is needed if GRASS was installed trough OSGeo4W and not through a standalone installer.
The advantage of using the OSGeo4W shell is that, by installation, the path to all apps installed using OSGeo4W installer are already set, so R will most probably find it easily. R, however, is not necessarily added to the path. See the steps below that worked for me.

  1. Open the OSGeo4W shell
  2. Open R, most probably using
R.exe

It might be that R is not listed in the PATH for OSGeo4W; you can actually check that using the command SET. If not there, you can instead find where R is installed (the command R.home() might help) and run R from there:

# Suppose R is located in
# C:/Programs/R/R-4.1.2
# We can then start R with:
cd "C:/Programs/R/R-4.1.2"
bin\R.exe
  1. From there, it should work to find the path to GRASS executables using something like (for GRASS version 7.8):
library(rgrass7)

grassdir <- system("grass78 --config path", intern = T) # gets the path to GRASS
grassdir
# should be something like
# [1] "C:\\OSGeo4W\\apps\\grass\\grass78"
  1. Now we can create a GRASS GIS location and open it from R. The code below should work:
library(oneimpact) # we use an internal map from the package as a reference to create the GRASS location
library(terra) # we read the raster map with terra

# Load raster data
f <- system.file("raster/cabins.tif", package = "oneimpact")
cabins <- as(raster::raster(f), "SpatialPixelsDataFrame")

# connect to grass gis 7.8 and create grass location
gisDB <- "." # create location and mapset in the working directory
loc <- "test_location/" # name of the location
ms <- "PERMANENT" # name of the mapset
rgrass7::initGRASS(gisBase = grassdir,
                   SG = cabins, # use map to define location projection
                   home = tempdir(),
                   override = T,
                   gisDbase = gisDB,
                   location = loc,
                   mapset = ms)

From here you can try the different examples from the oneimpact vignette or from the documentation of the functions.

If you installed GRASS through OSGeo4W: adding the OSGeo4W to your user environmental variables and run it from R or RStudio

This should work regardless of the OSGeo4W shell. However, it seems the function initGRASS from the rgrass7 package has a condition there and does not allow one to connect to GRASS outside the OSGeo4W shell if the installation was done with OSGeo4W.

So do not try that.