What libraries do I need to execute this GRASS tool (or any other) from an script in R ? Has anybody try to compare performance between using r.viewshed from QGIS and r.viewshed from R ?
Quite a few GIS articles cover this. I think Riggs paper in TGIS is likely the one you want.
You must have a R and GRASS version installed. Check the compatibility among them. Then you can use:
library(rgrass7) loc <- initGRASS('C://Program Files//GRASS GIS 7.4.0', # Path to your GRASS folder gisDbase='D://grass', # Yor grass DB location = 'MAGNA2', # Grass location mapset ='PERMANENT', # Grass mapset override = TRUE) # ## Calculate new layer. A raster ER is created into GRASS DB, not into R environment execGRASS("r.mapcalc", expression = 'ER=if(P_over_L>0.316,ptrast/sqrt(0.9+P_over_L^2),ptrast)', flags = 'overwrite') execGRASS("r.watershed", elevation='dem', flow = 'flow', depression = 'depression', flags = 'help') execGRASS('g.list', mapset = 'PERMANENT', type = 'raster', intern = TRUE) execGRASS('g.list', mapset = 'PERMANENT', type = 'vector', intern = TRUE) ## List available layers. Use ´intern = TRUE´ for bring the list into R layers <- execGRASS('g.list', mapset = 'PERMANENT', type = 'all', intern = TRUE) ## Bring GRASS layer into R exampleVEC <- readVECT('basin_1101_11017010') ## Bring GRASS layer into R exampleR <- readRAST('ER)
Hope you find it useful.