0

I'm calling an .exe from R using system("script.exe object").

I get Warning: running command had status 127. I know it means the .exe file has not been found.

I'm on windows. When I use shell instead of system it works like a charm. However, I am designing a Shiny application that will be deployed in a Linux environment (shinyapps.io). This is why I need to use system.

EDIT

On Windows, it works with system(paste("cmd.exe /c", "script.exe object"), intern = FALSE, wait = TRUE) as suggested here. But not when I deploy the app (on 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.

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. 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).

Community
  • 1
  • 1
Antoine
  • 1,649
  • 4
  • 23
  • 50

2 Answers2

1

Just add \" could solve you problem, e.g.

> 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. One also needs to worry about the input of the application, e.g. "/path A/A 2" --in-path "/home/A/B C/d 123.dta". Here are some update comments:

  1. system(shQuote(paste0(getwd(),"/Hi 2.exe"))) is much more convenient.
  2. At least in R 3.2.4, the manual of system() recommends to use system2() instead to avoid path problem under Win/Linux/OSX/.

Update 2:
For Linux user, I created a function to detect the given file in your working directory is executable or not:

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. 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.

yfyang
  • 220
  • 2
  • 7
  • thanks for your input. let me try and I'll get back to you – Antoine Apr 05 '16 at 18:35
  • thanks for your help, but unfortunately none of your proposed solutions work for me. I will try to create a reproducible example and post it tomorrow – Antoine Apr 05 '16 at 20:52
1

The problem actually stemmed from the fact that .exe files are executables for Windows only. 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). So I compiled the c++ code I had using g++ on a Linux virtual machine and used the .out file rather than the .exe.

Then in my R script I just needed these two calls:

system("chmod a+x script.out") # to make Linux understand that the file is an executable system("./script.out object") # to run the script

Antoine
  • 1,649
  • 4
  • 23
  • 50
  • 1
    It is good to see your application works. There is a wrapped build-in function `Sys.chmod`, which does (almost) the same thing. – yfyang Apr 06 '16 at 14:33
  • thank you! in case you're interested this is the [app](https://safetyapp.shinyapps.io/GoWvis/). The c++ code is used for k_truss decomposition (under graph - mining - degeneracy) – Antoine Apr 06 '16 at 14:36
  • This advice worked great for me, thanks! Like @yfyang said, the Sys.chmod function makes it bit neater but does the same thing – RDRR Aug 29 '18 at 08:46