Search code examples
rscheduled-taskscron-taskwindows-task-scheduler

How to run R script through multiple directories via windows Task Scheduler


I'm trying to automate some reports in R through the Windows Task Scheduler. The issue is that I keep getting 0x1 error codes and I believe it is because the automated script is using the source() function in R to call other scripts that are not in the same directory.

I have tried creating a .bat file but it runs into the same problem. My user does have security rights, as well.

@echo off 
"C:\Program Files\R\R-3.5.2\bin\Rscript.exe" "C:\Users\me\data\report\file.R"
pause

I've also tried:

@echo off 
"C:\Program Files\R\R-3.5.2\bin\Rscript.exe" -e"source('C:/Users/me/data/report/file.R')"

Does anybody know how to automate a script that would go through multiple directories? Or do I really have to clone each of these scripts and put all of them in the same folder just to automate things?


Solution

  • You shouldn't have to put them all in the same directory.

    I created a script that calls another script in another directory and created a task with taskscheduleR.

    It worked for me so I don't think the issue has to do with the source().

    Nevertheless, your issue is hard to reproduce. But it might have something to do with administrator roles. I would suggest that you talk to your IT-department if you have one.

    source("C:/Users/ADMFIWA/Documents/task-test-other/other-summary.R")
    
    library(taskscheduleR)
    taskscheduler_create(taskname = "test",
                         rscript = "C:/Users/ADMFIWA/Documents/task-test/summary.R", 
                         schedule = "ONCE", starttime = format(Sys.time() + 62, "%H:%M"))
    #> [1] "SUCCESS: The scheduled task \"test\" has successfully been created."
    

    Created on 2019-03-12 by the reprex package (v0.2.0).