I'm finding errors using the function fasterRastDistance() in the fasterRaster R library when running the same script in several simultaneous RStudio instances.

The error looks like this:

ERROR: You must select the PERMANENT mapset before updating the current
       location's projection (current mapset is <<UNKNOWN>>)
access: Invalid argument
ERROR: LOCATION <C:/Users/JavierF/AppData/Local/Temp/RtmpqoyKjm\<UNKNOWN>>
       not available
Error in doGRASS(cmd, flags = flags, ..., parameters = parameters, echoCmd = echoCmd,  : 
  Parameter <input> has multiple values

The script runs smoothly if I use just one RStudio execution, but appears at some point of a loop iteration if I use several simultaneous RStudio sessions. I want to use several RStudio instances because that allows me to process more files simultaneously; I am aware that code parallelization might work better than several RStudio sessions for that matter, but I cannot use that for unrelated reasons (the script also uses the terra package that does not work well with parallelization).

I don't have much experience with GRASS or rgrass7, which underlies fasterRaster, but I believe that the error indicates that some temporary files created within GRASS environments share name across RStudio instances. My interpretation is that, at some point, these tmp files are deleted when no longer needed by one of the R instances, and then the file is missed by the other instance. If that is correct, my approach to overcoming this problem would be to explicitly choose a unique tmp file name each time, but I don't have enough rgrass7/GRASS knowledge about how to go about that.

A simplified version of the script is:

grassDir <- 'C:/Program Files/GRASS GIS 7.8'
rasters_directories <- list.files(..., full.names = T) # these are rasterized lines, with value 1 on lines and NA on pixels not overlapping lines

for (r in rasters_directories){
  line_raster <- rast(r)   # (I use rast() from terra because the rest of the script runs faster like that)

  dist_raster <- fasterRastDistance(rast = raster(line_raster), 
                                         metric = "geodesic",
                                         grassDir = grassDir) 

  writeRaster(dist_raster, paste0(".../", r, ".tif))  # (well, a modification of this to pick the name, not the full directory...)

How do I solve the issue?

  • 1
    I think that this may be a design flaw in the fasterRaster package. I would recommend just using the rgrass7 package where you can specifically control the working directory for GRASS. Most of the fasterRaster functionality, with a few notable exceptions, are wrappers for rgrass7 to simplify file format swapping. The fasterRastDistance function is simply calling rgrass7::execGRASS("r.grow.distance"). Commented Dec 9, 2021 at 17:08
  • As per the help center please do not include chit chat like statements of appreciation within your posts.
    – PolyGeo
    Commented Dec 9, 2021 at 20:34
  • Thanks @JeffreyEvans - I am giving the option to code this in rgrass7 a shot, although I'm running in possibly related pitfalls, just because I'm new to GRASS and need to understand environments, mapsets or usage of working directories. I've started a new thread with a rgrass7 specific question here gis.stackexchange.com/questions/418599/… Commented Dec 10, 2021 at 18:18


Your Answer

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

Browse other questions tagged or ask your own question.