Marc Feuerstein
2006-Nov-17 09:39 UTC
[R] Mayday ! Needing urgent help about writing results to a file
Hey listmembers, I am desperately trying to write a data frame to a file. Not in CSV, but as they "appear" on the screen (nice, easy to read tables). I've read that the sink function is the way to go. I have tried the following code inside a function. sink("ABC.txt") MyFrameA MyFrameB sink() It gives the result I need when I use it outside a function, but when I use it *inside* a function I wrote, it creates an empty file. I want to pull my hair out ! What should I do so that, when the function ends, I have a text file called ABC.txt having MyFrameA and MyFrameB in the same file. Thanks so much (in advance) to help me out on this one ! I'm sure some of you already encountered such a situation. Marc. [[alternative HTML version deleted]]
Gavin Simpson
2006-Nov-17 09:52 UTC
[R] Mayday ! Needing urgent help about writing results to a file
On Fri, 2006-11-17 at 01:39 -0800, Marc Feuerstein wrote:> Hey listmembers, > I am desperately trying to write a data frame to a file. Not in CSV, > but as they "appear" on the screen (nice, easy to read tables). I've > read that the sink function is the way to go. > > I have tried the following code inside a function. > > sink("ABC.txt") > MyFrameA > MyFrameB > sink() > > It gives the result I need when I use it outside a function, but when > I use it *inside* a function I wrote, it creates an empty file. I want > to pull my hair out !Are you forgetting to *print* MyFrameA etc.? It works fine for me in a function if you print the object: foo <- function(x, file = "temp.file.txt") { sink(file = file) print(x) sink(file = NULL) }> dat <- as.data.frame(matrix(rnorm(10), ncol = 5)) > names(dat) <- LETTERS[1:5] > rownames(dat) <- letters[1:2] > foo(dat)G> > What should I do so that, when the function ends, I have a text file > called ABC.txt having MyFrameA and MyFrameB in the same file. > > Thanks so much (in advance) to help me out on this one ! I'm sure some > of you already encountered such a situation. > > Marc. > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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.-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC [f] +44 (0)20 7679 0565 UCL Department of Geography Pearson Building [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street London, UK [w] http://www.ucl.ac.uk/~ucfagls/ WC1E 6BT [w] http://www.freshwaters.org.uk/ %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Peter Dalgaard
2006-Nov-17 09:52 UTC
[R] Mayday ! Needing urgent help about writing results to a file
Marc Feuerstein <marc_feuerstein at yahoo.com> writes:> Hey listmembers, > I am desperately trying to write a data frame to a file. Not in CSV, but as they "appear" on the screen (nice, easy to read tables). I've read that the sink function is the way to go. > > I have tried the following code inside a function. > > sink("ABC.txt") > MyFrameA > MyFrameB > sink() > > It gives the result I need when I use it outside a function, but when I use it *inside* a function I wrote, it creates an empty file. I want to pull my hair out ! > > What should I do so that, when the function ends, I have a text file called ABC.txt having MyFrameA and MyFrameB in the same file.Have a look at related issue in FAQ 7.16> Thanks so much (in advance) to help me out on this one ! I'm sure some of you already encountered such a situation. > > Marc.-- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Alain Guillet
2006-Nov-17 09:57 UTC
[R] Mayday ! Needing urgent help about writing results to a file
Marc, This is an exercise of the lesson STAT2430 at the UCL. Please, do your homework alone!!!! Marc Feuerstein a ?crit :> Hey listmembers, > I am desperately trying to write a data frame to a file. Not in CSV, but as they "appear" on the screen (nice, easy to read tables). I've read that the sink function is the way to go. > > I have tried the following code inside a function. > > sink("ABC.txt") > MyFrameA > MyFrameB > sink() > > It gives the result I need when I use it outside a function, but when I use it *inside* a function I wrote, it creates an empty file. I want to pull my hair out ! > > What should I do so that, when the function ends, I have a text file called ABC.txt having MyFrameA and MyFrameB in the same file. > > Thanks so much (in advance) to help me out on this one ! I'm sure some of you already encountered such a situation. > > Marc. > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. > >-- Alain Guillet Statistician and Computer Scientist Institut de statistique - Universit? catholique de Louvain Bureau d.126 Voie du Roman Pays, 20 B-1348 Louvain-la-Neuve Belgium tel: +32 10 47 30 50