I am attempting to run an R script using a Windows batch file. From my understanding a batch file is best run using the Rscript.exe
as oppossed to using R.exe
as the former is specifically designed for running R scripts from the command line and doesn’t open the R console, and instead runs the R script and then exits. My problem is that my batch file produces the following error when using Rscript.exe
, Fatal error: cannot open file '-f': No such file or directory
, it however runs with no problems when I use R.exe
instead.
A simple example of the problem I am having. My batch file (saved as: .cmd file type)
@echo off
:: Set code page to UTF-8 for handling special characters
chcp 65001
:: Run Rscript.exe with the correct paths enclosed in quotes
"C:Program FilesRR-4.1.3binRscript.exe" -f --vanilla "C:testprint_test.R"
pause
Where print_test.R
file is simply: print("Test")
When I run this batch file using R.exe
it runs without a problem, only when I am using Rscript.exe
do I get the error message.
Fatal error: cannot open file '-f': No such file or directory
I also tried running directly in the Command Prompt
"C:Program FilesRR-4.1.3binRscript.exe" --vanilla -e "print('Hello World')"
and this runs also with no problem. I think the problem is to do with my batch file connecting Rscript.exe
to my R script file. All file paths are correct.
RotundSlim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2