[GRASSLIST:1516] R & BATCH & setenv()

Massimiliano Cannata massimiliano.cannata at supsi.ch
Fri Oct 17 13:01:27 EDT 2003


Hi, back again,
as my poor background i cannot understand why it doesn't works.....

I do:

1- i built my "file.c" for GRASS input that is:

.....
timestamp=input->answer;
setenv("R_TTIME",(const char*)timestamp,1);
system("R BATCH /home/pippo/file.R");
return 0;
}

2-i have my "file.R" with:
Ttime<-Sys.getenv("R_TTime");


But if I compile and run in grass I do not get any output, while if I run my
"file.R" in R interactive environment setting by hand the variable Ttime it
works perfectly....
I guess, the C funcion setenv() is the rigth one?
As if I put in my code "system(echo $R_TTIME)" I have the rigth answer but
when I exit Ididn't see it no more typing in my shall echo $R_TTIME....??????

Please help me with en example as i'm going crazy with it!





Roger Bivand wrote:

> On Fri, 17 Oct 2003, Cannata, Massimiliano wrote:
>
> > Hello users,
> > i write an R script that execute temperature interpolation
> > inside a GRASS environmental.
> > Now all i want to do is write a GRASS command that send
> > some variable and command to my system.....
> >
> > After having started my GRASS section i have to run my
> > command, set my variable and then trogth the C system()
> > command start my R section, execute my script.R and quit
> > R.
> >
> > My problem is that after starting R section my system do
> > not go on..... is waiting for command input.....
> > Have you the solution?
> >
> > Follows my code:
> >
> > //variable setted
> > timestamp = input->answer;
> >       dtm=elevation->answer;
> >
> > //start R
> >       system("R;");
>
> I think this is where things are falling apart. My guess would be that
> this is starting an *interactive* R. You need rather to write the whole R
> command file first, then set appropriate environment variables, then call
> R as a batch job, saving the output as appropriate. There is a good
> section on this in Neteler/Mitasova (2002) in the chapter on interfacing
> with other software. Have a look at:
>
> http://grass.itc.it/statsgrass/grass_geostats.html
>
> and some of the resources there too. Running R as a batch job is also
> covered in R, R --help shows some, R BATCH --help some more. By the way,
> sending R code through system() to your regular shell is not likely to
> work, but if you want to use sockets, R can read from and write to them
> too. Then you could perhaps "interact" with an R instance started by
>
> system("R --vanilla < give_me_a_socket.R > output_to_log.txt");
> or whatever, where the input file gets R ready to read from your socket
> connection. But in general people write batch files ahead of time, and
> pass variable strings into R using environment variables in the batch
> script (see Sys.getenv()).
>
> >
> > //delete all variable
> >       system("rm(list = ls());");
> >
> > //set the R variable Ttime & dem
> >       command = strcat("Ttime<-'",(const char*)timestamp);
> >       command = strcat(command,"' > /home/tmp.txt");
> >       system(command);
> >       printf(command);
> >
> >       command = strcat("dem<-'",(const char*)dtm);
> >       command = strcat(command,"' > /home/tmp.txt");
> >       system(command);
> >       printf(command);
> >
> > //run my R script
> >       system("source(/home/mcannata/Rscript/temper.R)>tmp.txt");
> >       printf("eleboration running...");
> >
> > //write my Raster to GRASS
> >       command = "rast.put(G,lname=";
> >       strcat(command,(const char*)timestamp);
> >       strcat(command,",Temperatura$t)");
> >       printf(command);
> >
> > //delete redirected output
> >       system("rm /home/tmp.txt");
> >
> > //stop R session
> >          system("q()");
> >
> > --------------------------------------
> > Eng. Massimiliano Cannata
> > Phd student
> > www.supsi.ch
> >
> >
>
> --
> Roger Bivand
> Economic Geography Section, Department of Economics, Norwegian School of
> Economics and Business Administration, Breiviksveien 40, N-5045 Bergen,
> Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
> e-mail: Roger.Bivand at nhh.no

--
-------------
note: change my e-mail reference to massimiliano.cannata at supsi.ch
      because the old one will be deleted soon.
-------------
Ing.  Massimiliano Cannata
Istituto di Scienze della Terra - SUPSI
C.P. 72 - CH-6952 Canobbio (Ticino, Switzerland)
Tel +41 91 /935 12 25 - Fax +41 91 /935 12 09
eMail: massimiliano.cannata at supsi.ch
Internet: http://www.ist.supsi.ch




More information about the grass-user mailing list