【问题标题】:system2() in R 3.5 ignores stdout parameterR 3.5 中的 system2() 忽略标准输出参数
【发布时间】:2018-12-01 13:03:05
【问题描述】:

我最近从 R 3.4.3 升级到了 R 3.5.1

我的应用进行了一个 system2() 调用,该调用在 windows 和 linux 上都可以在 3.4.3 中运行,但在 3.5.1 windows 中不再运行(但在 linux 中仍然可以运行)。特别是,我的 system2() 调用将标准输出重定向到一个文件,但是在 R 3.5.1 中的 Windows 上,它不会重定向,而是将输出发送到控制台。

有人对这个问题有任何经验吗?我没有找到任何关于改变 system2 应该如何工作的注释;我是否错过了对其行为的预期变化?我可以使用什么变通方法让它工作?

我创建了这段代码 sn-p 来演示这个问题:

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")

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"

3.5.1 在 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

3.5.1 在 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"

【问题讨论】:

    标签: r rstudio


    【解决方案1】:

    我能够使用修补了 R 3.5.1 的 RGui 重现此问题——我将此作为 R 核心团队的错误报告提交。 https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=17508

    编辑:虽然我不确定这是否是故意的,但我相信这是负责更改的提交:

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

    为了适应行为的变化,您可以设置 stderr 参数——例如,这应该可以工作:

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

    【讨论】:

      猜你喜欢
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多