1

I have a .bat file as follows. task.bat

@echo off
R CMD BATCH C:\Users\Raghavan\Desktop\MyTask.R

The file needs to run an R script. The following is the R script.

 A<-read.csv("C:\Users\Raghavan\Desktop\A.csv")
 write.csv(A, file = "B.csv")

The R script runs by itself. However, when I try running it through the .bat file or through windows scheduler, the script fails to run. I have tried many sites.I request someoen to see if this error can be fixed. Most likely, the error is in the .bat file

4
  • 1
    In all probability, it's a matter of where the current directory is. Try inserting, after the @echo off, a line cd C:\wherever\the directory\containing\the r executable\is located to change the current directory when R is run. Replace the directory name with wherever the directory is from which you normally run R.
    – Magoo
    Commented Jan 4, 2018 at 7:09
  • @Magoo, current directory as in current working directory. I Will make the change Commented Jan 4, 2018 at 7:13
  • I have made the necessary changes. I have set the working directory to the desktop, placed the two files on the desktop and run the script. The .bat file opens but the script doesn't run. I have reset the working directory as per your request Commented Jan 4, 2018 at 7:25
  • This works. But it needs to be used in conjunction with a changed drive. Commented Jan 4, 2018 at 8:41

2 Answers 2

2

I run my R files with the following command inside of the .bat script:

@echo Off
"C:\Program Files\R\R-3.4.2\bin\R.exe" CMD BATCH --vanilla --slave "C:\File.R"

Define the full paths to executable R and to the file that you wanna run. Hope this helps.

See also ?BATCH and this resource on arguments of R CMD BATCH.

1
  • 1
    This works. Also I was helped by shifting the files to D drive. C was write protected. Commented Jan 4, 2018 at 8:40
0

I made the following edits to the code.

  @ echo off
  "C:\Program Files\R\R-3.3.2\bin\R.exe"  R CMD BATCH "D:\Newfolder\task1.R"

The files were then shifted to the D drive. This resulted in the code working.

Not the answer you're looking for? Browse other questions tagged or ask your own question.