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
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.
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.
Related
I'm trying for my first time to run an R script from command line on WINDOWS to automate calculation with another program. So I made my R script using R studio and saved it a .bat file with this line
C:\Program Files\R\R-4.0.2\bin CMD BATCH C:\my_directory\my_script.R
Then I putted the script and files the script have to be executed on in the same directory, the one that contains files to run also the program I need. I came throught command line in that directory and I executed the batch file but it doesn't work. I have this as a error message:
"C:\Program Files\R\R-4.0.2\bin"it is not recognized as an internal or external command,
an executable program or batch file.
Where I am wrong? thank you!
You should use Rscript instead, assuming its in your PATH.
E.g.
Rscript C:\my_directory\my_script.R
Or a single expression
Rscript -e "print(123)"
I'm trying to run a R script through a .bat file, and I only managed to open the file and not to run the script.
These lines are what I use in the .bat file.
"C:\Program Files\R\R-3.3.3\bin\Rscript.exe"
"C:\Users\Documents\Script\Script1d.R" --vanilla --slave
What am I missing here?
I am trying to run a .bat file, which does run perfectly when I double click in it (Windows OS), but fails when I try to run it in R
com <- "C:\\SASLocal\\RUN3614\\56a8c11b-84b2-4af7-a155-01190936b1c1\\M1_superGOtest.bat"
system(com)
I am getting back a message of had status 2
Just an FYI, this triggers a SAS program, which I need to run in SAS as it is for comparison purposes between SAS and R.
In Windows, to run batch files from command line you need to call a command line interpreter, Command Prompt or PowerShell, passing batch file as an argument.
A .bat script by itself is like an .R script and does not do anything until an executable runs it (i.e., Rscript.exe, R.exe, Rcmd.exe, Rterm.exe) and in this case, cmd.exe and powershell.exe:
# COMMAND PROMPT
system('cmd /c "C:\\SASLocal\\RUN3614\\56a8c11b-84b2-4af7-a155-01190936b1c1\\M1_superGOtest.bat"')
# POWERSHELL
system('powershell -c & "\'C:\\SASLocal\\RUN3614\\56a8c11b-84b2-4af7-a155-01190936b1c1\\M1_superGOtest.bat\'"')
I'm trying to create a batch file to run an R script for me automatically on Windows 7.
Batch file:
"C:\Program Files\R\R-3.2.3\bin\Rscript.exe" CMD BATCH "C:\Users\<my username>\Documents\R\Script Testing\script.R"
pause
R script:
write.csv("hello", "automatic_output.txt")
This script works when I run it in R, but when I try to run the .bat, I get this error:
Fatal error: cannot open file 'CMD': No such file or directory
This happens regardless of whether I paste the code into the command prompt, run the .bat as administrator, or schedule the task.
I tried following the advice given in this thread:
Exporting .csv from R & Batch file
but am seeing the same behavior.
I also used the Windows search to try to find "automatic_output.txt" in case it was being stored in an unexpected location but no results came up.
Lastly,
https://stackoverflow.com/a/23514987 seemed to suggest that I could omit the "CMD BATCH", and when I do I no longer get an error, but automatic_output.txt is still not produced.
Any ideas?
As Thomas pointed out, I needed to change "Rscript.exe" to "R.exe". For future googlers, here is the line of code that worked:
"C:\Program Files\R\R-3.2.3\bin\R.exe" CMD BATCH "C:\Users\<my username>\Documents\R\Script Testing\script.R"
I have some scripts that I run using R's batch mode.
/usr/bin/R CMD BATCH --vanilla --no-timing ~/scripts/R/sess_dur.R
I redirect the output to a file using:
> sink("~/scripts_output/R_output.txt",append=TRUE)
The problem is that when I run this script, files are created with the same name of the script and the "out" suffix (sess_dur.Rout).
There is some way to tell R not to generate these files?
Have you tried something like:
R CMD BATCH --vanilla --no-timing ~/scripts/R/sess_dur.R /dev/null