Skip to content

Redirect standard input and output of R

2 messages · Victor Robles, Uwe Ligges

#
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
#
Victor Robles wrote:
Please read the manual "Writing R Extensions", in particular the lines 
about "Rprintf".

Uwe Ligges