[R] Redirect standard input and output of R

Victor Robles vrobles at fi.upm.es
Thu Nov 18 03:55:44 CET 2004


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???

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




More information about the R-help mailing list