R system functions always returns error 127
Asked Answered
C

7

19

I need to execute an external tool from R and process errors (if any) occurred in that tool. I know 3 functions to do something familiar with my task:

shell, system and system2.

Trying to test those, I see that command

shell("notepad") 

opens notepad. As far as I know shell doesn't allow to check errors (there's no interface to look into stderr).

When I call

system("notepad")

or

system2("notepad") 

R freezes trying to make those commands.

Calling

system("start notepad") 

or

system2("start notepad") 

returns warning

Warning message:
running command '"start notepad"' had status 127 
Crenshaw answered 11/11, 2015 at 8:37 Comment(6)
The problem boils down to notepad not being in the PATH and therefore is not visible to R for executing it. On my machine, I can call system("java -version") with no trouble. Strangely, I could not get Notepad to launch from R even after adding it to my PATH.Diastyle
I can also get system("git version") to work. There may be an issue with non command line programs in R. Why do you want to launch Notepad from R may I ask?Diastyle
> system("git version") Warning message: running command 'git version' had status 127Crenshaw
Yes, because git is not on your PATH. Type echo %PATH% from a command prompt and you will see this.Diastyle
shell("notepad") opens notepad, so notepad is in my PATHCrenshaw
Yes, this just worked for me as well. It seems like system() cannot launch a new window. So it hangs for Notepad.Diastyle
D
6

As I mentioned in my comments, the R documentation reveals that in Windows the system() function does not launch a separate shell (if needed). This is why command line commands run with system(), but Notepad, which needs a separate window, does not run:

From the documentation for system():

The most important difference is that on a Unix-alike system launches a shell which then runs command. On Windows the command is run directly – use shell for an interface which runs command via a shell (by default the Windows shell cmd.exe, which has many differences from a POSIX shell).

Diastyle answered 11/11, 2015 at 9:3 Comment(0)
D
24

Adapting @DavidTseng's answer (sorry for not having enough reputation to upvote it)...

system("cmd.exe", input = "notepad")

worked for me in Windows.

Dorwin answered 19/11, 2016 at 16:29 Comment(1)
This is very useful in several ways - for instance, system("cmd.exe", input = "tree > Tree.txt /A /F") will make a file Tree.txt containing a directory tree with all folders, subfolders and files.Cowberry
D
6

As I mentioned in my comments, the R documentation reveals that in Windows the system() function does not launch a separate shell (if needed). This is why command line commands run with system(), but Notepad, which needs a separate window, does not run:

From the documentation for system():

The most important difference is that on a Unix-alike system launches a shell which then runs command. On Windows the command is run directly – use shell for an interface which runs command via a shell (by default the Windows shell cmd.exe, which has many differences from a POSIX shell).

Diastyle answered 11/11, 2015 at 9:3 Comment(0)
M
4
system("bash -l", input = "notepad")
Margalo answered 9/11, 2016 at 12:59 Comment(0)
H
1

I'm not sure if there's been an update to R that allows this since the question was asked nearly four years ago, but system("\"C:\path\to\exe.exe\" args", intern = T) works for me and WILL bring up a separate child window and works on Windows 10 + R 3.6 + RStudio.

Not using the 'intern = T' was giving me a return code of 127 and did not run the process.

Herculaneum answered 23/8, 2019 at 13:43 Comment(0)
M
1

I had the same issue. there is an additional step in the installation process which i did not do.

refer to to the url

https://cran.r-project.org/bin/windows/Rtools/

Look for "Putting Rtools on the PATH"

writeLines('PATH="${RTOOLS40_HOME}\usr\bin;${PATH}"', con = "~/.Renviron")

Multifoil answered 21/5, 2020 at 10:47 Comment(0)
U
0

for windows users wrong: system(path("c:", "program files", "r", "anysoft.EXE")) but works : system(path("c:", shQuote("program files"), "r", "anysoft.EXE"))

Ulises answered 13/7, 2018 at 11:47 Comment(0)
T
-3

You guys are making it so complicated. I solved this problem by referring to this answer. The problem is with the PATH. type Sys.which('') in R, and you will see nothing. So you have to set the path in CMD, and then use Sys.setenv(PATH = '') in R to get this work.

Theophylline answered 11/8, 2018 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.