【发布时间】: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"
【问题讨论】: