[R] capturing stderr/stdout

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Fri Nov 21 07:24:43 CET 2008


I have an application in perl that prints some output to either stderr 
or stdout.

Here's an example:

# tmp.pl
print STDERR "starting iterator\n";
for(my $i = 0; $i < 1000000; $i++) {
   print $i . "\n";

# tmp.R
con <- pipe("perl tmp.pl")
r <- readLines(con, n = -1)

However, the second line stalls until the perl for-loop finishes. What I 
would like is to process each line as it comes. E.g. something like:

while(TRUE) {
   r <- readLines(con, n = 1) # read one line
   if(r == "10000") print(r)
   if(length(r) == 0) break

Of course, this won't work since I'm not calling readLines 
appropriately. Answers must work on Windows but may include cygwin 
utilities if necessary. Any advice would be appreciated. Version info at 
the end if it matters.

Thanks, --sundar

 > version
platform       i386-pc-mingw32
arch           i386
os             mingw32
system         i386, mingw32
major          2
minor          8.0
year           2008
month          10
day            20
svn rev        46754
language       R
version.string R version 2.8.0 (2008-10-20)

More information about the R-help mailing list