Bug 15393 - Redirect system2 stdout to a file on windows
Redirect system2 stdout to a file on windows
Status: RESOLVED FIXED
Product: R
Classification: Unclassified
Component: Windows GUI / Window specific
R 3.0.1
All Windows 64-bit
: P5 minor
Assigned To: Duncan Murdoch
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2013-07-16 13:29 UTC by Jeroen
Modified: 2014-02-16 11:43 UTC (History)
3 users (show)

See Also:


Attachments
paper for example (557.18 KB, application/pdf)
2013-11-17 08:12 UTC, Matt
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jeroen 2013-07-16 13:29:52 UTC
Specifying a file as the 'stdout' argument of the 'system2' function does not work in Rgui on windows. It does work for Rscript and R.exe on the same system.

    setwd(tempdir()) 
    system2("whoami", stdout="out.txt", stderr="err.txt") 
    file.exists("out.txt") 

See also this thread on r-devel: http://r.789695.n4.nabble.com/Redirect-system2-stdout-to-a-file-on-windows-td4671543.html
Comment 1 Brian Ripley 2013-08-25 11:04:46 UTC
First, the example is not reproducible.  There is no 'whoami' command on Windows (at least, not in my sole copy).

Second, it cannot be major: it works correctly with basic R: it is only an issue in the GUI.

A similar function now works correctly.  But this would not be the first time the issue was with a particular executable.
Comment 2 Duncan Murdoch 2013-08-27 18:28:23 UTC
I just tried this using "attrib" instead of "whoami", and it was fine.  (attrib.exe is a standard Windows executable).  I also tried "whoami" from Cygwin, and it also worked.  So I agree with Brian that the problem is with whatever "whoami" executable you were using.
Comment 3 Jeroen 2013-08-27 20:02:54 UTC
You seem to be right that the problem does not occur for every executable, but it is not limited to whoami (which was perhaps an unfortunate example). More users have been experiencing a related issue with other programs, e.g. http://stackoverflow.com/questions/18080378/tm-readpdf-error-in-filecon-r-cannot-open-the-connection. 

Actually it seems to be the happening for the R executable itself as well. The reason why I came to this problem was because I am trying to capture output from install.packages(). Because sink() and capture.output() don't work, I tried something like this:

setwd(tempdir())
download.file("http://cran.r-project.org/src/contrib/MASS_7.3-28.tar.gz", "MASS_7.3-28.tar.gz")
system2("R", c("CMD", "INSTALL", "MASS_7.3-28.tar.gz"), stdout="out.txt", stderr="err.txt")
file.exists("out.txt")
file.exists("err.txt")

This will prevent stdout and stderr to be printed to the console, but no files are created (at least I couldn't find them).
Comment 4 Duncan Murdoch 2013-08-27 20:31:32 UTC
Thanks, the example with R is very useful.  I'll see if I can reproduce it and identify what's going wrong.
Comment 5 Duncan Murdoch 2013-08-27 20:48:35 UTC
I can't confirm the bug in R-patched or R-devel using your R CMD INSTALL example, though I do see it in 3.0.1.  Could you please try R-patched?
Comment 6 Jeroen 2013-08-27 21:29:56 UTC
The problem seems to have disappeared in R patched indeed.
Comment 7 Duncan Murdoch 2013-08-27 21:33:10 UTC
This was fixed in r63697 by Brian Ripley.
Comment 8 Matt 2013-11-17 08:12:12 UTC
Created attachment 1526 [details]
paper for example
Comment 9 Jackie Rosen 2014-02-16 11:43:50 UTC
(spam comment removed)