automate tasks in R for Windows 8 platform - r

I would like to have my R script files automatically executed at certain times each day. I have tried to add the script file to the Windows task scheduler but all that does is open up Rstudio and the file but no execution.I followed these steps Scheduling R Tasks via Windows Task Scheduler but this does not work on my computer. When I create the batch file, command won't open it. I need help!

Schedule R scripts/processes with the Windows task scheduler. This allows R users working on Windows to automate R processes on specific timepoints from R itself.
https://github.com/bnosac/taskscheduleR
If you are looking for a Linux/Unix scheduler, you might be interested in the R package cronR available
https://github.com/bnosac/cronR

Related

Automated R script through Task Scheduler not running complete script

I have an R script that I've set up to automatically update every week using Task Scheduler. The R script is supposed to pull out about 10 extracts from Google Analytics, however when I run the script through task scheduler it only extracts 4 and then seemingly stops. If I run it manually in RStudio it runs all the way through with no errors. Does anyone know why it might stop in Task Scheduler?

How to run existing task scheduler's task using Python?

I created a task in my Windows 10 Task Scheduler. I was wondering whether it is possible to run the task using Python. I know it is possible to run the task using CMD (How to run existing windows 7 task using command prompt) but can I somehow do it using Python.

How to access R session/global environment after running script through Rscript on Linux

I am using a Linux Workstation to run my R script. I did so using screen and then Rscript myRscript.R. Is there anyway to access the R session after the script has run? I want to be able to write new commands and access the global environment that was created during that session.
I tried asking the Unix community, but no response...
https://unix.stackexchange.com/questions/608073/how-to-pass-code-to-attached-screen
The session is lost after the script is done running. But you can save the environment using save.image("env_file.Rdata") and use it later using load("env_file.Rdata").
See http://ugrad.stat.ubc.ca/R/library/base/html/save.html

R automatic task scheduling in windows: how to use external functions

I have a scheduled task in windows to run a R program ("ftp.R")
After many attempts and reading many SO literature, I found that the only way to make it work properly was writing this piece of code into a .bat file:
#ECHO OFF
RSCRIPT ftp.R
Everything goes fine except wqith it, when I am trying to use functions that I have in other R programs.
For example, in the "ftp.R" program I have this pice of code:
source("//BCN-01/Server/R/Main/Production/Check_Integrity.R")
In the "Check_Integrity.R" program I have some functions I need to use in "ftp.R".
The thing is that if I execute the .bat file manually, there is no problem, and the "ftp.R" runs perfectly. But if I run exactly the same .bat file but from the task scheduler the "ftp.R" is unable to find the external functions.
(I am running the code in a Windows Server 2012)
One big difference between running a batch manually / with the scheduler is that the scheduler starts the script with system32 folder as work directory. So it might be enough to add the following line to your batch file: CD %~dp0.
Another point is that the scheduler runs your batch as a different user. So it is possible that you (your user account) has access to //BCN-01/Server/R/Main/Production/ while the system user the scheduler is running your script with does not. You could also try to tell the scheduler to run the script with the same user which you are logged in when running it successfully by hand.
Finally I figured out what was wrong:
All the paths have to be defined as absolute paths: mapped units are not valid.
Althought I already I knew that (thanks to some posts I read here in SO), I missed one path mapped into one of the functions in 'Check_Integrity.R' .

R script schedulling

I have created an R script that runs a K-means segmentation for one of my customers. How can I run this script every thirty days? The script should be on Linux Server.
First you need to create an R script for your k-means calculation. Save this file as your_script.R.
Next create a Windows batch file your_batch.bat which will execute the R script. The batch file should contain the following line which uses Rscript to launch R:
Rscript your_script.R arg1 arg2
Finally you can use the Windows Task Scheduler to setup your script to run every 30 days. Do a Windows search for "Task Scheduler" to find it, and specify your_batch.bat as the program to run.
You can use Sys.sleep to suspend execution for a given number of seconds. You could use this combined with a while to execute a function that runs your K-means segmentation.
I would however recommend you use a task scheduler for this, cron in your case. Just use Rscript to run your script as a cronjob. This does not require an R process to remain active, will launch on restart of the machine, and is easier to stop (no need to kill the process).

Resources