[R] embedding data frame in R code?

Dirk Eddelbuettel edd at debian.org
Fri Aug 3 05:35:43 CEST 2012


On 3 August 2012 at 03:15, William Dunlap wrote:
| > and you probably want closeAllConnections() immediately following
| 
| No you do not want to close all connections.  You should close each
| connection that you open, but not others (they may be used by other
| functions like sink() or capture.output()).  Use something like:
| 
|    readTableFromText <-function (text, ...) {
|        tc <- textConnection(text, open = "r")
|        on.exit(close(tc))
|        read.table(tc, ...)
|    }
| 
| as in
| 
|    > readTableFromText(c("10 ant", "20 bear", "30 cougar"), row.names=NULL)
|      V1     V2
|    1 10    ant
|    2 20   bear
|   3 30 cougar
|   > showConnections() # no open connections
|         description class mode text isopen can read can write
|   >

Nice example, but you no longer need this. 

I forgot which version changed this (R 2.15.0 maybe?) but now the much
simpler direct use works without the need to close the connection:

  R> tab <- read.table(textConnection("a b\n1 2\n3 4"), header=TRUE)
  R> tab
    a b
  1 1 2
  2 3 4
  R> showConnections()
       description class mode text isopen can read can write
  R> 


Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com



More information about the R-help mailing list