R系统功能始终返回错误127

Pow*_*ool 12 error-handling shell r system system2

我需要执行外部工具R并处理该工具中发生的错误(如果有的话).我知道3个函数可以完成我的任务:

shell, system and system2.
Run Code Online (Sandbox Code Playgroud)

为了测试这些,我看到了那个命令

shell("notepad") 
Run Code Online (Sandbox Code Playgroud)

打开记事本.据我所知,shell不允许检查错误(没有接口可以查看stderr).

我打电话的时候

system("notepad")
Run Code Online (Sandbox Code Playgroud)

要么

system2("notepad") 
Run Code Online (Sandbox Code Playgroud)

stderr 冻结试图制作这些命令.

调用

system("start notepad") 
Run Code Online (Sandbox Code Playgroud)

要么

system2("start notepad") 
Run Code Online (Sandbox Code Playgroud)

返回警告

Warning message:
running command '"start notepad"' had status 127 
Run Code Online (Sandbox Code Playgroud)

小智 14

改编@ DavidTseng的答案(抱歉没有足够的声誉来支持它)......

system("cmd.exe", input = "notepad")
Run Code Online (Sandbox Code Playgroud)

在Windows中为我工作.


Tim*_*sen 5

正如我在评论中提到的那样,R文档揭示了在Windows中,该system()函数不会启动单独的shell(如果需要)。这就是为什么命令行命令运行带有的原因system(),而需要单独窗口的记事本却无法运行:

文档system()

最重要的区别是,在类似Unix的系统上启动外壳程序,然后运行命令。在Windows上,该命令直接运行– 将shell用于通过shell运行命令的界面(默认情况下,Windows shell cmd.exe与POSIX shell有很多区别)。