I want to use GRASS GIS 7 through R 3.1.3 in kubuntu operational system. Could someone help me connect GRASS with R, and use GRASS commands through R (not the other way round!).

  • look at GRASS wiki: R statistics
    – gene
    Commented Jun 8, 2015 at 15:20
  • Yes, I have looked into it. Still, I would need some clearer instructions! Commented Jun 8, 2015 at 15:49
  • 1
    Show us what you tried, and where you got stuck, and then we can try and get you past that. Have you installed rgrass7?
    – Spacedman
    Commented Jun 8, 2015 at 16:41
  • yo can also use spgrass6
    – gene
    Commented Jun 8, 2015 at 19:53
  • Note that the other way round you refer to is just starting R in GRASS GIS (i.e. from command line inside GRASS session). This will save you from calling initGRASS() but the rest should be the same (you are still using execGRASS() and others to call GRASS modules etc.
    – wenzeslaus
    Commented Jun 9, 2015 at 22:48

2 Answers 2


Here is a quick worked example of setting the GRASS environment, reading an on-disk raster, calculating a focal mean (using r.neighbors) and reading the results back into R. Hopefully this will get you started.

if (!require(rgrass7)) stop("rgrass7 PACKAGE MISSING")

setwd("D:/TMP") # Working directory 

# Set on-disk raster variable
rname <- paste(getwd(), "elev.img", sep="/")

# Set GRASS environment and database location 
loc <- initGRASS("C:/Program Files (x86)/GRASS GIS 7.0.0", 
       home=getwd(), gisDbase="GRASS_TEMP", override=TRUE )

# Import raster to GRASS and set region
execGRASS("r.in.gdal", flags="o", parameters=list(input=rname, output="tmprast"))
execGRASS("g.region", parameters=list(raster="tmprast") ) 

# Calculate 9x9 focal mean 
execGRASS("r.neighbors", flags="overwrite", parameters=list(input="tmprast", output="xxfm", 
          method="average", size=as.integer(9)) )

r <- readRAST("xxfm")
  • 1
    When you end (stop()) after unsuccessful require(), library() might be more appropriate.
    – wenzeslaus
    Commented Jun 9, 2015 at 22:43
  • 1
    works like a charm. if anybody searches for linux path to grass executable: -> /usr/lib/grass72 if you have 7.2 installed. otherwise check with grass<<your version here>> --config path. Note that you need grass-dev package installed as well.
    – joaoal
    Commented Nov 9, 2017 at 10:47

Thank you very much for your responses! Eventually, is very very easy to work with GRASS GIS through R. After you have created in GRASS the location and mapset in which you wish to work, you can type in the GRASS shell: "rstudio &" "&" Helps for working simultaneously in both GRASS GIS and R. Otherwise, the GRASS shell would switch to R. And that is for Linux!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.