As you all may know, Grass GIS is a great software for academic purposes and using it in an environment like R/RStudio is a very good idea, because you can share your code and other people can reproduce the tasks you did.
Unfortunatly the r-package used for this interactiuon between R and Grass can have some problems when using Windows as an Operating System.
Luckily the creator, Roger S. Bivand provided a safe solution, that should be used initialy so you can work without a problem. Other users at the support board also added some lines and i hope it this code can help maybe save you some time in the near future:
First of all, you need to find your GrassGIS installation folder. When using Windows i highly recomend using the OSGEO4W installation, because it provides you some other nice tools that may be needed. So your installation folder Grass is perhaps included in OSGEO4W64, but you better check if this is true. Otherwise you need to change the path in the following code. Also you need to check which version of grass is installed and which version of python is availabe in OSGEO4W, otherwise it may not work as intended. For me its Python37 and grass78. Be aware that grass may have different folder path for different versions like "grass-6.4.3", "grass78" or even "GRASS GIS 7.8" (having dots in filenames/paths is just not a good idea).
library(rgrass7)
use_sp()
osgeo4w.root<-"C:\\OSGEO4W64"
Sys.setenv(OSGEO4W_ROOT=osgeo4w.root)
# define GISBASE
grass.gis.base<-paste0(osgeo4w.root,"\\apps\\grass\\grass78")
Sys.setenv(GISBASE=grass.gis.base)
Sys.setenv(GRASS_PYTHON=paste0(Sys.getenv("OSGEO4W_ROOT"),"\\bin\\python.exe"))
Sys.setenv(PYTHONHOME=paste0(Sys.getenv("OSGEO4W_ROOT"),"\\apps\\Python37"))
Sys.setenv(PYTHONPATH=paste0(Sys.getenv("OSGEO4W_ROOT"),"\\apps\\grass\\grass78\\etc\\python"))
Sys.setenv(GRASS_PROJSHARE=paste0(Sys.getenv("OSGEO4W_ROOT"),"\\share\\proj"))
Sys.setenv(PROJ_LIB=paste0(Sys.getenv("OSGEO4W_ROOT"),"\\share\\proj"))
Sys.setenv(GDAL_DATA=paste0(Sys.getenv("OSGEO4W_ROOT"),"\\share\\gdal"))
Sys.setenv(GEOTIFF_CSV=paste0(Sys.getenv("OSGEO4W_ROOT"),"\\share\\epsg_csv"))
Sys.setenv(FONTCONFIG_FILE=paste0(Sys.getenv("OSGEO4W_ROOT"),"\\etc\\fonts.conf"))
# call all OSGEO4W settings
system("C:/OSGeo4W64/bin/o-help.bat")
# create PATH variable
Sys.setenv(PATH=paste0(grass.gis.base,";",
"C:\\OSGEO4~1\\apps\\Python37\\lib\\site-packages\\numpy\\core",";",
"C:\\OSGeo4W64\\apps\\grass\\grass78\\bin",";",
"C:\\OSGeo4W64\\apps\\grass\\grass78\\lib",";",
"C:\\OSGeo4W64\\apps\\grass\\grass78\\etc",";",
"C:\\OSGeo4W64\\apps\\grass\\grass78\\etc\\python",";",
"C:\\OSGeo4W64\\apps\\Python37\\Scripts",";",
"C:\\OSGeo4W64\\bin",";",
"c:\\OSGeo4W64\\apps",";",
"C:\\OSGEO4~1\\apps\\saga",";",
paste0(Sys.getenv("WINDIR"),"/WBem"),";",
Sys.getenv("PATH")))
# initial again to be sure
use_sp()
#create a location so you can start with grass tasks
loc <- rgrass7::initGRASS(gisBase=grass.gis.base,
home=tempdir(),
mapset='PERMANENT',
override=TRUE)
I have also used Linux (Manjaro KDE) and it was not necessary to have this code for rgrass7 to work. rgrass7 just found the grass files on its own.
Run this code anytime you start up your R Environment.