Search code examples
rapp-startup

Start RScript without DOS box (on Windows)?


For a tcltk application, I would like to start an R script without opening a console window or starting a DOS box. I already figured out that by creating a link to RScript.exe I can force the console window to start minimized, but I wonder if I can avoid the console window at all?


Solution

  • You want to "run R in batch mode". It's quite straightforward; there are some instructions here.

    EDIT: I don't see a console window; here are the steps I took.

    1) I created a file named r.bat containing the line Rterm --vanilla and saved it in the R startup working directory (as given by Sys.getenv("USERPROFILE")).

    2) I created a test R script, test.r, that would take several seconds to run (so I'd have chance to see any windows popping up.

    n <- 1e3
    for(i in 1:10)
    {
      qr.solve(matrix(runif(n*n), nrow = n), seq_len(n)/(n+1))  
    }
    

    Obviously you can run any script that you like.

    3) I opened a dos command prompt in the same dir as r.bat and typed R <test.r> test.txt.