Skip to content

Instantly share code, notes, and snippets.

@tonyelhabr
Last active March 9, 2020 15:31
Show Gist options
  • Save tonyelhabr/b271b831bf24a050e57676b0b46e6fb2 to your computer and use it in GitHub Desktop.
Save tonyelhabr/b271b831bf24a050e57676b0b46e6fb2 to your computer and use it in GitHub Desktop.
Schedule an R script using the {taskscheduleR} package (for Windows).
.get_config_schedule <-
function(path = NULL,
...,
dir = getwd(),
file = NULL,
index = 1,
taskname = NULL,
validate = FALSE) {
if(is.null(path)) {
path <- file.path(dir, file)
}
stopifnot(file.exists(path))
if(is.null(file)) {
file <- tools::file_path_sans_ext(basename(path))
}
if(is.null(taskname)) {
taskname <- paste0(gsub('[0-9]+-', '', tools::file_path_sans_ext(basename(file))), '-', index)
}
if(validate) {
tasks_ls <- tibble::as_tibble(taskscheduleR::taskscheduler_ls())
tasknames_distinct <- dplyr::distinct(tasks_ls, TaskName)
tasknames <- dplyr::pull(tasknames_distinct, TaskName)
stopifnot(!any(config_schedule$taskname == tasknames_existing))
}
list(
taskname = taskname,
path_rexe = file.path(Sys.getenv('R_HOME'), 'bin', 'Rscript.exe'),
# path_rexe = 'C:/PROGRA~1/R/R-3.5.2/bin/Rscript.exe',
path_script = path
)
}
config_schedule <- .get_config_schedule('{path_script}.R')
taskscheduleR::taskscheduler_create(
taskname = config_schedule$taskname,
rscript = config_schedule$path_script,
# chedule = 'MINUTE',
schedule = 'DAILY',
# modifier = 5,
Rexe = config_schedule$path_rexe,
# starttime = format(as.POSIXct('2018-08-02 10:00:00 CDT'), '%H:%M'),
starttime = format(as.POSIXct(paste0(Sys.Date(), ' 5:29:00 CDT')), '%H:%M'),
startdate = format(Sys.Date(), '%m/%d/%Y'),
debug = TRUE
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment