[英]R system functions always returns error 127
I need to execute an external tool from R
and process errors (if any) occurred in that tool.我需要从R
执行外部工具并处理该工具中发生的错误(如果有)。 I know 3 functions to do something familiar with my task:我知道 3 个函数可以做一些熟悉我的任务的东西:
shell, system and system2.
Trying to test those, I see that command试图测试这些,我看到了那个命令
shell("notepad")
opens notepad.打开记事本。 As far as I know shell doesn't allow to check errors (there's no interface to look into stderr
).据我所知,shell 不允许检查错误(没有界面可以查看stderr
)。
When I call当我打电话
system("notepad")
or或者
system2("notepad")
R
freezes trying to make those commands. R
冻结尝试发出这些命令。
Calling打电话
system("start notepad")
or或者
system2("start notepad")
returns warning返回警告
Warning message:
running command '"start notepad"' had status 127
Adapting @DavidTseng's answer (sorry for not having enough reputation to upvote it)...调整@DavidTseng 的答案(抱歉没有足够的声誉来支持它)...
system("cmd.exe", input = "notepad")
worked for me in Windows.在 Windows 中为我工作。
As I mentioned in my comments, the R documentation reveals that in Windows the system()
function does not launch a separate shell (if needed).正如我在评论中提到的,R 文档显示在 Windows 中system()
函数不会启动单独的 shell(如果需要)。 This is why command line commands run with system()
, but Notepad, which needs a separate window, does not run:这就是为什么命令行命令使用system()
运行,但需要单独窗口的记事本不运行:
From the documentation for system()
:从system()
的文档中:
The most important difference is that on a Unix-alike system launches a shell which then runs command.最重要的区别是在类 Unix 系统上启动一个 shell,然后运行命令。 On Windows the command is run directly – use shell for an interface which runs command via a shell (by default the Windows shell cmd.exe, which has many differences from a POSIX shell).在 Windows 上,命令直接运行——使用 shell 作为通过 shell 运行命令的接口(默认情况下,Windows shell cmd.exe,它与 POSIX shell 有很多不同)。
system("bash -l", input = "notepad")
I'm not sure if there's been an update to R that allows this since the question was asked nearly four years ago, but system("\\"C:\\path\\to\\exe.exe\\" args", intern = T)
works for me and WILL bring up a separate child window and works on Windows 10 + R 3.6 + RStudio.我不确定 R 是否有更新允许这样做,因为这个问题是在大约四年前提出的,但是system("\\"C:\\path\\to\\exe.exe\\" args", intern = T)
对我有用,将打开一个单独的子窗口,并在 Windows 10 + R 3.6 + RStudio 上工作。
Not using the 'intern = T' was giving me a return code of 127 and did not run the process.不使用 'intern = T' 会给我一个 127 的返回码并且没有运行该过程。
I had the same issue.我遇到过同样的问题。 there is an additional step in the installation process which i did not do.安装过程中还有一个我没有做的额外步骤。
https://cran.r-project.org/bin/windows/Rtools/ https://cran.r-project.org/bin/windows/Rtools/
writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con = "~/.Renviron") writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con = "~/.Renviron")
对于 Windows 用户错误: system(path("c:", "program files", "r", "anysoft.EXE"))
但有效: system(path("c:", shQuote("program files"), "r", "anysoft.EXE"))
You guys are making it so complicated.你们把事情搞得这么复杂。 I solved this problem by referring to this answer .我通过参考这个答案解决了这个问题。 The problem is with the PATH.问题出在 PATH 上。 type Sys.which('')
in R, and you will see nothing.在 R 中输入Sys.which('')
,你将什么也看不到。 So you have to set the path in CMD, and then use Sys.setenv(PATH = '')
in R to get this work.所以你必须在 CMD 中设置路径,然后在 R 中使用Sys.setenv(PATH = '')
来完成这项工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.