7

I know to run a shell script in R is using system command:

my.table <- system(command,intern=TRUE)

However, if the result of my "command" is to print out a table, and I want R to read the table directly into its own data structure. (something like data frame) Is there an easy way to do that? Because the current output in "table" is a character string table. What I want is the R object as read.table().

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pengyao
  • 817
  • 10
  • 15
  • I suspect you'll have to do some text parsing yourself. A few unsolicited suggestions: 1) The `->` assignment is technically correct, but to be avoided. 2) Please post reproducible questions (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) as you'll get better answers. In this case, you'll definitely want to post some sample output. – Ari B. Friedman May 21 '12 at 18:54
  • "The -> assignment is technically correct, but to be avoided" What? – Hansi May 22 '12 at 09:44

2 Answers2

6

If the result 'table' has white-space separators and carriage-returns to mark lines, then you should pass the results to the 'text' argument of read.table:

 inp.tbl <- read.table(text = system(command,intern=TRUE) )
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • (A caveat for users of earlier versions of R: The 'text' argument to `read.table` and derivatives thereof is a relatively recent addition.) – IRTFM May 21 '12 at 19:03
4

I expect using pipe will be more efficient in memory and time than system with intern

inp.tbl <- read.table(pipe(command) )
malcook
  • 1,686
  • 16
  • 16