【发布时间】:2018-02-27 18:46:02
【问题描述】:
我需要从 R 会话发出 unix 命令。我在使用 RStudio 1.1.383 和 R 3.4.3 的 Windows R2 2012 服务器上。
shell() 函数看起来对我来说是正确的,但是当我指定 bash shell 的路径(从 Git for 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 将输出作为 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
```
【问题讨论】:
标签: r bash rstudio knitr git-bash