[R] Redirect standard input and output of R

Uwe Ligges ligges at statistik.uni-dortmund.de
Thu Nov 18 09:34:53 CET 2004


Victor Robles wrote:
> Dear R-people!
> 
> IÂ’m trying to write a C program that write to the standard input of R
> and read the standard output.
> I can perfectly read the R output, but IÂ’m not able of writing anything
> to R.
> 
> This program really works with the 'catÂ’ UNIX command, but it does not
> work with R. What IÂ’m doing wrong??? It is possible to do it???


Please read the manual "Writing R Extensions", in particular the lines 
about "Rprintf".

Uwe Ligges


> I want to start R once and use it thousands of times... thatÂ’s why IÂ’m
> trying to use this system.
> 
> 
>       pipe(fdW);
>       pipe(fdR);
> 
> 	pid = fork();
> 	if (pid==-1)
> 		perror("Error fork");
> 	
> 	if (pid==0)
> 	{
> 		close(0);
> 		dup(fdW[0]);
> 		close(fdW[0]);
> 		close(fdW[1]);
> 		
> 		close(1);
> 		dup(fdR[1]);
> 		close(fdR[1]);
> 		close(fdR[0]);
> 		
> 		execlp("R","R","--slave",NULL);
> 		perror("Error exec");
> 	}
> 	else
> 	{
> 		close(fdW[0]);
>         	close(fdR[1]); 
> 
> 		write(fdW[1], "3+7", 3);
> 
> 		total = 0;
> 		while (total<3)
> 		{
> 			leidos = read(fdR[0], &k, sizeof(k));
> 			printf("leidos son %d \n",leidos);
> 			k[leidos]='\0';
> 			if (leidos > 0){
> 				total = total + leidos;
> 				printf("%s\n",k);
> 			}
> 		}
> 	}
> 	exit(0);
> 
> 
> Best regards,
> Victor
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list