这里结合了令人沮丧的问题。本质上,我希望 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()
相关命令中的空格,或者如何调用程序?