4

I'm a novice when it comes anything outside of coding for R. I've looked up many similar posts but they aren't detailed enough as I'm unfamiliar with many of the terms.

I have a simple R script that's saved on the C: drive. I want it to run from a .bat file for now, which I will later use in the task scheduler to run automatically everyday once it's working.

I saw many posts pointed to this link.

The link mentions the working directory but then never mentions anything it again. How does windows command know the folder of the .R script if it's never seems to be entered? Right now I've changed the working directory:

cd C:\Program Files\R\R-3.0.1\bin\x64

I don't know where to go from there.

Edit: I'm running windows 7

rcs
  • 67,191
  • 22
  • 172
  • 153
John M.
  • 323
  • 2
  • 5
  • 13

1 Answers1

5

On Linux:

R CMD BATCH [options] my_script.R [outfile]

On Microsoft Windows (adjust the path to R.exe as needed):

"C:\Program Files\R\R-2.13.1\bin\R.exe" CMD BATCH 
   --vanilla --slave "c:\my projects\my_script.R"

From here.

americo
  • 1,013
  • 8
  • 17
  • 1
    This works, thanks! I had read that CMD BATCH wasn't being used in favor of Rscript.exe as noted here: http://stackoverflow.com/questions/3506007/running-r-code-from-command-line-windows – John M. Nov 12 '13 at 16:23
  • So this works in the command line, how would I transfer to a .bat file? I know how to create a .bat file, just don't know what to put in it. – John M. Nov 12 '13 at 16:34
  • Take a look at this question: http://stackoverflow.com/questions/17811871/run-r-script-from-bat-batch-file – americo Nov 12 '13 at 17:08
  • In order to run Rscript you need your scrip to be not interactive, take a look at this to get an idea: http://www.statmethods.net/interface/io.html – americo Nov 12 '13 at 17:20
  • Does anyone know how to run CMD BATCH to call a project using `renv?` I've seen this done using RScript.exe, but not with CMD BATCH. – StatsStudent Dec 21 '20 at 22:56