Scheduling R Tasks via Windows Task Scheduler

This post will allow you to impress your boss with your strong work ethic by enabling Windows R users to schedule late night tasks.  Picture it, your boss gets an email at 1:30 in the morning with the latest company data as a beautiful report.  I’m quite sure Linux and Mac users are able to do this rather easily via cron.  Windows users can do this via the Task Scheduler.  Users can also interface the task scheduler via the command line as well.

As this is more process oriented, I have created a minimal example on GitHub and the following video rather than providing scripts in-text.  All the scripts can be accessed via: https://github.com/trinker/Make_Task  User’s will need to fill in relevant information (e.g., paths, usernames, etc.) and download necessary libraries to run the scripts.  The main point of this demonstration is to provide the reader (who is a Windows user) with a procedure for automating R tasks.

Advertisements

About tylerrinker

I am Literacy PhD student with a bent for the quantitative and a passion for R.
This entry was posted in r, Uncategorized, work flow and tagged , , , , , , . Bookmark the permalink.

44 Responses to Scheduling R Tasks via Windows Task Scheduler

  1. Pingback: Distilled News | Data Analytics & R

  2. apwheele says:

    Thank you for this. Two points: To find the task scheduler, you can just search for “task sch” in the search part of the start (or whatever that windows button thing is called!) I forget if this existed for xp though…

    Second point is a pro-tip, on various machines I have had inconsistent behavior with whether “R.exe” or “Rscript.exe” is in the “path” environment variable, (so simply using “R CMD BATCH” will not find the R.exe to run.) Ditto for basically every program I’ve ever installed on windows. If that is the case, you can just call the exe file directly with the full path name to both the exe file and the “.R” file (enclose in double quotes if spaces in either path).

    (You can add R.exe to the path environment, but that is just as annoying and limits portability to new machines.) This advice basically extends to any automated task you want to do in windows, figure out the command line prompts, put them in a plain text “.bat” file, and then make a scheduled task. If whatever program you call is in the path environment, and the bat file is in the same folder as the “.R” file, you don’t need to specify the paths at all.

  3. Tal Galili says:

    Very cool – thanks 🙂

  4. aephidayatuloh says:

    Hello,

    I have a problem when I run this line in R (scheduled or manually):
    rmarkdown::render(“apple_microsoft.Rmd”, “all”, output_dir = “reports”)

    As the result I get message
    Error: pandoc version 1.12.3 or higher is required and was not found.

    But when I run in RStudio manually everything is works.
    Anyone has ideas how to solve this?

    Thank you.

  5. mazatlan says:

    First, Thanks for sharing this post. I have been trying to implement something like this in a while but was having trouble with the email section. Second, I have two questions;
    1- Can I email to multiple users at once?
    2 – The packages that I downloaded while running task.r now reside on my machine locally or they have dependencies on your website or github still?. A more straight forward question, If you delete all the code from github today, I would still be able to send automated email and reports? Thanks and sorry for the naïve questions.

  6. Gopi Goteti says:

    Thanks! Very helpful and useful!

    Just a note on network drives – if your data/files happen to be on a network drive, and not on C: or a local drive, then the windows task scheduler may not work properly. The tricky thing is that R is smart enough to realize that “X:” drive is the same as \\company_network\\users_dirs\\X\\, but windows scheduler is not. You might have to change the file paths in your code accordingly. Some folks went through similar pain –
    http://windowssecrets.com/forums/showthread.php/161351-Task-scheduler-not-running-my-batch-file

  7. jgreenb1 says:

    gmailR looks like a great package! Unfortunately, I’m having trouble loading it. I get the following response:

    * installing *source* package ‘gmailR’ …
    ** R
    ** data
    *** moving datasets to lazyload DB
    ** inst
    ** byte-compile and prepare package for lazy loading
    Note: no visible binding for ‘<<-' assignment to '.UNAME'
    ** help
    *** installing help indices
    ** building package indices
    ** testing if installed package can be loaded
    *** arch – i386
    Error : .onLoad failed in loadNamespace() for 'rJava', details:
    call: inDL(x, as.logical(local), as.logical(now), …)
    error: unable to load shared object 'C:/Users/Jon.INCUBET/Documents/R/win-library/3.1/rJava/libs/i386/rJava.dll':
    LoadLibrary failure: %1 is not a valid Win32 application.

    Error : package 'rJava' could not be loaded
    Error: loading failed
    Execution halted
    *** arch – x64
    ERROR: loading failed for 'i386'
    * removing 'C:/Users/Jon.INCUBET/Documents/R/win-library/3.1/gmailR'
    Error : Command failed (1)
    Warning message:
    In pacman::p_load_gh("trinker/gmailR") :
    Failed to install/load:
    trinker/gmailR

    I think this means that R doesn't know where Java is located. I installed Java, but I am still getting the same error. I tried running loading the rJava package – using library(rJava) – and it loads without any error.

    Can you help me?

  8. Jordan says:

    Hi folks,

    Thanks for the great tips. I got stuck so any help would be greatly appreciated! I’ve created the .bat file but when I run it I get the following message:

    ARGUMENT ‘BATCH’ __ignored__ and that’s it. The script does not execute. What am I doing wrong? Here is the content of my batch file for reference:

    @echo off
    “C:\Program Files\R\R-3.1.2\bin\R.exe” BATCH “C:\Users\eyv\Desktop\task\TJ-num-of-items-in-search.R”

    I had to type in the full location of the R.exe and the R script as @apwheele said otherwise it did not work at all.

  9. Joseph F. says:

    Hi Jordan- I ran into the exact same problem.
    Try this using CMD BATCH (your code is just BATCH) and put lines 2 and 3 on the same row.

    Your slightly modified example below:
    @echo off
    “C:\Program Files\R\R-3.1.2\bin\R.exe” CMD BATCH “C:\Users\eyv\Desktop\task\TJ-num-of-items-in-search.R”

  10. Boom says:

    Thank you for this video. I have one issue- when I try to run the batch file I get the following error:

    “‘R’ is not recognized as internal or external command, operable program or batch file”

    Any ideas on how to solve this? Thanks everyone.

    • tylerrinker says:

      Someone asked the same question via email a week back and this helped:

      This means R is not on your path. To solve…

      1. Navigate to where R is located. Mine is C:\R\R-3.2.1\bin . This should get R’s location: gsub("library", "bin", .libPaths())
      2. Copy that path
      3. Go to your path environment variables in control panel. Here’s how I do this:
        1. Control Panel\System and Security\System
        2. Click on Advanced system settings
        3. Click environment variables
        4. Find “path” under environment variables
        5. Click on it and add a semicolon followed by the path to R from above
      4. You’ll know it worked if you go into the windows command prompt and type “r” it will run the R program from the windows command line
      • Boom says:

        You are the man. Everything looks to be working perfectly. Thanks again for the help! This video is awesome.

  11. luizfuzaro says:

    Reblogged this on bsbrug <- and commented:
    Geração de Gráficos e outras apresentações agendadas.

  12. And what happen if I use RStudio for Windows, what can I have to change? Cause if i just change R.exe by RStudio.exe it dosen´t work

  13. Viola says:

    Hi Tyler,

    Thank you very much for the detailed tutorial. I followed all the steps, including apwheele’s suggestions (added the R.exe path the environmental variables), but when I click on the .bat file, the rompt starts and immediately closes. The script doesn’t execute.

    Here is how my .bat file looks like (I created it in .txt file and saved as .bat):
    @echo off
    “C:\Program Files\R\R-3.2.0\bin\x64\R.exe” CMD BATCH “C:\Users\viola\Documents\Plotly\Scheduled_Script\Sales_Dashboard_Scheduled.R”

    My R script starts with this:

    setwd(“C:/Users/viola/Documents/Plotly/Scheduled_Script”)

    library (RODBC)
    library (plyr)
    library (ggplot2)
    library (shiny)
    library (plotly)
    library (dplyr)

    ch <- odbcConnect("Cumulus_DB_test", believeNRows=FALSE) #connects to the DB and runs SQL query
    source("variables.R") #stores my DB connection credentias
    source("plotly.R") #stores my Plotly credentials

    I store plotly.R & variables.R in the same folder as the main script.

    What am I doing wrong? Any advice is much appreciated!

  14. Ankur says:

    Hi Tyler,

    I have an R code that does something and then sends an email to a list of recipients when the code has run.

    When run on Rstudio – the code runs successfully (i.e. all the necessary files are created) AND emails are sent out successfully.
    When run via Windows Task Scheduler – the code runs successfully (i.e. all the necessary files are created) BUT emails are not sent out.

    I am just not able to figure out why a part of the code is running fine on both Rstudio & via task scheduler, and another part only runs on Rstudio? Following are two pieces of code that I am using to send out emails – both are working fine enough when run on Rstudio.

    1.
    library(RDCOMClient)

    path_name <- file.path(mainDir, subDir)
    subject <- paste0("Data quality checks completed for ", analysis_date)
    body <- paste0("Data summary has been compiled for all the farms for ", analysis_date,
    ". All the data summaries are saved in the folder .”)
    email_fn <- function(recipient) {

    OutApp <- COMCreate("Outlook.Application")
    outMail = OutApp$CreateItem(0)
    outMail[["To"]] = recipient
    outMail[["subject"]] = subject
    outMail[["body"]] = body
    outMail$Send()

    }

    #email_fn(recipient = "person1@abc.com")
    #email_fn(recipient = "person2@abc.com")

    2.

    library(sendmailR)

    from <- "sender@abc.com"
    to <- c("person1@abc.com","person2@abc.com")
    subject <- "Email Subject"
    body <- "Email body."
    mailControl = list(smtpServer = "tucson.websitewelcome.com")

    sendmail(from = from, to = to, subject = subject, msg = body, control = mailControl)

    Would you know what might be happening here?

    Thanks,
    Ankur

    • Nikolay says:

      Hi Tyler,

      I have the same batch file with RDCOM setup as Ankur, and running into same issue. When the “Run whether user is logged on or not”, my task runs and the output is created, but RDCOM is not sending the email. All runs OK when the box is not ticked… However, I’d like to be able to run tasks even when my computer is logged off.

      Any ideas why RDCOM gets stumped by the user context?

      Nik

      • KELVIN TAN says:

        I got this error:

        Error in COMCreate(guid, existing = FALSE) :
        Failed to create COM object: Server execution failed
        Calls: email_fn -> COMCreate -> getCOMInstance -> COMCreate -> .Call
        Execution halted

  15. Ankur says:

    Rout doesn’t print anything for this part of the code. Anyway, figured out the problem. In the task scheduler security options, earlier I had checked “Run whether user is logged on or not”. I un-checked that and checked “Run only when user is logged on” – this did the trick and the emails are now going through. Thanks 🙂

  16. KELVIN TAN says:

    Hi Ankur,

    I have tried different options, whether checked “Run whether user is logged on or not” or check “Run only when user is logged on”, both fail to send the email with or without attachment. However, when I run the R code manually without the Task Scheduler, it is able to send the email with or without attachment. So what did I miss out in Task Scheduler, that fail to run my code? My code as below:

    analysis_date <- Sys.Date()
    subject <- paste0("Historical Data Analysis and Report completed for ", analysis_date)
    body <- paste0("Data summary has been compiled for all the farms for ", analysis_date,
    ". All the data summaries are saved in the folder .")
    path_to_attch_file <- paste(getwd(),"/reports/RealTimeUpdate.pdf",sep="")

    email_fn <- function(recipient) {
    OutApp <- COMCreate("Outlook.Application")
    outMail = OutApp$CreateItem(0)
    outMail[["To"]] = recipient
    outMail[["subject"]] = subject
    outMail[["body"]] = body
    # outMail[["Attachments"]]$Add(path_to_attch_file)
    outMail$Send()
    }
    email_fn(recipient = "myname@yahoo.com")

  17. KELVIN TAN says:

    > analysis_date subject body path_to_attch_file
    > email_fn <- function(recipient) {
    + OutApp email_fn(recipient = “tanthiamhuat@yahoo.com”)
    Error in COMCreate(guid, existing = FALSE) :
    Failed to create COM object: Access is denied.
    Calls: email_fn -> COMCreate -> getCOMInstance -> COMCreate -> .Call
    Execution halted

  18. bshelt says:

    This was an awesome post! It worked great (with the addition of apwheele’s suggestion).

  19. Rob says:

    I’m having a similar problem to Ankur’s above, but I don’t have the option of checking the “Run only when user is logged on” button, as this scheduled task is run on a remote server. The task works as expected when a user is logged in (both scheduled and force-run), but if the user logs out, the task completes, but no email is sent.

    Additionally, when I write an error file from this scheduled task, I get the following when the user isn’t logged in:

    “”,”x”
    “1”,”package or namespace load failed for ‘sendmailR'”

    I’ve confirmed that sendmailR is installed in the local library for both the user account that runs the task, as well as the C:\Users\Administrator directory.

    Any thoughts on what else to try?

  20. Pingback: Track your local R scheduled tasks with CommandCenter2000!!! – AmitKohli.com

  21. Pingback: Track your local R scheduled tasks with CommandCenter2000!!! – Mubashir Qasim

  22. Pingback: Track your local R scheduled tasks with CommandCenter2000!!!

  23. audreykaspi@gmail.com says:

    Thanks for this. Has really helped me build the case for using R at work. Cheers!

  24. Pablo says:

    Hi Tyler,
    I´m a user of some of your packages, I´m from Spain. Thanks.

    I´ve a trouble with mi project. In few words, I have a script in R, that scraps the web to get the securities of the spanish values.
    I have my script in one hand, my R file, and finally my file .bat to schedule the action.

    It doesn´t works, I don´t know why? How can you help me, If you a able to of course.

    Thank you very much
    Pablo

  25. Pablo says:

    I´m sure that Im doing a lot of things wrong, so I give you my codes.

    RScript
    library(httr)
    library(XML)
    url <- "http://www.infobolsa.es/acciones/ibex35&quot;
    doc = htmlParse(url)
    tableNodes = getNodeSet(doc, "//table")
    ibex35 = readHTMLTable(tableNodes[[1]],encoding="UTF-8",trim = TRUE,as.data.frame = TRUE)
    write.csv(ibex35, file = "swirl_temp/ibex35.csv")
    print(ibex35)

    .bat file
    @echo off
    R CMD BATCH C:\Users\Pablo Ruigomez\Documents\swirl_temp

    .R file
    The R Script ^

    Thank you again

  26. DS says:

    I used outMail[[“Attachments”]]$Add(“path_to_attach_file”) line of code to add attachment but didn’t work.
    Error:Error in outMail[[“DS.xlsx”]] :
    Cannot locate 0 name(s) DS..xlsx in COM object (status = -2147352570)

    Just to confirm: Is “path_to_attach_file” the current directory?

    Thanks in advance,
    DS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s