0

I have Shiny App, one of its functionalities is to run an external script in separate session. I do it with the code:

system2(command = "RScript", args = c("--vanilla",  "external_script.r"), wait = FALSE)

This works fine when the external_script.R script is in the same path as the main application. The problem comes when I want to change the path of this script. I don't know where to put the new path so I can continue using the script. I tried:

system2(command = "RScript", args = c("--vanilla",  file.path("new_path", "external_script.r")), wait = FALSE)

but it failed. Could someone help me do this correctly?

tomsu
  • 371
  • 2
  • 16
  • 1
    Your code should generally work although I would suggest using `file.path(R.home("bin"), "Rscript")` to ensure the correct R is called. And I would also recommend using ‘callr’ unless you want to avoid the extra dependency. (Also, beware that using `--vanilla` disables user configuration, which might render R non-functional; for instance, I set required configuration in my `.Rprofile` file, and running R with `--vanilla` will consequently not work.) – Konrad Rudolph Apr 04 '23 at 13:39
  • 1
    I second the use of `callr`, and offer another alternative: `future`. If the contents of the script are something you already have defined in the local environment, then you can start a (forked) process with the data and function/expressions in an easy step. – r2evans Apr 04 '23 at 13:55
  • @KonradRudolph you are saying that `system2(command = file.path(R.home("bin"), "Rscript"), args = c("--vanilla", file.path("new_path", "external_script.r")), wait = FALSE)` should work? Unfortunately it is not @KonradRudolph , @r2evans about `callr`, I understand you mean `callr::r_bg`? Looks promising, thanks! How is `callr` better than `system2`? – tomsu Apr 04 '23 at 19:58
  • @tomsu Yes, if you copy and paste that exact command into an R session it works. If it does not work for you then you did something else wrong but since you didn’t post any relevant code we cannot help you. – Konrad Rudolph Apr 05 '23 at 07:29

0 Answers0