Jean-Victor Côté
2007-Dec-31 19:37 UTC
[R] Program output to file using a batch command file
If I copy the following commands in my batch file to the R console, R creates a file named "basicStatsIBM.lis" with the program output in it: Sortie <- file(description = "basicStatsIBM.lis", open = "wt", blocking = TRUE, encoding = "UTF-8") sink(file = Sortie, append = FALSE, type = "output", split = FALSE) basicStats(ibm) close(Sortie) However, the same commands in a text file (named batch.txt, for instance) referred to from the R console by the command source("batch.txt") yields an empty "basicStatsIBM.lis" file. Why the discrepancy in processing? How to make the batch commands work as if the commands were pasted directly in the R console? A solution to this problem would save time and aggravation to many people. Thank you.
Charles C. Berry
2007-Dec-31 23:13 UTC
[R] Program output to file using a batch command file
On Mon, 31 Dec 2007, Jean-Victor C?t? wrote:> > If I copy the following commands in my batch file to the R console, R > creates a file named "basicStatsIBM.lis" with the program output in it: > > Sortie <- file(description = "basicStatsIBM.lis", open = "wt", blocking > TRUE, > encoding = "UTF-8") > sink(file = Sortie, append = FALSE, type = "output", split = FALSE) > basicStats(ibm) > close(Sortie) > > However, the same commands in a text file (named batch.txt, for instance) > referred to from the R console by the command source("batch.txt") yields an > empty "basicStatsIBM.lis" file. > > Why the discrepancy in processing? > How to make the batch commands work as if the commands were pasted directly > in the R console? > A solution to this problem would save time and aggravation to many people.As it happens, the R Development Core Team provides just such a solution (to 'save time and aggravation to many' of us) in the form of: 1) EXCELLENT DOCUMENTATION to enable you to figure out why this happens 2) flexible functions like source() with useful arguments like 'print.eval' and sensible defaults for them. and 3) Useful problem solving advice in the Posting Guide. By following the advice in the Posting Guide, you would find this help page: ?source and I expect see this bit: Arguments: . . . print.eval: logical; if 'TRUE', the result of 'eval(i)' is printed for each expression 'i'; defaults to 'echo'. --- You might be confused by the word 'printed', which seems to refer only to expressions that would autoprint (and not stuff like 'x <- y') --- 'Nother words use 'source("batch.txt", print.eval=TRUE)' and try the suggestions in the Posting Guide when you hit a snag. Chuck> Thank you. > > ______________________________________________ > R-help at 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901