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

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

Related

Run .R functions via CMD - missing execute all command?

Good day,
I want to automate data cleaning and merging via R. I have created multiple folders to which you upload excel files. Than with R I clean the data and merge them to a final file.
I would like to do this process without running R or Rstudio. To accomplish this I run R via CMD (.bat), however, the code does not run.
In my R script I have multiple functions calling from other files, I assume that I need a simple "Run all command" to execute the code via CMD?
Example of Combine_all_excel_files.R
setwd("C:\\Code source\\")
source("Seperate_input_folder_by_years.R")
Seperate_input_folder_by_years()
Merge data.bat file:
start "" "C:\Program Files\R\R-3.3.3\bin\x64\Rscript.exe" CMD BATCH "C:\Code source\Combine_all_excel_files.R"
pause
When I run the .bad file, the code does not run. What I am doing wrong?

R script to Task Scheduler

I have an R script that gets data from databases on another server and brings it into my database. I have it saved as "dataimport.R"
I followed a few answers from here and from other websites and created a batch file like this:
"C:\Program Files\R\R-3.4.0\bin\R.exe" CMD BATCH --vanilla --slave "C:\dataimport.R"
This is not working. The cmd window opens up but the tables are not recreated and I dont get any error. I wanted to run the Task Scheduler to automate the process. Any ideas on how to fix this?
I kept at it and interestingly the answer to this was this:
"C:\Program Files\R\R-3.4.0\bin\R.exe" "C:\dataimport.R"
I dont know the reason for this but as long as it works.
I've had loads of problems with this, but finally managed to get it to work. To be more comprehensive, here are some of the things I've tried (in case one of these work for other persons):
#echo off, R CMD BATCH C:\myfolder\script.R
R CMD BATCH C:\myfolder\script.R
Using the package taskschedulerR (somehow didn't work overnight)
Used the answer provided above ("C:\Program Files\R\R-3.4.0\bin\R.exe" "C:\dataimport.R")
and all kinds of variations and combinations off these. (can't remember them all exactly)
What finally worked was:
Make an R script and save it (C:\myfolder\Test.R for example)
Through notepad, fill in: "C:\Program Files\R\R-3.5.2\bin\x64\R.exe" CMD BATCH "C:\myfolder\Test.R" (also tried Rscript.exe, didn't work for me).
in Windows Task Scheduler (v1.0) do 'Create task'
fill in time triggers.
in Actions, make an action with Start a program and in the Program/Script line provide the location where your bat script is. C:\myfolder\Test.bat
in the Start in (optional) line: enter C:\myfolder\
Note: both your .bat file and .R script are in the "C:\myfolder" folder.

Automating R to run scripts

I'm basically looking for any way to automatically run R scripts just like it would run as if I was copy and pasting it into console. I've tried the package 'taskscheduleR' however it just seems to output to a log file in the directory which isn't as if I were to just run it inside the Rstudio application.
An example might be, say I want to get the last closing stock prices of 5 stocks each night, then the script in Rstudio and have the variables there and all of the code would be in the script file.
Any thoughts?
I would suggest the in-built Task Scheduler application if you using Windows.
Create a task that will run a batchscript file. This batchscript file has only 1 line which executes the Rscript you want. Set it to run each night (or whatever time you want).
I am not that well-versed in linux and MacOS but here's what I know:
Linux has cron. Add a job to crontab with your preferred timing and execute your script 'path/to/bin/r /path/to/script.r'
MacOS has Automator + iCal (for scheduling). It also has crontab like Linux.

How to use PowerShell to schedule sourcing an R script?

My goal is to use PowerShell to schedule the sourcing of an R script.
My current work flow is that I open RStudio, click the "Source" button in the upper right corner. Then I wait until it's finished, and close RStudio. I change nothing in the R script.
In PowerShell I've been using its Register-ScheduledJob cmdlet to kick off C# programs on a daily schedule. And here's the problem, I can't find an example of effectively using PowerShell to source an R script.
I believe the PowerShell script should probably use the Invoke-Expression cmdlet. But I'm not 100% sure.
To no avail I've tried this:
Start-Process "C:\Program Files\R\R-3.2.4revised\bin\x64\Rterm.exe" -RedirectStandardInput "C:\MyScript.R"
Also, I'd like to avoid the solution that uses CMD BATCH as that's defeating the purpose of using PowerShell.
If just sourcing the R script is what you're looking for then one way to do is something like this
& "C:\Program Files\R\R-3.1.1\bin\Rscript.exe" "C:/Program Files/R/R-3.1.1/tests/demos.R"
where "C:\Program Files\R\R-3.1.1\bin\Rscript.exe" is path Rscript in your local R installation and "C:/Program Files/R/R-3.1.1/tests/demos.R" is path to script you'd normally source() directly in RStudio.
One thing to keep in mind is depending on location of files your R script needs you might need to adjust your script with appropriate setwd()

Run R web scrape daily

I have a script written in R that pulls online and then exports it to a spreadsheet on my computer. I am attempting to create a batch file so that I can set up a scheduled task, but it only opens up the R file without actually running it. The batch file is as follows:
"C:\Program Files\R\R-3.0.2\bin\Rscript.exe" CMD BATCH
"C:\Users\xxx\OneDrive\xxx\Scraper.r"
I am very open to ideas other than creating a batch file, I just want this to work!
Try using this instead (note the x64 addition)
"C:\Program Files\R\R-3.0.2\bin\x64\Rscript.exe" C:\Users\xxx\OneDrive\xxx\Scraper.r
and without the CMD BATCH flags. That works for me.
I am not sure what is going on, but it may be you only have the 64 bit version installed, and you are trying to execute the 32 bit version. But I get a silent fail when I don't have specify the "x64" sub-directory.

Resources