How to schedule R scripts (2024)

[This article was first published on R – Open Source Automation, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

How to schedule R scripts (1)


Running R with taskscheduleR and cronR

In a previous post, we talked about how to run R from the Windows Task Scheduler. This article will talk about two additional approaches to schedule R scripts, including using the taskscheduleR package on Windows and the cronR package for Linux. For scheduling Python code, check out this post.

Schedule R scripts with taskscheduleR

Let’s install taskscheduleR using the install.packages command.

install.packages("taskscheduleR")

Next, we just need to load the package to get started.

library(taskscheduleR)

Creating a sample R script to run automatically

Before we do any scheduling, we need to first create a script. We’ll save the code below in a file called “create_file.txt”. This script will randomly generate a collection of integers and write them out to a file.

nums <- sample(10000, 100)write(nums, "sample_nums.txt")

Using the taskscheduler_create function

Next, in order to schedule the script to run automatically, we need to use the taskscheduler_create function. This function takes several arguments, which can be seen below.

taskscheduler_create(taskname = "test_run", rscript = "/path/to/file/create_file.R", schedule = "ONCE", starttime = format(Sys.time() + 50, "%H:%M"))

Firstly, we need to give a name to the task we want to create. In this case, we’ll just call our task “test_run”. Next, we need to specify the R script we want to automatically run. Third, we add the schedule parameter, which denotes how frequently we want to run this script. There are several options here, including WEEKLY, DAILY, MONTHLY, HOURLY, and MINUTE. For example, if we want our script to run every day, we would modify our function call like this:

taskscheduler_create(taskname = "test_run", rscript = "/path/to/file/create_file.R", schedule = "DAILY", starttime = format(Sys.time() + 50, "%H:%M"))

The other parameter we need to select is start time. In our examples, we’re setting the task to start in 50 seconds from the current time.

In addition to these arguments, taskscheduler_create also has a parameter called “modifier”. This allows us to modify the schedule frequency. For example, what if we want to run the task every 2 hours? In this case, we would just set modifier = 2.

taskscheduler_create(taskname = "test_run", rscript = "/path/to/file/create_file.R", schedule = "HOURLY", starttime = format(Sys.time() + 50, "%H:%M"), modifier = 2)

.

Similarly, we could run our script every 10 minutes using the code below, with a modifier of 10.

taskscheduler_create(taskname = "test_run", rscript = "/path/to/file/create_file.R", schedule = "MINUTE", starttime = format(Sys.time() + 50, "%H:%M"), modifier = 10)

Passing arguments to the Task Scheduler

We can pass arguments to the scheduled task using the “rscript_args” parameter.

taskscheduler_create(taskname = "test_run", rscript = "/path/to/file/create_file.R", schedule = "MINUTE", starttime = format(Sys.time() + 50, "%H:%M"), modifier = 10, rscript_args = c("10", "test", "this"))

Listing the tasks in the scheduler

What if we want to look at all the tasks currently in the task scheduler? There’s a quick method for that called taskscheduler_ls.

taskscheduler_ls()

Running this function returns a data frame of all the tasks currently in the task scheduler.

Deleting tasks from the scheduler

Tasks can be deleted from the scheduler using the taskscheduler_delete function. We just need to input the the name of the task.

taskscheduler_delete("test_run")

Stopping tasks

Similarly, tasks can also be stopped with a single function call. This time, we’ll use the taskscheduler_stop function.

taskscheduler_stop("test_run")

Scheduling tasks on Linux

If you’re running on Linux, there’s an alternative package called cronR which can be used. To use this package on Linux, we need to install it using devtools, like below.

devtools::install_github("bnosac/cronR")

Additionally, the cron daemon needs to be running for the package to work properly. On Debian, you can ensure this by running the below commands:

sudo apt-get updatesudo apt-get install -y cronsudo cron start

Schedule R scripts on Linux

Once installed, we can get started creating tasks. In this example we create a task that runs daily, starting at 9:00 AM.

library(cronR)cmd <- cron_rscript("/path/to/file/create_file.R")cron_add(command = cmd, frequency = 'daily', at = "9:00" , id = 'test_linux_run', description = "testing linux scheduler")

To change the time, we just need to adjust the “at” parameter. The example below runs the same script automatically at 3:00 PM (15:00) each day.

cron_add(command = cmd, frequency = 'daily', at = "15:00" , id = 'test_linux_run', description = "testing linux scheduler")

The next example runs the same task every 30 minutes.

cron_add(command = cmd, frequency = '/30 * * * *', at = "15:00" , id = 'test_linux_run', description = "testing linux scheduler")

Passing arguments to cronR

Running an R script with command line arguments is a common need. This can be handled with cronR using the “rscript_args” parameter in the cron_rscript function.

cmd <- cron_rscript("/path/to/file/create_file.R", rscript_args = c("10", "test", "this"))cron_add(command = cmd, frequency = '/30 * * * *', at = "15:00" , id = 'test_linux_run', description = "testing linux scheduler")

Running cron jobs on specific days of the week

It’s also possible to select what days of the week you want a job to run. For example, if we want a job to run hourly, but only on certain days of the week, we could specify that like below.

cron_add(command = cmd, frequency = 'hourly', at = "15:00" , id = 'test_linux_run', description = "testing linux scheduler", days_of_week = c(0,3))

Running cron jobs on specific days of the month

Similarly, we can also select days of the month in which to run jobs.

cron_add(command = cmd, frequency = 'hourly', at = "15:00" , id = 'test_linux_run', description = "testing linux scheduler", days_of_month = c(10, 20, 30))

Listing all Linux tasks

cronR can list all scheduled Linux jobs using the cron_ls function.

cron_ls()

Conclusion

If you enjoyed this post, make sure to share it with others, or follow my blog on Twitter!. Check out this link to see how to run Python from the task scheduler.

taskscheduleR and cronR can also be utilized with an RStudio add-in. Learn more about taskscheduleR here, and cronR by clicking here.

The post How to schedule R scripts appeared first on Open Source Automation.

Related

To leave a comment for the author, please follow the link and comment on their blog: R – Open Source Automation.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

How to schedule R scripts (2024)

References

Top Articles
The Shocking Death Of Nikki Catsouras: A Cautionary Tale
Grim Dawn Dynamite Farming
Automated refuse, recycling for most residences; schedule announced | Lehigh Valley Press
Hay day: Top 6 tips, tricks, and cheats to save cash and grow your farm fast!
Rochester Ny Missed Connections
My.doculivery.com/Crowncork
Culver's Flavor Of The Day Monroe
Full Range 10 Bar Selection Box
Echo & the Bunnymen - Lips Like Sugar Lyrics
Summer Rae Boyfriend Love Island – Just Speak News
Guidewheel lands $9M Series A-1 for SaaS that boosts manufacturing and trims carbon emissions | TechCrunch
180 Best Persuasive Essay Topics Ideas For Students in 2024
Teenleaks Discord
065106619
Gino Jennings Live Stream Today
Craiglist Tulsa Ok
Imagetrend Inc, 20855 Kensington Blvd, Lakeville, MN 55044, US - MapQuest
Inside the life of 17-year-old Charli D'Amelio, the most popular TikTok star in the world who now has her own TV show and clothing line
Bank Of America Financial Center Irvington Photos
Long Island Jobs Craigslist
Mc Donald's Bruck - Fast-Food-Restaurant
The Tower and Major Arcana Tarot Combinations: What They Mean - Eclectic Witchcraft
Mega Personal St Louis
Sadie Sink Reveals She Struggles With Imposter Syndrome
Koninklijk Theater Tuschinski
Prep Spotlight Tv Mn
Foodsmart Jonesboro Ar Weekly Ad
Jamielizzz Leaked
Dell 22 FHD-Computermonitor – E2222H | Dell Deutschland
Neteller Kasiinod
Tripcheck Oregon Map
FSA Award Package
Red Sox Starting Pitcher Tonight
Home Auctions - Real Estate Auctions
new haven free stuff - craigslist
2487872771
Fridley Tsa Precheck
Metra Union Pacific West Schedule
Uhaul Park Merced
Autozone Locations Near Me
Clima De 10 Días Para 60120
Carroll White Remc Outage Map
Karen Wilson Facebook
Juiced Banned Ad
Flappy Bird Cool Math Games
Food and Water Safety During Power Outages and Floods
Mytmoclaim Tracking
Evil Dead Rise - Everything You Need To Know
O.c Craigslist
Arre St Wv Srj
Factorio Green Circuit Setup
Coors Field Seats In The Shade
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 6290

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.