R system functions always returns error 127

2019-01-15 13:29发布

问题:

I need to execute 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.

Thying 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 

回答1:

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



回答2:

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

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

worked for me in Windows.



回答3:

system("bash -l", input = "notepad")


回答4:

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



回答5:

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.