[R-sig-Geo] syntax problem with r.mapcalculator

Roger Bivand Roger.Bivand at nhh.no
Tue Oct 5 13:28:14 CEST 2010


On Tue, 5 Oct 2010, Aubrey Moore wrote:

>
> I created a raster map, "R20101001", using v.surf.rst.  The interpolation 
> overshoots and dips below zero in a few areas.  I'd like to use 
> r.mapcalculator, called from spgrass6, to replace all negative values with 
> zeros.  But I can't seem to get the syntax right.  The following fails and 
> produces the error message: "The system cannot find the file specified."
>
> execGRASS("r.mapcalculator", flags = c("overwrite"),
>   parameters=list(outfile="result", amap="R20100101", 
> formula='if(A<0,0,A)'))
>
> I would appreciate it if anyone can spot the mistake.

library(spgrass6)
data(meuse.grid)
coordinates(meuse.grid) <- c("x", "y")
gridded(meuse.grid) <- TRUE
fullgrid(meuse.grid) <- TRUE
meuse.grid$ddev <- meuse.grid$dist - mean(meuse.grid$dist, na.rm=TRUE)
loc <- initGRASS("/home/rsb/topics/GRASS/g640/grass-6.4.0",
   home=tempdir(), SG=meuse.grid)
writeRAST6(meuse.grid["ddev"], "ddev")
execGRASS("r.mapcalculator", flags = c("overwrite"),
   parameters=list(outfile="result", amap="ddev", formula="'if(A<0,0,A)'"))
res <- readRAST6("result")
summary(res)
summary(meuse.grid["ddev"])

works for me under Linux with: formula="'if(A<0,0,A)'", protecting the 
inner quotes with outer quotes. On Windows the incantation will be 
different, possibly:

execGRASS("r.mapcalculator", flags = c("overwrite"),
   parameters=list(outfile="result", amap="ddev",
     formula="\"if(A<0,0,A)\""))

(untried on Windows, also works on Linux).

Hope this helps,

Roger

PS. The statsgrass list has more direct access to GRASS users and 
developers with experience of shell quoting solutions.


>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list