来自 R 的 UNIX 命令通过 shell 函数
UNIX commands from R via shell function
我需要从 R 会话发出 unix 命令。我在 Windows R2 2012 服务器上使用 RStudio 1.1.383 和 R 3.4.3。
shell()
函数看起来对我来说是正确的,但是当我指定 bash shell 的路径时(从 Git 到 Windows 安装)命令失败,错误代码为 127。
shell_path <- "C:\Program Files\Git\git-bash.exe"
shell("ls -a", shell = shell_path)
## running command 'C:\Program Files\Git\git-bash.exe /c ls -a' had status 127'ls -a' execution failed with error code 127
很确定我的 shell 路径是正确的:
我做错了什么?
编辑:为了清楚起见,我想传递任意数量的 UNIX 命令,我只是使用 ls -a
作为示例。
编辑:
经过一些关于2018-03-09的游戏:
shell(cmd = "ls -a", shell = '"C:/Program Files/Git/bin/bash.exe"', intern = TRUE, flag = "-c")
我的 bash.exe 的正确位置是 .../bin/bash.exe
。这使用 shell
和 intern = TRUE
到 return 作为 R 对象的输出。请注意在 shell 路径两边使用单引号。
编辑:2018-03-09 21:40:46 UT
在 RStudio 中,我们还可以使用 knitr
调用 bash 并设置块选项:
library(knitr)
```{bash my_bash_chunk, engine.path="C:\Program Files\Git\bin\bash.exe"}
# Using a call to unix shell
ls -a
```
也许您需要使用 shQuote
?
shell( paste("ls -a ", shQuote( shell_path) ) )
(未经测试。我不在 Windows 上。但请务必阅读 ?shQuote
))
这里有两点很突出。如果找不到命令,Bash 将 return 退出代码 127;您应该尝试 运行 使用完全限定的命令名称。
我还看到您的 shell 运行 带有 /c
标志。 According to the documentation,flag
参数指定 "the switch to run a command under the shell",它默认为 /c
,但是 "if the shell is bash or tcsh or sh the default is changed to '-c'." 显然,git-bash.exe
不会发生这种情况。
试试这些改变:
shell_path <- "C:\Program Files\Git\git-bash.exe"
shell("/bin/ls -a", shell = shell_path, flag = "-c")
不在 Windows,所以不能确定这是否有效。
如果你只想做ls -a
,你可以使用下面的命令:
shell("'ls -a'", shell="C:\Git\bin\sh.exe")
#or
shell('C:\Git\bin\sh.exe -c "ls -a"')
如果 "Program Files" 中的 space 导致问题,请告诉我们。
如果您需要登录才能调用您的命令,
shell('C:\Git\bin\sh.exe --login -c "ls -a"')
但是,如果您正在考虑从 R 执行 git 命令,ropensci 的 git2r
可能会满足您的需要。
我需要从 R 会话发出 unix 命令。我在 Windows R2 2012 服务器上使用 RStudio 1.1.383 和 R 3.4.3。
shell()
函数看起来对我来说是正确的,但是当我指定 bash shell 的路径时(从 Git 到 Windows 安装)命令失败,错误代码为 127。
shell_path <- "C:\Program Files\Git\git-bash.exe"
shell("ls -a", shell = shell_path)
## running command 'C:\Program Files\Git\git-bash.exe /c ls -a' had status 127'ls -a' execution failed with error code 127
很确定我的 shell 路径是正确的:
我做错了什么?
编辑:为了清楚起见,我想传递任意数量的 UNIX 命令,我只是使用 ls -a
作为示例。
编辑:
经过一些关于2018-03-09的游戏:
shell(cmd = "ls -a", shell = '"C:/Program Files/Git/bin/bash.exe"', intern = TRUE, flag = "-c")
我的 bash.exe 的正确位置是 .../bin/bash.exe
。这使用 shell
和 intern = TRUE
到 return 作为 R 对象的输出。请注意在 shell 路径两边使用单引号。
编辑:2018-03-09 21:40:46 UT
在 RStudio 中,我们还可以使用 knitr
调用 bash 并设置块选项:
library(knitr)
```{bash my_bash_chunk, engine.path="C:\Program Files\Git\bin\bash.exe"}
# Using a call to unix shell
ls -a
```
也许您需要使用 shQuote
?
shell( paste("ls -a ", shQuote( shell_path) ) )
(未经测试。我不在 Windows 上。但请务必阅读 ?shQuote
))
这里有两点很突出。如果找不到命令,Bash 将 return 退出代码 127;您应该尝试 运行 使用完全限定的命令名称。
我还看到您的 shell 运行 带有 /c
标志。 According to the documentation,flag
参数指定 "the switch to run a command under the shell",它默认为 /c
,但是 "if the shell is bash or tcsh or sh the default is changed to '-c'." 显然,git-bash.exe
不会发生这种情况。
试试这些改变:
shell_path <- "C:\Program Files\Git\git-bash.exe"
shell("/bin/ls -a", shell = shell_path, flag = "-c")
不在 Windows,所以不能确定这是否有效。
如果你只想做ls -a
,你可以使用下面的命令:
shell("'ls -a'", shell="C:\Git\bin\sh.exe")
#or
shell('C:\Git\bin\sh.exe -c "ls -a"')
如果 "Program Files" 中的 space 导致问题,请告诉我们。
如果您需要登录才能调用您的命令,
shell('C:\Git\bin\sh.exe --login -c "ls -a"')
但是,如果您正在考虑从 R 执行 git 命令,ropensci 的 git2r
可能会满足您的需要。