Passing a parameter from R to r.mapcalc ?

2 messages Options
Embed this post
Permalink
alobo

Passing a parameter from R to r.mapcalc ?

Reply Threaded More More options
Print post
Permalink
Hi!

I have an script in R that mostly controls the
flow of grass commands through system()
I've got stuck at:

for (k in mistat[,1]){
                system("r.mapcalc 'mancha = if(manchas.clmp==k,1,0)'")


is there any way I could pass the value of k to r.mapcalc?
I was thinking on using the shell through system, something like
for (k in mistat[,1]){
                system("set k = @1")
                system("r.mapcalc 'mancha = if(manchas.clmp==$k,1,0)'")

but do not get thru,
(perhaps I'm mixing my memories of the csh and the Bourne shell...)

any help?

Agus

--
Dr. Agustin Lobo
Institut de Ciencies de la Terra "Jaume Almera" (CSIC)
LLuis Sole Sabaris s/n
08028 Barcelona
Spain
Tel. 34 934095410
Fax. 34 934110012
email: [hidden email]
http://www.ija.csic.es/gt/obster
_______________________________________________
grass-stats mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats
Roger Bivand

Re: Passing a parameter from R to r.mapcalc ?

Reply Threaded More More options
Print post
Permalink
On Tue, 20 Jan 2009, Agustin Lobo wrote:

> Hi!
>
> I have an script in R that mostly controls the
> flow of grass commands through system()
> I've got stuck at:
>
> for (k in mistat[,1]){
> system("r.mapcalc 'mancha = if(manchas.clmp==k,1,0)'")
>
>
> is there any way I could pass the value of k to r.mapcalc?
> I was thinking on using the shell through system, something like
> for (k in mistat[,1]){
> system("set k = @1")
> system("r.mapcalc 'mancha = if(manchas.clmp==$k,1,0)'")
>
> but do not get thru,
> (perhaps I'm mixing my memories of the csh and the Bourne shell...)

Please don't cross-post - people may not be subscribed to both lists and
this may break threads.

The answer is paste(), or possibly a formatting function:

cmd <- paste("r.mapcalc \'mancha = if(manchas.clmp==", formatC(k, ...),
   ",1,0)\'", sep="")
cmd
cat(cmd, "\n")
system(cmd)

if you need more control of the string going to system(); in my
experience, eyeballing cmd is very helpful.

Roger

>
> any help?
>
> Agus
>
>

--
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: [hidden email]

_______________________________________________
grass-stats mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats
Roger Bivand
Economic Geography Section
Department of Economics
Norwegian School of Economics and Business Administration
Helleveien 30
N-5045 Bergen, Norway