[R] reading in results from system(). There must be an easier way...

Marc Schwartz marc_schwartz at comcast.net
Fri Sep 12 18:58:23 CEST 2008

on 09/12/2008 11:34 AM Michael A. Gilchrist wrote:
> Hello,
> I am currently using R to run an external program and then read the
> results the external program sends to the stdout which are tsv data.
> When R reads the results in it converts it to to a list of strings which
> I then have to maniuplate with a whole slew of commands (which, figuring
> out how to do was a reall challenge for a newbie like myself)--see below.
> Here's the code I'm using.  COMMAND runs the external program.
>     rawInput= system(COMMAND,intern=TRUE);##read in tsv values
>     rawInput = strsplit(rawInput, split="\t");##split elements w/in the
> list
>                           ##of character strings by "\t"
>     rawInput = unlist(rawInput); ##unlist, making it one long vector
>     mode(rawInput)="double"; ##convert from strings to double
>     finalInput = data.frame(t(matrix(rawInput, nrow=6))); ##convert
> Because I will be doing this 100,000 of times as part of an optimization
> problem, I am interested in learning a more efficient way of doing this
> conversion.
> Any suggestions would be appreciated.
> Thanks in advance.
> Mike

Based upon the presumption that your incoming data are simple tab
delimited values in lines, no header record and where each line is to
end up as a single row in a data frame, you could use something like the

  finalDF <- read.table(textConnection(system(COMMAND,intern = TRUE)),
                        sep = "\t", header = FALSE)

Alternatively, you could use scan() directly, then convert to a matrix:

  finalMAT <- matrix(scan(textConnection(system(COMMAND,intern = TRUE)),
                          sep = "\t"),
                     nrow = 6)

These are untested of course, but should get you close, if not there.

See ?textConnection for the basic process of taking incoming data from a
text stream.


Marc Schwartz

More information about the R-help mailing list