[英]running .exe from R: status 127 warning on Linux not on Windows
我正在使用system("script.exe object")
从R调用.exe
。
我得到Warning: running command had status 127
。 我知道这意味着没有找到.exe
文件。
我在窗户上。 当我使用shell
而不是system
它就像一个魅力。 但是,我正在设计一个将在Linux环境(shinyapps.io)中部署的Shiny应用程序。 这就是为什么我需要使用system
。
编辑
在Windows上,它的工作原理与system(paste("cmd.exe /c", "script.exe object"), intern = FALSE, wait = TRUE)
的建议在这里 。 但是当我在Linux上部署应用程序时却没有。
暗示
在Windows本地上,如果我用system2
替换system
: system2(paste("cmd.exe /c", "script.exe object"), wait = TRUE)
,它将引发status 127
警告,并且输出与以下内容完全相同在我在Linux上部署的应用程序中 。
在此处创建可复制的示例很困难,但是如果需要,我可以尝试。 请告诉我。
上下文:基本上, .exe
是一个黑匣子(编译的C ++代码),它使用.txt文件作为输入并输出另一个.txt文件。 我正在使用R将.txt文件转储到当前工作目录,然后读回.exe
生成的.txt文件(在当前工作目录中创建,该文件存储.exe
文件)。
只需添加\\"
就可以解决您的问题,例如
> setwd("W:/www/ADemo/")
> system(paste0(getwd(),"/Hi 2.exe"))
Hello, world.
> setwd("W:/www/A Demo/")
> system(paste0(getwd(),"/Hi 2.exe"))
Warning message:
running command 'W:/www/A Demo/Hi 2.exe' had status 127
> system(paste0("\"",getwd(),"/Hi 2.exe","\" "))
Hello, world.
更新:
当路径中有空格时,通常会看到127错误。 还需要担心应用程序的输入,例如"/path A/A 2" --in-path "/home/A/BC/d 123.dta"
。 以下是一些更新注释:
system(shQuote(paste0(getwd(),"/Hi 2.exe")))
更加方便。 system()
手册建议改用system2()
以避免Win / Linux / OSX /下的路径问题。 更新2:
对于Linux用户,我创建了一个函数来检测您的工作目录中的给定文件是否可执行:
chkpermission<-function(file, mode='0777'){
exe_list <- system("echo `ls -l | grep -E ^-.{2}x\\|^-.{5}x\\|^-.{8}x` | awk '{print $9}'", intern=T)
if(length(exe_list)==0){
stop("no file is executable");
##Make sure you know what you are doing here, add a+x permission:
## if (!(file%in%exe_list)) Sys.chmod(file, mode = mode)
}
return(file%in%exe_list);
}
我已经在GNU awk / grep上对其进行了测试。 2/5/8表示[u / 2] ser,[g / 5]组合,[o / 8]等的可执行许可,可以对其进行更改以满足要求。
问题实际上源于.exe
文件仅是Windows可执行文件的事实。 它在Linux环境中不是开箱即用的(您可以使用WINE,但在我的情况下是不可能的,因为我是从R内部调用可执行文件的,我在使用的虚拟机上没有任何sudo
权限或任何权限。我的应用的主机)。 因此,我在Linux虚拟机上编译了使用g ++编写的c ++代码,并使用了.out
文件而不是.exe
文件。
然后在我的R脚本中,我只需要以下两个调用:
system("chmod a+x script.out") # to make Linux understand that the file is an executable
system("./script.out object") # to run the script
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.