Corentin Darg
Corentin Darg

Reputation: 13

Using system() function in R

I'm running a new script on R and i'm trying to call a file .exe using the function system() from r. I run :

system("C:/Program Files (x86)/OxMetrics6/ox/bin/oxl.exe I:/Code R/GarchOxModelling.ox", show.output.on.console = TRUE, wait = TRUE)

But it seams to do nothing. And when i launch manually the file GarchOxModelling.ox, it works. Would you have any idea on how to make it work from R ?

Thanks in advance

Upvotes: 1

Views: 453

Answers (1)

r2evans
r2evans

Reputation: 160417

Without testing, try

ret <- system(paste(shQuote("C:/Program Files (x86)/OxMetrics6/ox/bin/oxl.exe"),
                    shQuote("I:/Code R/GarchOxModelling.ox")),
              show.output.on.console = TRUE, wait = TRUE)

A few issues with the code you provided in your question:

  1. It is syntactically wrong R:

    "system(C:/Program Fil...", show.output.on.console = TRUE, wait = TRUE)
    

    is like typing in

    "ABC", x=1, y=2)
    

    which should error.

  2. Even assuming that the leading quote is not correct, you need to start the quote at the beginning of the executable name, as in

    system("C:/Program File...", ...)
    
  3. Further, though, is that this is being passed verbatim to the shell. While something windows guesses correctly about embedded spaces, it's really not good practice to assume this can happen all of the time, so you should manually quote all of your arguments that either (a) do include a space in them, or (b) you do not know because they are variables. In this case, I prefer shQuote, but dQuote might be sufficient.

    system(paste(shQuote("C:/Program Files (x86)/OxMetrics6/ox/bin/oxl.exe"),
                 shQuote("I:/Code R/GarchOxModelling.ox")),
           show.output.on.console = TRUE, wait = TRUE)
    
  4. I suggest that you consider using intern=TRUE instead of show.output..., so that you can perhaps programmatically verify the output is what you expect.

  5. Last suggestion, I find the processx package much more reliable for calls like this,

    # library(processx)
    ret <- processx::run("C:/Program Files (x86)/OxMetrics6/ox/bin/oxl.exe", "I:/Code R/GarchOxModelling.ox")
    

    where quoting is handled automatically.

Upvotes: 1

Related Questions