cronR
is a handy package for automating your Rscripts
so they can run even if you aren’t thereHowever, newer versions of macOS don’t give cron access to your full hard drive (so it can run your R script and write the results!), so we’ll have to change the permissions (walk through this together). A couple of explainers about this can be found here and here.
There are two ways to run cronR
—through the RStudio
addin, and directly in a script. We’ll walk through both next! Example
code that we’re going to talk through is below, and the script that
we’re going to automate is here:
library(cronR)
cmd = cron_rscript("~/Documents/GitHub/epimath/mi-umbrella-R-workshop/cronRexample.R")
cron_add(command = cmd, frequency = 'minutely', at = "00:21", id = 'test1', description = 'My process 1', tags = c('lab', 'xyz'))
cron_ls()
cron_rm("test1")
cron_clear() # danger zone!
#### if you have a Windows machine, you must use taskscheduleR instead!
library(taskscheduleR) #https://cran.r-project.org/web/packages/taskscheduleR/readme/README.html
myscript <- "~/Documents/GitHub/epimath/mi-umbrella-R-workshop/cronRexample.R" # have to change the "~"
taskscheduler_create(taskname = 'My-process-1', rscript = myscript,
schedule = "MINUTE", starttime = "00:21")
tasks <- taskscheduler_ls() # creates a dataframe of all tasks
# simplify
filter(tasks, TaskName != "TaskName") %>% select(TaskName, Status)
## some of these are background tasks, or system tasks that the computer does for
## regular maintenance
## if you find "Task Scheduler" on your computer, a window will open up and you'll
## also be able to see jobs there
taskscheduler_delete(taskname = "My-process-1") # this will remove the task we just scheduled