Skip to content

Reading several tables from stdin

5 messages · Julio Sergio, Jeff Newmiller

#
I'm trying to write a Rscript program capable of reading several tables from the 
standard input. The problem is that the tables aren't in files because they are 
coming from another process that is generating them. From the R-console the 
following works pretty well:

  |> f <- stdin()
  |> t <- read.table(f)
  |> t2 <- read.table(f)
  |> t
     uno dos
  01   3   4
  02   5   6
  03   7   8
  |> t2
     uno dos
  01   9  10
  02  11  12
  03  13  14
  
  |> t + t2
     uno dos
  01  12  14
  02  16  18
  03  20  22

Being aware of the differences between stdin(), and file("stdin"), I wrote a 
Rscript program that is aimed at doing the same as the shown interactive 
session:


  #! /usr/bin/Rscript
  f <- file("stdin")
  t <- read.table(f)
  t2 <- read.table(f)
  print (t + t2)

When I try this script, it always ends in error, as follows:

FIRST TRY:
  $ ./addT.R 
  uno dos
  01 3 4
  02 5 6
  03 7 8
             <-I introduced an emtpy line here
  uno dos
  Error en scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
    la linea 4 no tiene 3 elementos
  Calls: read.table -> scan
  Ejecuci?n interrumpida

SECOND TRY
  $ ./addT.R 
  uno dos
  01 3 4
  02 5 6
  03 7 8
             <-I introduced the EOF terminator (^D) here
  Error en isOpen(file, "rt") : conexi?n inv?lida
  Calls: read.table -> isOpen
  Ejecuci?n interrumpida


Do you have any comments on this?

Thanks,
-- Sergio.
#
Julio Sergio <juliosergio <at> gmail.com> writes:
the
Well, maybe I'm not posting this message in the right forum ...
Uhmm, could anyone tell me where to post this?

Thanks,

--Sergio
#
Your expectations of response time are out of line for a mailing list.  The subject of your question might or might not be appropriate here depending on  how portable you want the solution to be.

Your dependence on operating-system-specific features (ctrl-d as eof) and expectation that such features will allow you to squeeze multiple files into a single file (they usually don't) are also problems. You also didn't supply the output of sessionInfo so we don't even know your environment. Perhaps a (re)read of the posting guide is in order?

One simple approach would be to write the tables to temporary files that your R script would delete after reading. You could then simply pass the names of the files in your stdin.

If you are bound and determined to put the data in one stream, you will probably need to wrap it in some higher-level format and split the tables out of it. One option there would be XML. Alternatively, you could define your own protocol, read all the data in with readLines, and parse it out yourself.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
Julio Sergio <juliosergio at gmail.com> wrote:

            
#
Jeff Newmiller <jdnewmil <at> dcn.davis.CA.us> writes:
I thought the output was provided, as I copied the output directly from my 
terminal. By the way the OS system is Ubuntu Linux 12.4
I guess, then, that there are some facilities in R to read XML. Can you 
direct me to such information?

Thanks,

-- Sergio.
#
Since you don't seem to recognize the term "sessionInfo", you definitely need to read the posting guide. See the link at the bottom of the message.
is a good tool to know about.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
Julio Sergio <juliosergio at gmail.com> wrote: