James
James

Reputation: 235

system2() in R 3.5 ignores stdout parameter

I have recently upgraded from R 3.4.3 to R 3.5.1

My app makes a system2() call that works in 3.4.3 on both windows and linux, but no longer works in 3.5.1 windows (but does still work in linux). In particular, my system2() call redirects stdout to a file, however on windows in R 3.5.1, it does not redirect, but sends the output to the console.

Does anyone have any experience with this issue? I didn't find any notes on a change to how system2 is supposed to work; am I missing an expected change to its behaviour? Any workarounds I can use to get it to work?

I created this code snippet to demonstrate the problem:

version$version.string
unlink("output.txt")
file.exists("output.txt")
system2("cmd", args = c("/c", "echo", "hello world"), stdout = TRUE)
system2("cmd", args = c("/c", "echo", "hello world"), stdout = "output.txt")
file.exists("output.txt")
readLines("output.txt")

The output from 3.4.3:

> version$version.string
[1] "R version 3.4.3 (2017-11-30)"
> unlink("output.txt")
> file.exists("output.txt")
[1] FALSE
> system2("cmd", args = c("/c", "echo", "hello world"), stdout = TRUE)
[1] "hello world"
> system2("cmd", args = c("/c", "echo", "hello world"), stdout = "output.txt")
> file.exists("output.txt")
[1] TRUE
> readLines("output.txt")
[1] "hello world"

The output from 3.5.1 on windows:

> version$version.string
[1] "R version 3.5.1 (2018-07-02)"
> unlink("output.txt")
> file.exists("output.txt")
[1] FALSE
> system2("cmd", args = c("/c", "echo", "hello world"), stdout = TRUE)
[1] "hello world"
> system2("cmd", args = c("/c", "echo", "hello world"), stdout = "output.txt")
hello world
> file.exists("output.txt")
[1] FALSE
> readLines("output.txt")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file 'output.txt': No such file or directory

The output from 3.5.1 on linux:

> version$version.string
[1] "R version 3.5.1 (2018-07-02)"
> unlink("output.txt")
> file.exists("output.txt")
[1] FALSE
> system2("echo", args = c("hello world"), stdout = TRUE)
[1] "hello world"
> system2("echo", args = c("hello world"), stdout = "output.txt")
> file.exists("output.txt")
[1] TRUE
> readLines("output.txt")
[1] "hello world"

Upvotes: 2

Views: 603

Answers (1)

Kevin Ushey
Kevin Ushey

Reputation: 21285

I was able to reproduce this using RGui with R 3.5.1 patched -- I filed this as a bug report for the R core team. https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=17508

EDIT: While I'm not sure if this was intentional, I believe this is the commit responsible for the change:

https://github.com/wch/r-source/commit/a0217674cba49d50a24dd42ea156f78687bd8de3

To accommodate the change in behavior, you can set the stderr argument -- for example, this should work:

args <- c("/c", "echo", "hello")
system2("cmd", args = args, stdout = "output.txt", stderr = FALSE)

Upvotes: 1

Related Questions