Scheduling an R Script - r

My team and I are using a .bat file
#echo off
"C:\Program Files\RStudio\bin\rstudio.exe" R CMD BATCH --vanilla --slave "C:\Users(user name)\Hello.R"
to execute a script that reads...
print("Hello World!")
When we run the .bat file, Rstudio opens, it presents the script, but it does not run the script automatically to present the final result in the counsel.
What am I doing wrong?
Thanks

Related

Error in .BAT file R Script

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.

Running R script through .bat file on windows 8.1?

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?

Run multiple R scripts concurrently through a batch file

I'm trying to follow the conventional method to trigger R scripts through batch like
RScript Example.R
but what i look to is some way to run multiple R scripts through a batch file.
I tried to do use Start command to open multiple sessions but that doesn't work either. (RScript START ex1.R START ex2.R)
PS complete noob to batch files.
On Windows - if you want to run them in parallel make sure you add start in yoyr batch (.bat) script. Otherwise Example2.R waits for Example1.R to complete etc.
start RScript Example1.R
start RScript Example2.R
...
If you're using sh to launch your scripts, this could do it.
cd /path_to_script1/
sh script1.sh &
cd /path_to_script2/
sh script2.sh &
cd /path_to_script3/
sh scipt2.sh &
This launches parallel R sessions (one for each script) so careful with memory and CPU use. Each script file contains Rscript command.
Simply save the RScript commands in a Windows batch file (.bat) then double-click the file in directory or call it via command line. Below assumes RScript is an environment variable.
Batch file (type below in Notepad and save with extension .bat, not default .txt)
cd "C:\Path\To\Scripts"
RScript Example1.R
RScript Example2.R
RScript Example3.R
RScript Example4.R
RScript Example5.R
CMD Command line
call myRScriptBatchFile.bat
PowerShell Command line
cmd.exe /c myRScriptBatchFile.bat

Running a .bat file from R - had status 2

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\'"')

R Batch Mode - Suppress output file

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

Resources