1

A combination of frustrating problems here. Essentially I want R to open an external program with command line parameters. I am currently trying to achieve it on a Windows machine, ideally it would work cross-platform.

The program (chimera.exe) is in a directory containing spaces: C:\Program Files\Chimera1.15\bin\ The command line options could be for instance a --nogui flag and a script name, so from the shell I would write (space-specifics aside):

C:\Program Files\Chimera1.15\bin\chimera.exe --nogui scriptfile

This works if I go in windows cmd.exe to the directory itself and just type chimera.exe --nogui scriptfile

Now in R:

I've been playing with shell(), shell.exec(), and system(), but essentially I fail because of the spaces and/or the path separators.

most of the times system() just prints "127" for whatever reason:

> system("C:/Program Files/Chimera1.15/bin/chimera.exe")
[1] 127`

back/forward slashes complicate the matter further but don't make it work:

> system("C:\Program Files\Chimera1.15\bin\chimera.exe")
Error: '\P' is an unrecognized escape in character string starting "C\P"

> system("C:\\Program Files\\Chimera1.15\\bin\\chimera.exe")
[1] 127

> system("C:\\Program\ Files\\Chimera1.15\\bin\\chimera.exe")
[1] 127

> system("C:\\Program\\ Files\\Chimera1.15\\bin\\chimera.exe")
[1] 127

When I install the program in a directory without spaces, it works. How can I escape or pass on the space in system() or related commands or how do I invoke the program otherwise?

1 Answer 1

6

Try system2 as it does not use the cmd line processor and use r"{...}" to avoid having to double backslashes. This assumes R 4.0 or later. See ?Quotes for the full definition of the quotes syntax.

chimera <- r"{C:\Program Files\Chimera1.15\bin\chimera.exe}"
system2(chimera, c("--nogui",  "myscript"))

For example, this works for me (you might need to change the path):

R <- r"{C:\Program Files\R\R-4.1\bin\x64\Rgui.exe}"  # modify as needed
system2(R, c("abc", "def"))

and when Rgui is launched we can verify that the arguments were passed by running this in the new instance of R:

commandArgs()
## [1] "C:\\PROGRA~1\\R\\R-4.1\\bin\\x64\\Rgui.exe"
## [2] "abc"                                       
## [3] "def"   

system

Alternately use system but put quotes around the path so that cmd interprets it correctly -- if it were typed into the Windows cmd line the quotes would be needed too.

system(r"{"C:\Program Files\Chimera1.15\bin\chimera.exe" --nogui myscript}")
Sign up to request clarification or add additional context in comments.

4 Comments

I will try this later; many thanks already. Can you point me to a reference for th r"{}" synthax, haven't seen that before? Any thoughts on why the system() double backslash space escape fails or we be like *shrug* with this command?
Have added responses to the answer.
?Quotes revealed a lot of information that are extremely useful but easily overlooked during earlier days of learning R. Thanks.
Very helpful. Your solution solved for me. I referenced it on a related question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.