Automate Your R Scripts with taskscheduleR


Steven P. Sanderson II, MPH


July 6, 2024


Today, let’s dive into a nifty R package called taskscheduleR that can automate running your R scripts. Whether you need to execute a task every hour or just once a day, taskscheduleR has you covered. This package leverages the Windows Task Scheduler, making it a breeze to schedule and automate repetitive tasks directly from R. Let’s walk through a couple of examples from my new book, “Extending Excel with Python and R”.


Example 1: Hourly Script Execution

First, let’s set up a task that runs a script every hour. Here’s the code:


# Create a task scheduler job that runs the script every hour
  taskname = "Hello World Hourly",
  rscript = "hello_world.R",
  schedule = "0 * * * *"

In this snippet, we use the taskscheduler_create() function to create a new task. Let’s break down the arguments:

  • taskname: A unique name for the task, in this case, “Hello World Hourly”.
  • rscript: The path to the R script you want to run, here it’s “hello_world.R”.
  • schedule: This is the cron expression for scheduling. 0 * * * * means the script will run at the start of every hour.

Example 2: Daily Script Execution at a Specific Time

Now, let’s set up a task that runs the script once a day at 10:00 AM. Here’s how you can do it:

# Create a task scheduler job that runs the script once a day at 10:00 AM
  taskname = "Hello World Daily",
  rscript = "hello_world.R",
  schedule = "0 10 * * *"

In this example, the schedule argument 0 10 * * * ensures the script runs daily at 10:00 AM.

Why Automate?

Scheduling routine tasks can save you a lot of time and reduce the chances of forgetting to run an important script. Whether it’s updating a report, fetching data, or performing backups, automation ensures that these tasks run consistently and accurately without manual intervention.

Give It a Try!

I encourage you to experiment with taskscheduleR and see how it can fit into your workflow. Whether you’re new to automation or looking for ways to optimize your existing processes, this package offers a simple yet powerful solution.

If you found this helpful and want to learn more about extending your Excel capabilities with R and Python, check out my new book, “Extending Excel with Python and R”. It’s packed with practical examples and detailed explanations to help you get the most out of these powerful tools.

Happy coding!