【发布时间】:2021-06-09 22:27:05
【问题描述】:
这里有一系列令人沮丧的问题。 本质上,我希望 R 使用命令行参数打开一个外部程序。我目前正在尝试在 Windows 机器上实现它,理想情况下它可以跨平台工作。
程序 (chimera.exe) 位于包含空格的目录中:C:\Program Files\Chimera1.15\bin\
例如,命令行选项可以是 --nogui 标志和脚本名称,所以我会从 shell 编写(除了空间细节):
C:\Program Files\Chimera1.15\bin\chimera.exe --nogui scriptfile
如果我在 windows cmd.exe 中进入目录本身并输入 chimera.exe --nogui scriptfile,则此方法有效
现在在 R 中:
我一直在玩 shell()、shell.exec() 和 system(),但基本上我因为空格和/或路径分隔符而失败了。
大多数时候 system() 出于某种原因只打印“127”:
> system("C:/Program Files/Chimera1.15/bin/chimera.exe")
[1] 127`
后/正斜线使问题进一步复杂化,但不能使其发挥作用:
> system("C:\Program Files\Chimera1.15\bin\chimera.exe")
Error: '\P' is an unrecognized escape in character string starting "C\P"
> system("C:\\Program Files\\Chimera1.15\\bin\\chimera.exe")
[1] 127
> system("C:\\Program\ Files\\Chimera1.15\\bin\\chimera.exe")
[1] 127
> system("C:\\Program\\ Files\\Chimera1.15\\bin\\chimera.exe")
[1] 127
当我将程序安装在没有空格的目录中时,它可以工作。如何在system() 或相关命令中转义或传递空格,或者如何调用程序?
【问题讨论】:
标签: r command-line-arguments path-separator