The original R-GRASS interface (Bivand 2000;
Neteler and Mitasova 2008) was written mainly to permit R objects
represented as sp class objects to be moved to GRASS,
and GRASS objects to be moved to R for statistical analysis. From
spgrass6 0.6-3 (April 2009) following a fruitful
workshop at Queen’s University, Belfast, the interface was re-written to
use the --interface-description
flag provided for each
GRASS command, also used by the Python interface to GRASS commands.
Command interface descriptions are parsed from XML and cached as R
objects for efficiency. The current version of the R-GRASS interface is
contained in this package, rgrass. In addition, an R
function initGRASS()
was written to permit GRASS to be
started from within GRASS to which we will return below.
When starting GRASS GIS from a terminal console (here GRASS 8.3.2), one can continue in the GRASS terminal console, starting an interactive R session from there (here R 4.3.3). Loading and attaching the R-GRASS interface package rgrass in the R session, we see that the current GRASS location is detected and reported:
$ grass
Starting GRASS GIS...
__________ ___ __________ _______________
/ ____/ __ \/ | / ___/ ___/ / ____/ _/ ___/
/ / __/ /_/ / /| | \__ \\_ \ / / __ / / \__ \
/ /_/ / _, _/ ___ |___/ /__/ / / /_/ // / ___/ /
\____/_/ |_/_/ |_/____/____/ \____/___//____/
Welcome to GRASS GIS 8.3.2
GRASS GIS homepage: https://grass.osgeo.org
This version running through: Bash Shell (/bin/bash)
Help is available with the command: g.manual -i
See the licence terms with: g.version -c
See citation options with: g.version -x
If required, restart the GUI with: g.gui wxpython
When ready to quit enter: exit
Launching <wxpython> GUI in the background, please wait...
GRASS nc_basic_spm_grass7/rsb:~ > R
R version 4.3.3 (2024-02-29) -- "Angel Food Cake"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
...
> library(rgrass)
GRASS GIS interface loaded with GRASS version: GRASS 8.3.2 (2024)
and location: nc_basic_spm_grass7
Since rgrass knows the current location, we
can for example use execGRASS()
to list GRASS rasters in
the PERMANENT mapset in the standard North Carolina basic data set (https://grass.osgeo.org/sampledata/north_carolina/nc_basic_spm_grass7.zip):
> execGRASS("g.list", type="raster", mapset="PERMANENT")
basins
elevation
elevation_shade
geology
lakes
landuse
soils
> q()
Save workspace image? [y/n/c]: n
GRASS nc_basic_spm_grass7/rsb:github-rsb > exit
exit
Done.
Goodbye from GRASS GIS
Leaving R returns us to the GRASS terminal console, which we can also exit.
R can also be started within the GRASS GUI, by choosing the console
tab, and entering for example rstudio
, or another R
graphical user interface. This screendump shows the same listing of
rasters in PERMANENT in rstudio
: