[GRASSLIST:1513] Re: system() & R

Roger Bivand Roger.Bivand at nhh.no
Fri Oct 17 07:29:25 EDT 2003


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




More information about the grass-user mailing list