[英]running .exe from R: status 127 warning on Linux not on Windows
I'm calling an .exe
from R using system("script.exe object")
. 我正在使用
system("script.exe object")
从R调用.exe
。
I get Warning: running command had status 127
. 我得到
Warning: running command had status 127
。 I know it means the .exe
file has not been found. 我知道这意味着没有找到
.exe
文件。
I'm on windows. 我在窗户上。 When I use
shell
instead of system
it works like a charm. 当我使用
shell
而不是system
它就像一个魅力。 However, I am designing a Shiny application that will be deployed in a Linux environment (shinyapps.io). 但是,我正在设计一个将在Linux环境(shinyapps.io)中部署的Shiny应用程序。 This is why I need to use
system
. 这就是为什么我需要使用
system
。
EDIT 编辑
On Windows, it works with system(paste("cmd.exe /c", "script.exe object"), intern = FALSE, wait = TRUE)
as suggested here . 在Windows上,它的工作原理与
system(paste("cmd.exe /c", "script.exe object"), intern = FALSE, wait = TRUE)
的建议在这里 。 But not when I deploy the app (on Linux). 但是当我在Linux上部署应用程序时却没有。
HINT 暗示
Locally on Windows, if I replace system
with system2
: system2(paste("cmd.exe /c", "script.exe object"), wait = TRUE)
, it raises the status 127
warning and the output is exactly the same as in my deployed app on Linux . 在Windows本地上,如果我用
system2
替换system
: system2(paste("cmd.exe /c", "script.exe object"), wait = TRUE)
,它将引发status 127
警告,并且输出与以下内容完全相同在我在Linux上部署的应用程序中 。
It's tough to create a reproducible example here but if needed I can try. 在此处创建可复制的示例很困难,但是如果需要,我可以尝试。 Please tell me.
请告诉我。
Context: basically the .exe
is a black box (compiled C++ code) that takes a .txt file as input and outputs another .txt file. 上下文:基本上,
.exe
是一个黑匣子(编译的C ++代码),它使用.txt文件作为输入并输出另一个.txt文件。 I am using R to dump the .txt file to the current working directory, then read back in the .txt file generated by the .exe
(created in the current working directory, where the .exe
file is stored). 我正在使用R将.txt文件转储到当前工作目录,然后读回
.exe
生成的.txt文件(在当前工作目录中创建,该文件存储.exe
文件)。
Just add \\"
could solve you problem, eg 只需添加
\\"
就可以解决您的问题,例如
> 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.
Update: 更新:
The 127 error is usually seen when there is a space in the path. 当路径中有空格时,通常会看到127错误。 One also needs to worry about the input of the application, eg
"/path A/A 2" --in-path "/home/A/BC/d 123.dta"
. 还需要担心应用程序的输入,例如
"/path A/A 2" --in-path "/home/A/BC/d 123.dta"
。 Here are some update comments: 以下是一些更新注释:
system(shQuote(paste0(getwd(),"/Hi 2.exe")))
is much more convenient. system(shQuote(paste0(getwd(),"/Hi 2.exe")))
更加方便。 system()
recommends to use system2()
instead to avoid path problem under Win/Linux/OSX/. system()
手册建议改用system2()
以避免Win / Linux / OSX /下的路径问题。 Update 2: 更新2:
For Linux user, I created a function to detect the given file in your working directory is executable or not: 对于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);
}
I've tested it on GNU awk/grep. 我已经在GNU awk / grep上对其进行了测试。 The 2/5/8 indicates the executable permission of [u/2]ser, [g/5]roup, [o/8]thers., one could change it to meet the requirement.
2/5/8表示[u / 2] ser,[g / 5]组合,[o / 8]等的可执行许可,可以对其进行更改以满足要求。
The problem actually stemmed from the fact that .exe
files are executables for Windows only. 问题实际上源于
.exe
文件仅是Windows可执行文件的事实。 It does not work out of the box on Linux environments (you can use WINE but in my case it is not possible because I am calling the executable from within R, I don't have any sudo
rights or anything on the virtual machine used by the host of my app). 它在Linux环境中不是开箱即用的(您可以使用WINE,但在我的情况下是不可能的,因为我是从R内部调用可执行文件的,我在使用的虚拟机上没有任何
sudo
权限或任何权限。我的应用的主机)。 So I compiled the c++ code I had using g++ on a Linux virtual machine and used the .out
file rather than the .exe
. 因此,我在Linux虚拟机上编译了使用g ++编写的c ++代码,并使用了
.out
文件而不是.exe
文件。
Then in my R script I just needed these two calls: 然后在我的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
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.