Skip to content
Prev 26998 / 29559 Next

How to test for a layer in GRASS GIS mapset using rgrass7

Hello,

I am not sure if this is the correct list to ask this specific question 
about executing GRASS GIS from R Scripts.

My problem is how to determine if a layer exists in the current GRASS 
GIS mapset or not.

During my script I use r.mapcalc from GRASS GIS to do some expensive 
computation. So I want to store and reuse the resulting layer, wich is 
produced according to a variable value, in case I need this layer for 
the specific variable value again.

If I just omit the overwrite tag it does not work, becasue GRASS GIS 
stops the execution on this event.

Error in execGRASS("r.mapcalc", expression = expr) : The command:
r.mapcalc expression="rsl40 = GEBCO_2014_2D_4326 >= -40"
produced an error (1) during execution:
FEHLER: output map <rsl40> exists. To overwrite, use the --overwrite flag
Error in execGRASS("r.mapcalc", expression = expr) : The command:
r.mapcalc expression="land_NA_1_rsl40? =? if( rsl40 , 1 ,null())"
produced an error (1) during execution:
FEHLER: output map <land_NA_1_rsl40> exists. To overwrite, use the
 ??????? --overwrite flag

So, I tryed to check if a certain layer already exists in the mapset to 
test if the computation needs/can be executed or not.

Using the following:

if(execGRASS("g.findfile", element="cell", file=lyrname, mapset='"."')){
 ??? return(FALSE)
 ? }else{
 ??? return(TRUE)
 ? }

Here it stops, if the layer does not exist:

Fehler in execGRASS("g.findfile", element = "cell", file = lyrname, 
mapset = "\".\"") :
 ? The command:
g.findfile element=cell file=rsl40 mapset="."
produced an error (1) during execution:


Does anyone know a solution to this?

Thank you very much!

Best,
Christian