I need help to connect R with Grass. In RStudio when I send the line:

loc <- initGRASS("C:/Program Files/R/R-3.1.0/bin/i386", home=tempdir(), override=TRUE)

It says:

Error in system(paste("g.dirseps.exe -g", shQuote(Sys.getenv("WINGISRC"))),  : 
  'g.dirseps.exe' not found

Guys, can you help me, please?


Are your grass binaries really in "C:/Program Files/R/R-3.1.0/bin/i386"? The "gisBase" argument is the path to your GRASS install.

Something like this:

loc <- initGRASS("C:/Program Files (x86)/GRASS 6.4.2", home=getwd(),  
                 gisDbase="GRASS_TEMP", override=TRUE )

Here is an example R/GRASS session that calculates the 3x3 surface relief ratio.

# Load package and set GRASS environment
loc <- initGRASS("C:/Program Files (x86)/GRASS 6.4.2", 
       home=getwd(), gisDbase="GRASS_TEMP", override=TRUE )

execGRASS("r.in.gdal", flags="o", parameters=list(input="dem.img", output="DEM"))
  execGRASS("g.region", flags="a", parameters=list(rast="DEM", res=as.character("30"),
            align="DEM") )

# Calculate surface relief ratio and export to img format
execGRASS("r.neighbors", flags="overwrite", parameters=list(input="DEM", output="mean", 
          method="average", size=as.integer(3)) )
execGRASS("r.neighbors", flags="overwrite", parameters=list(input="DEM", output="min", 
          method="minimum", size=as.integer(3)) )
execGRASS("r.neighbors", flags="overwrite", parameters=list(input="DEM", output="max", 
          method="maximum", size=as.integer(3)) )                 
execGRASS("r.mapcalculator", flags="overwrite", parameters=list(formula="(mean - min)/(max - min)",
          outfile="srr" ) )                   
execGRASS("r.out.gdal", parameters=list(input="srr", format="HFA", type="Float32", 
          output="srr3.img", nodata=-9999) )

# Clean up temp rasters       
execGRASS("g.remove", parameters=list(rast=c("mean", "min", "max", "srr")) )    

# Clean up GRASS workspace
unlink(paste(getwd(), "GRASS_TEMP", sep="/"), recursive=TRUE)
  file.remove(paste(getwd(), ".grassrc6", sep="/"))
  • thank you a lot, it works. But, it is another problem which I didn't notice before. When I run the next line: library("spgrass6") , it says: GRASS GIS interface loaded with GRASS version: (GRASS not running) – Ariel Apr 15 '14 at 14:25
  • That is not an error just a package loading message. Although, I do not understand what you mean by "run next line". You should be loading the package before running "initGRASS". I expanded my answer to provide a brief example of an spgrass6 session. – Jeffrey Evans Apr 15 '14 at 14:43
  • how does this work on linux? – joaoal Mar 18 '17 at 16:49
  • ok its /usr/lib/grass72 if you have 7.2 installed. otherwise check with grass<<your version here>> --config path – joaoal Mar 18 '17 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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