从 Python 调用 R 脚本不会在版本 4 中保存日志文件
Calling R script from Python does not save log file in version 4
我正在从 python 调用一个名为 R Code.R:
的非常简单的 R 脚本
args <- commandArgs(TRUE)
print(args)
source(args[1])
setwd("<YOUR PATH>")
output <- head(mtcars, n = n)
write.table(output, "output.txt")
使用以下脚本:
import subprocess
pth = "<YOUR PATH>"
subprocess.call(" ".join(["C:/R/R-3.6.0/bin/x64/R.exe", "-f", '"' + pth + '/R Code.R"', "--args",
'"' + pth + '/arguments.txt"',"1>", '"' + pth + '/log.txt"', "2>&1"]))
subprocess.call(" ".join(["C:/R/R-4.0.3/bin/x64/R.exe", "-f", '"' + pth + '/R Code.R"', "--args",
'"' + pth + '/arguments.txt"',"1>", '"' + pth + '/log.txt"', "2>&1"]))
其中 arguments.txt 包含:
n <- 10
问题是当我使用 R-4.0.3 时,log.txt 文件没有生成,我需要转储一个日志文件,因为它会在我的后续过程中自动寻找它。
当我在 CMD (Windows) 中执行以下命令时:
C:/R/R-4.0.3/bin/x64/R.exe -f "<YOUR PATH>/R Code.R" --args "<YOUR PATH>/arguments.txt" 1> "<YOUR PATH>/log.txt" 2>&1'
它确实工作得很好,只有当嵌入到另一个软件中时。
此外,我尝试过在名称中不使用白色 space 并从根文件夹调用脚本而无需指定路径。
知道为什么它不适用于 R-4.* 甚至更好,如何解决它?
谢谢!
PD:Martin,谢谢你的提示,让我提出了一个更好的问题
好吧,在一种情况下 (3.6.0),您使用 R.exe
,在另一种情况 (4.0.3) 中,您使用 Rscript.exe
。
R 和 Rscript 都存在了很长时间,并且它们的行为总是略有不同。
您真的不应该将它们相互混淆(即使在 Windows 上,我明白了,它们看起来像是同一个文件..它们 不会 表现一样)。
好的,现在您对两者都使用 R.exe
。
只是为了了解更多/查看更多可能发生问题的地方,你可以尝试
全部
- 使用最小可重现示例,即我们可以自己直接使用的示例,即不使用
"<YOUR PATH>"
(也不setwd(.)
)
- 不 使用带有
' '
(space) 的文件名,即,例如,使用 code.R
- 从“终端”调用它/shell 而不是作为 python 子进程?
最后但同样重要的是:是的,对于 R 4.0.0,使用了一个完全更新的工具集(“全新的工具链”,例如更新得多的 C 编译器)来为 windows、“Rtools 4.0”构建 R或 rtools40
: https://cran.r-project.org/bin/windows/Rtools/ 。所以变化是意料之中的,但通常只会变得更好,而不是更糟..
帮助人们解决了这个问题,谢谢,Duncan Murdoch!
解决方案 1:
import os
pth = "<YOUR PATH>"
os.system(" ".join(["C:/R/R-4.0.3/bin/x64/R.exe", "-f", '"' + pth + '/RCode.R"', "--args",
'"' + pth + '/arguments.txt"',"1>", '"' + pth + '/log.txt"']))
解决方案 2:
import subprocess
pth = "<YOUR PATH>"
subprocess.call(" ".join(["1>", '"' + pth + '/log.txt"', "2>&1",
"C:/R/R-4.0.3/bin/x64/R.exe", "-f", '"' + pth + '/RCode.R"', "--args",
'"' + pth + '/arguments.txt"']), shell = True)
我正在从 python 调用一个名为 R Code.R:
的非常简单的 R 脚本args <- commandArgs(TRUE)
print(args)
source(args[1])
setwd("<YOUR PATH>")
output <- head(mtcars, n = n)
write.table(output, "output.txt")
使用以下脚本:
import subprocess
pth = "<YOUR PATH>"
subprocess.call(" ".join(["C:/R/R-3.6.0/bin/x64/R.exe", "-f", '"' + pth + '/R Code.R"', "--args",
'"' + pth + '/arguments.txt"',"1>", '"' + pth + '/log.txt"', "2>&1"]))
subprocess.call(" ".join(["C:/R/R-4.0.3/bin/x64/R.exe", "-f", '"' + pth + '/R Code.R"', "--args",
'"' + pth + '/arguments.txt"',"1>", '"' + pth + '/log.txt"', "2>&1"]))
其中 arguments.txt 包含: n <- 10
问题是当我使用 R-4.0.3 时,log.txt 文件没有生成,我需要转储一个日志文件,因为它会在我的后续过程中自动寻找它。
当我在 CMD (Windows) 中执行以下命令时:
C:/R/R-4.0.3/bin/x64/R.exe -f "<YOUR PATH>/R Code.R" --args "<YOUR PATH>/arguments.txt" 1> "<YOUR PATH>/log.txt" 2>&1'
它确实工作得很好,只有当嵌入到另一个软件中时。
此外,我尝试过在名称中不使用白色 space 并从根文件夹调用脚本而无需指定路径。 知道为什么它不适用于 R-4.* 甚至更好,如何解决它?
谢谢!
PD:Martin,谢谢你的提示,让我提出了一个更好的问题
好吧,在一种情况下 (3.6.0),您使用 R.exe
,在另一种情况 (4.0.3) 中,您使用 Rscript.exe
。
R 和 Rscript 都存在了很长时间,并且它们的行为总是略有不同。
您真的不应该将它们相互混淆(即使在 Windows 上,我明白了,它们看起来像是同一个文件..它们 不会 表现一样)。
好的,现在您对两者都使用 R.exe
。
只是为了了解更多/查看更多可能发生问题的地方,你可以尝试
全部
- 使用最小可重现示例,即我们可以自己直接使用的示例,即不使用
"<YOUR PATH>"
(也不setwd(.)
) - 不 使用带有
' '
(space) 的文件名,即,例如,使用code.R
- 从“终端”调用它/shell 而不是作为 python 子进程?
最后但同样重要的是:是的,对于 R 4.0.0,使用了一个完全更新的工具集(“全新的工具链”,例如更新得多的 C 编译器)来为 windows、“Rtools 4.0”构建 R或 rtools40
: https://cran.r-project.org/bin/windows/Rtools/ 。所以变化是意料之中的,但通常只会变得更好,而不是更糟..
帮助人们解决了这个问题,谢谢,Duncan Murdoch!
解决方案 1:
import os
pth = "<YOUR PATH>"
os.system(" ".join(["C:/R/R-4.0.3/bin/x64/R.exe", "-f", '"' + pth + '/RCode.R"', "--args",
'"' + pth + '/arguments.txt"',"1>", '"' + pth + '/log.txt"']))
解决方案 2:
import subprocess
pth = "<YOUR PATH>"
subprocess.call(" ".join(["1>", '"' + pth + '/log.txt"', "2>&1",
"C:/R/R-4.0.3/bin/x64/R.exe", "-f", '"' + pth + '/RCode.R"', "--args",
'"' + pth + '/arguments.txt"']), shell = True)