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 | e...@debian.org | http://dirk.eddelbuettel.com ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.