[R-sig-Debian] R strange behaviour when working with fifos

Julio Sergio Santana juliosergio at gmail.com
Fri Jan 10 19:50:37 CET 2014


I'm trying to establish a connection to a pair of fifos in R, one represents
the input stream of a process and the other one the output of the same
process. The problem is that R behaves very different when running the
commands directly in the interpreter than when running via a script file.

Here is the script

   #! /usr/bin/Rscript --vanilla
   #  The fifos (fifo1 and fifo2) were previously created
   #  in Linux with mkfifo

   #  First we open fifo2 for reading:
   cc2 <- fifo("fifo2", "r+")

   # We launch a process just to count lines, words and chars,
   # with fifo1 as its input and fifo2 as its output, the
   # process won't wait:
   system("wc < fifo1 > fifo2", wait=F)

   # we write three lines on the process' input stream
   writeLines(c("uno dos tres", "cuatro cinco", "seis"), "fifo1")

   # Finally we read the output stream of the process
   (xx <- readLines(cc2)) ## <NOTE 1
   ## character(0)        ## <OUTPUT 1

   (yy <- readLines(cc2))           ## <NOTE 2
   ## [1] "      3       6      31" ## <OUTPUT 2
   close(cc2)

The very strage thing is that when executing in the interpreter line by
line, the line labeled with "NOTE 1" yields, as expected, the output in the
line labeled as "OUTPUT 2", whereas when executing via the script file, the
line in "NOTE 1" reads nothing and it's necesary to execute an additional
reading, as in line labeled with "NOTE 2", to have the desired output.

Do you have any comments in this matter?

Thanks,

 -Sergio.



More information about the R-SIG-Debian mailing list