Learn R Programming

cronR (version 0.6.5)

cron_rscript: Create a command to execute an R script which can be scheduled with cron_add

Description

Create a command to execute an R script which can be scheduled with cron_add where the stdout and stderr will be passed on to a log

Usage

cron_rscript(
  rscript,
  rscript_log = sprintf("%s%s.log", tools::file_path_sans_ext(rscript),
    ifelse(log_timestamp, "-`date+\\%Y-\\%m-\\%d_\\%H:\\%M:\\%S`", "")),
  rscript_args = "",
  cmd = file.path(Sys.getenv("R_HOME"), "bin", "Rscript"),
  log_append = TRUE,
  log_timestamp = FALSE,
  workdir = NULL,
  type = c("default", "output_on_error", "output_always"),
  ...
)

Value

a character string with a command which can e.g. be put as a cronjob for running a simple R script at specific timepoints

Arguments

rscript

character string with the path to an R script with .r or .R extension

rscript_log

where to put the log, defaults in the same directory and with the same filename as rscript but with extension .log.

rscript_args

a character string with extra arguments to be passed on to Rscript

cmd

path to Rscript. Defaults to R_HOME/bin/Rscript

log_append

logical, append to the log or overwrite

log_timestamp

logical, indicating to append a timestamp to the script log filename in the default argument of rscript_log. This will only work if the path to the log folder does not contain spaces.

workdir

If provided, Rscript will be run from this working directory.

type

a character string specifying the type of command to generate:

default

The command will send stdout and stderr to the log file but will never output these streams.

output_always

The command will send stdout and stderr to the log file in addition to emitting them as an output.

output_on_error

The command will send stdout and stderr to the log file, and it will emit them as an output when the R script has a non-zero exit status.

...

further arguments, specific to argument type, currently not used

Examples

Run this code
f <- system.file(package = "cronR", "extdata", "helloworld.R")
cron_rscript(f)
cron_rscript(f, rscript_args = "more arguments passed on to the call")
cron_rscript(f, rscript_args = c("more", "arguments", "passed", "on", "to", "the", "call"))

cron_rscript(f, log_append = FALSE)
cron_rscript(f, log_append = TRUE)
cron_rscript(f, log_append = FALSE, log_timestamp = TRUE)

## run from home directory
cron_rscript(f, workdir = normalizePath("~"))

## other non-default options for type
cron_rscript(f, type = "output_on_error")
cron_rscript(f, type = "output_always")

Run the code above in your browser using DataLab