Running R Scripts With Windows Task Scheduler - r

I am attempting to run a saved R script using Windows Task Scheduler. When I run it it fails. In an attempt to troubleshoot the problem I created a small test R script
test <- function(){
print("test")
}
test()
I then create a small bat file with
"C:\R\R-3.4.4\bin\R.exe" "C:\Test.R"
When I attempt to manually run that .bat file R in the cmd line opens but its initial message is
"C:\R\R-3.4.4\bin\R.exe" "C:\Test.R"
ARGUMENT "C:\Test.R" __ignored__
and the script does not run.

Related

Running several R scripts at once using .bat file with CMD BATCH?

I wanted to use a Windows .bat file to run several R scripts and schedule it using windows scheduler.
However, when I test using windows scheduler, everything executes but the files that the R scripts should create are never created. Instead, when I double-click on the .bat file itself it works fine. My goal is for several scripts to just run overnight without me going in and running each one of them manually one-by-one.
If I were to add a second line to the .bat file that would make it run a second script, would this execute only after the first is complete? If not, would I be able to delay the second until the first finishes somehow?? For instance, my .bat file looks like this:
"C:\Program Files\R\R-3.4.2\bin\x64\R.exe" CMD BATCH C:\Users\gma\Desktop\R_Task\script1.R
"C:\Program Files\R\R-3.4.2\bin\x64\R.exe" CMD BATCH C:\Users\gma\Desktop\R_Task\script2.R
I used the answer provided by #Gautam (R taskscheduleR not executing script) to get this far

Scheduling R scripts in Task Scheduler - CMD runs, but task does not complete

I am trying to automate R scripts in Windows Task Scheduler. I've finally managed to get the program to run, sort of, but it doesn't complete its task.
When task scheduler runs, the CMD windows pops up and I can see it installing the necessary packages to run the script, but the task doesn't actually complete. It is supposed to update a spreadsheet, which works when I run the script in R Studio but does not work when I run it through Task Scheduler.
I am running the script through Windows Task Scheduler as follows:
Action: Start a Program
Program/script:"C:\R-4.0.3\bin\Rscript.exe"
Add arguments: "C:\Documents\Options-Measurement.R"
This may be related to the working directory. Could you please add something like print(getwd()) to your script and check if it is the desired working directory?

Return R Script Log From Scheduling

I have scheduled an RScript to run via the TaskScheduler but there seems to be an error occurring in the code which does not occur when I run the code manually.
Is there a way I can output the RScript log into a separate file so I can then debug?

How to combine R script, IF statement, and Pause code?

I am running a batch file (.bat) to run an R script which works fine. My batch file is ran daily with my task manager. What I am trying to do now is figure out how to pause my batch file if the condition in R script stops the R script.
My batch file:
#echo off
"C:\Program Files\R\R-3.5.2\bin\Rscript.exe" C:\Auto_R_Reports\Codes\SICK_WTGs.R
My R script:
if(NROW(Sick_WTG_USA) == 0){
stop('NO SICK WTGs')
} else {source("./Codes/2WKcheck.R")}
The idea is that when the if condition in R script evaluates true and my R script stops, I also want my batch file to pause so that I know it stopped and paused.

Error in scheduling R script

We are trying to schedule an R script using windows task scheduler.
location of R C:\Program Files\R\R-3.1.0\bin\R.exe
Location of my script D:\K-exercise\k-demo.R
These are the steps that we are following:
Task scheduler--Actions--new--program script--location of cmd.exe--arguments--"C:\Program Files\R\R-3.1.0\bin\R.exe" CMD BATCH "D:\K-exercise\k-demo.R"--triggered the time
Command prompt is opening but is not sure whether the script it is running or not?
We are not able to see any output.
Can someone help here?

Resources