0

I would like to invoke R script by using a .bat file. For testing, I saved an R file on the desktop in the name print_test.R The code in this file is:

p<-500
write.csv(p,"print.csv")

I wrote the .bat file :

@echo off
R CMD BATCH C:\Users\User\Desktop\print_test.R

UPDATE: After adding pause to the .bat file I got the following text:

"'R' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . ."

When I click on the bat file (also saved on the desktop) there is a blink of the CMD window but the print.csv file is not created.What is wrong?

mql4beginner
  • 2,193
  • 5
  • 34
  • 73

2 Answers2

1

Consider using the automated utility, Rscript.exe, as the R CMD BATCH might be considered a legacy command. See post: R.exe, Rcmd.exe, Rscript.exe and Rterm.exe: what's the difference?

Rscript as an environment PATH variable:

@echo off
Rscript "C:\Users\User\Desktop\print_test.R"

Rscript not as a environment PATH variable (where you specify full path of the executable -usually in bin folder of installation):

@echo off
"C:\Path\To\Rscript" "C:\Users\User\Desktop\print_test.R"

OR if paths do not have spaces:

@echo off
C:\Path\To\Rscript C:\Users\User\Desktop\print_test.R
Community
  • 1
  • 1
Parfait
  • 104,375
  • 17
  • 94
  • 125
0

I followed Sumedh's tip : here is the new code:

@echo off
"C:\Program Files\R\R-3.2.4\bin\x64\r.exe" CMD BATCH  "C:\Users\User\Desktop\print_test.R"
mql4beginner
  • 2,193
  • 5
  • 34
  • 73