From 8e36c76f7a9e10c08e9b09014996cf39a620ddb9 Mon Sep 17 00:00:00 2001 From: Troejelsgaard Date: Thu, 5 Dec 2024 10:53:06 +0100 Subject: [PATCH 1/9] Correcting small typo --- R/custom_logging.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/custom_logging.R b/R/custom_logging.R index ca31218..12986a1 100644 --- a/R/custom_logging.R +++ b/R/custom_logging.R @@ -6,7 +6,7 @@ #' The default environment variable `WHIRL_LOG_MSG` is set in the session used to log scripts, and input #' is automatically captured in the resulting log. #' -#' If run outside of whirl, meaning when the above environment variable is unset, the aoperations +#' If run outside of whirl, meaning when the above environment variable is unset, the operations #' are streamed to `stdout()`. By default the console. #' #' @name custom_logging From b633fd6c8c236a1c0b428fd6cfe198d39d350ddd Mon Sep 17 00:00:00 2001 From: Troejelsgaard Date: Fri, 6 Dec 2024 09:56:18 +0100 Subject: [PATCH 2/9] Enable redirection of logs through log_dir argument in run() --- R/options.R | 7 +++++++ R/run.R | 6 ++++-- R/whirl_queue.R | 44 +++++++++++++++++++++++++++++++++++--------- R/whirl_r_session.R | 8 ++++++-- 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/R/options.R b/R/options.R index a781f9e..2700ad8 100644 --- a/R/options.R +++ b/R/options.R @@ -82,3 +82,10 @@ options::define_option( default = 1, desc = "Number of simultanous workers used in the run function. A maximum of 128 workers is allowed." ) + +options::define_option( + option = "log_dir", + default = dirname, + desc = "The output directory of the log files. Default is the folder of the excuted script." +) + diff --git a/R/run.R b/R/run.R index c91f5f8..bf92d46 100644 --- a/R/run.R +++ b/R/run.R @@ -52,7 +52,8 @@ run <- function(input, check_renv = options::opt("check_renv", env = "whirl"), verbosity_level = options::opt("verbosity_level", env = "whirl"), track_files = options::opt("track_files", env = "whirl"), - out_formats = options::opt("out_formats", env = "whirl") + out_formats = options::opt("out_formats", env = "whirl"), + log_dir = options::opt("log_dir", env = "whirl") ) { # Additional Settings @@ -105,7 +106,8 @@ run <- function(input, track_files_discards = track_files_discards, track_files_keep = track_files_keep, approved_pkgs_folder = approved_pkgs_folder, - approved_pkgs_url = approved_pkgs_url) + approved_pkgs_url = approved_pkgs_url, + log_dir = log_dir) result <- internal_run(input = input, steps = steps, diff --git a/R/whirl_queue.R b/R/whirl_queue.R index 139155f..96d7549 100644 --- a/R/whirl_queue.R +++ b/R/whirl_queue.R @@ -23,7 +23,8 @@ whirl_queue <- R6::R6Class( track_files_discards = options::opt("track_files_discards", env = "whirl"), track_files_keep = options::opt("track_files_keep", env = "whirl"), approved_pkgs_folder = options::opt("approved_pkgs_folder", env = "whirl"), - approved_pkgs_url = options::opt("approved_pkgs_url", env = "whirl") + approved_pkgs_url = options::opt("approved_pkgs_url", env = "whirl"), + log_dir = options::opt("log_dir", env = "whirl") ) { wq_initialise(self, private, n_workers, @@ -34,7 +35,8 @@ whirl_queue <- R6::R6Class( track_files_discards, track_files_keep, approved_pkgs_folder, - approved_pkgs_url) + approved_pkgs_url, + log_dir) }, #' @description Push scripts to the queue @@ -122,14 +124,15 @@ whirl_queue <- R6::R6Class( track_files_discards = NULL, track_files_keep = NULL, approved_pkgs_folder = NULL, - approved_pkgs_url = NULL + approved_pkgs_url = NULL, + log_dir = NULL ) ) wq_initialise <- function(self, private, n_workers, verbosity_level, check_renv, track_files, out_formats, track_files_discards, track_files_keep, - approved_pkgs_folder, approved_pkgs_url) { + approved_pkgs_folder, approved_pkgs_url, log_dir) { private$check_renv <- check_renv private$verbosity_level <- verbosity_level @@ -139,13 +142,15 @@ wq_initialise <- function(self, private, n_workers, private$track_files_keep <- track_files_keep private$approved_pkgs_folder <- approved_pkgs_folder private$approved_pkgs_url <- approved_pkgs_url + private$log_dir <- log_dir private$.queue <- tibble::tibble( id = numeric(), tag = character(), script = character(), status = character(), - result = list() + result = list(), + log_dir = character() ) private$.workers <- tibble::tibble( @@ -159,12 +164,32 @@ wq_initialise <- function(self, private, n_workers, wq_add_queue <- function(self, private, scripts, tag, status) { + #Adding the log directory to the queue + if (is.character(private$log_dir)) { + #Check if the directory exists + if (!file.exists(private$log_dir)) { + cli::cli_abort("Logs cannot be saved because {.val {private$log_dir}} does not exist") + } + folder <- file.path(private$log_dir) + + } else { + + folder <- private$log_dir(scripts) + #Check if the directory exists + unique_folders <- unique(folder) + if (any(!file.exists(unique_folders))) { + missing <- unique_folders[!file.exists(unique_folders)] + cli::cli_abort("Logs cannot be saved because {.val {missing}} does not exist") + } + } + private$.queue <- self$queue |> tibble::add_row( id = nrow(self$queue) + seq_along(scripts), tag = tag, script = scripts, - status = status + status = status, + log_dir = folder ) return(invisible(self)) } @@ -180,7 +205,7 @@ wq_skip <- function(self, private, scripts, tag) { wq_poll <- function(self, private, timeout, check_renv, verbosity_level, track_files, out_formats, track_files_discards, track_files_keep, - approved_pkgs_folder, approved_pkgs_url) { + approved_pkgs_folder, approved_pkgs_url, log_dir) { # Start new sessions if there are available workers and waiting scripts in the queue @@ -196,7 +221,8 @@ wq_poll <- function(self, private, timeout, track_files_discards = private$track_files_discards, track_files_keep = private$track_files_keep, approved_pkgs_folder = private$approved_pkgs_folder, - approved_pkgs_url = private$approved_pkgs_url), + approved_pkgs_url = private$approved_pkgs_url, + log_dir = private$log_dir), simplify = FALSE) private$.workers[wid, "id_script"] <- nid private$.workers[wid, "active"] <- TRUE @@ -251,7 +277,7 @@ wq_next_step <- function(self, private, wid) { "3" = { purrr::pluck(private$.queue, "result", id_script) <- session$ log_finish()$ - create_outputs(out_dir = dirname(purrr::pluck(private$.queue, "script", id_script)), + create_outputs(out_dir = purrr::pluck(private$.queue, "log_dir", id_script), format = private$out_formats) purrr::pluck(private$.queue, "status", id_script) <- diff --git a/R/whirl_r_session.R b/R/whirl_r_session.R index a94d050..10d38c0 100644 --- a/R/whirl_r_session.R +++ b/R/whirl_r_session.R @@ -19,7 +19,8 @@ whirl_r_session <- R6::R6Class( track_files_discards = options::opt("track_files_discards", env = "whirl"), track_files_keep = options::opt("track_files_keep", env = "whirl"), approved_pkgs_folder = options::opt("approved_pkgs_folder", env = "whirl"), - approved_pkgs_url = options::opt("approved_pkgs_url", env = "whirl") + approved_pkgs_url = options::opt("approved_pkgs_url", env = "whirl"), + log_dir = options::opt("log_dir", env = "whirl") ) { wrs_initialize(verbosity_level, check_renv, @@ -29,6 +30,7 @@ whirl_r_session <- R6::R6Class( track_files_keep, approved_pkgs_folder, approved_pkgs_url, + log_dir, self, private, super) }, @@ -109,6 +111,7 @@ whirl_r_session <- R6::R6Class( wd = NULL, track_files = NULL, out_formats = NULL, + log_dir = NULL, track_files_discards = NULL, track_files_keep = NULL, approved_pkgs_folder = NULL, @@ -122,7 +125,7 @@ whirl_r_session <- R6::R6Class( wrs_initialize <- function(verbosity_level, check_renv, track_files, out_formats, track_files_discards, track_files_keep, - approved_pkgs_folder, approved_pkgs_url, + approved_pkgs_folder, approved_pkgs_url, log_dir, self, private, super) { super$initialize() # uses callr::r_session$initialize() @@ -137,6 +140,7 @@ wrs_initialize <- function(verbosity_level, check_renv, track_files, private$track_files_keep <- track_files_keep private$approved_pkgs_folder <- approved_pkgs_folder private$approved_pkgs_url <- approved_pkgs_url + private$log_dir <- log_dir # If the stream does not support dynamic tty, which is needed for progress bars to update in place, the verbosity is downgraded. if (private$verbosity_level == "verbose" && !cli::is_dynamic_tty()) { From a6d261751334212f9f5de91c60f7c4edf06f7de1 Mon Sep 17 00:00:00 2001 From: Troejelsgaard Date: Fri, 6 Dec 2024 13:04:35 +0100 Subject: [PATCH 3/9] Updating test for the new log_dir argument --- tests/testthat/test-run.R | 58 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/testthat/test-run.R b/tests/testthat/test-run.R index 34c3b2f..8023413 100644 --- a/tests/testthat/test-run.R +++ b/tests/testthat/test-run.R @@ -85,3 +85,61 @@ test_that("Run yaml config file", { expect_no_error() }) + + +test_that("Change the log_dir to a path", { + #Custom path + custom_path <- test_path("scripts/logs") |> + normalize_with_base(base = getwd()) + + #Create the dir if it does not exist + if (!file.exists(custom_path)) { + dir.create(custom_path) + } else { + #If it exists then clean the folder + list.files(custom_path, full.names = TRUE) |> + file.remove() + } + + #Execute run() with log_dir = custom path + res <- test_script("success.R") |> + run(log_dir = custom_path) |> + expect_no_error() + + #Check if the log file is created in the custom path + expect_true(file.exists(test_script("logs/success_log.html"))) + + #Clean the folder + list.files(custom_path, full.names = TRUE) |> + file.remove() + +}) + + +test_that("Change the log_dir with a function", { + #Custom path + custom_path <- test_path("scripts/logs") |> + normalize_with_base(base = getwd()) + + #Create the dir if it does not exist + if (!file.exists(custom_path)) { + dir.create(custom_path) + } else { + #If it exists then clean the folder + list.files(custom_path, full.names = TRUE) |> + file.remove() + } + + #Execute run() with log_dir as a function + res <- test_script("warning.R") |> + run(log_dir = function(x) {paste0(dirname(x), "/logs")}) |> + expect_no_error() + + #Check if the log file is created in the correct folder + expect_true(file.exists(test_script("logs/warning_log.html"))) + + #Clean the folder + list.files(custom_path, full.names = TRUE) |> + file.remove() +}) + From 07c96f22035ea74bb01c7af6438c2e84d69d2cc1 Mon Sep 17 00:00:00 2001 From: Troejelsgaard Date: Fri, 6 Dec 2024 13:06:20 +0100 Subject: [PATCH 4/9] Updating documentations --- R/options.R | 2 +- R/run.R | 14 ++++++++++++++ man/custom_logging.Rd | 2 +- man/options.Rd | 6 ++++++ man/options_params.Rd | 2 ++ man/run.Rd | 19 ++++++++++++++++++- 6 files changed, 42 insertions(+), 3 deletions(-) diff --git a/R/options.R b/R/options.R index 2700ad8..9ebb645 100644 --- a/R/options.R +++ b/R/options.R @@ -86,6 +86,6 @@ options::define_option( options::define_option( option = "log_dir", default = dirname, - desc = "The output directory of the log files. Default is the folder of the excuted script." + desc = "The output directory of the log files. Default is the folder of the excuted script. log_dir can be a path as a character or it can be a function that takes the script path as input and returns the log directory. For more information se details." ) diff --git a/R/run.R b/R/run.R index bf92d46..9fe989f 100644 --- a/R/run.R +++ b/R/run.R @@ -43,6 +43,20 @@ #' ), #' n_workers = 2) #' +#' +#' # Re-directing the logs to a sub-folder by utilizing the log_dir argument in +#' # run(). This will require that the sub-folder exist and the code is therefore +#' # not executed +#' +#' \dontrun{ +#' # Specifying the path using a manually defined character +#' run("success.R", log_dir = getwd()) +#' +#' # Specifying the path with a generic function that can handle the scripts +#' # individually. +#' run("success.R", log_dir = function(x) {paste0(dirname(x), "/logs")}) +#' } +#' #' @export run <- function(input, diff --git a/man/custom_logging.Rd b/man/custom_logging.Rd index 399340f..99d2134 100644 --- a/man/custom_logging.Rd +++ b/man/custom_logging.Rd @@ -26,6 +26,6 @@ that are not automatically captured. The default environment variable \code{WHIRL_LOG_MSG} is set in the session used to log scripts, and input is automatically captured in the resulting log. -If run outside of whirl, meaning when the above environment variable is unset, the aoperations +If run outside of whirl, meaning when the above environment variable is unset, the operations are streamed to \code{stdout()}. By default the console. } diff --git a/man/options.Rd b/man/options.Rd index 79568e5..4a21221 100644 --- a/man/options.Rd +++ b/man/options.Rd @@ -75,6 +75,12 @@ Number of simultanous workers used in the run function. A maximum of 128 workers \item{envvar: }{R_WHIRL_N_WORKERS (evaluated if possible, raw string otherwise)} }} +\item{log_dir}{\describe{ +The output directory of the log files. Default is the folder of the excuted script. log_dir can be a path as a character or it can be a function that takes the script path as input and returns the log directory. For more information se details.\item{default: }{\preformatted{dirname}} +\item{option: }{whirl.log_dir} +\item{envvar: }{R_WHIRL_LOG_DIR (evaluated if possible, raw string otherwise)} +}} + } } diff --git a/man/options_params.Rd b/man/options_params.Rd index 8371e45..b01d71b 100644 --- a/man/options_params.Rd +++ b/man/options_params.Rd @@ -6,6 +6,8 @@ \arguments{ \item{verbosity_level}{How chatty should the log be? Possibilities are \code{quiet}, \code{minimal} and \code{verbose}. (Defaults to \code{"verbose"}, overwritable using option 'whirl.verbosity_level' or environment variable 'R_WHIRL_VERBOSITY_LEVEL')} +\item{log_dir}{The output directory of the log files. Default is the folder of the excuted script. log_dir can be a path as a character or it can be a function that takes the script path as input and returns the log directory. For more information se details. (Defaults to \code{dirname}, overwritable using option 'whirl.log_dir' or environment variable 'R_WHIRL_LOG_DIR')} + \item{n_workers}{Number of simultanous workers used in the run function. A maximum of 128 workers is allowed. (Defaults to \code{1}, overwritable using option 'whirl.n_workers' or environment variable 'R_WHIRL_N_WORKERS')} \item{track_files_discards}{List of file naming patterns not be tracked when track_files = TRUE (Defaults to \verb{c("^/lib", "^/etc", "^/lib64", "^/usr", "^/var", "^/opt", "^/sys", ; "^/proc", "^/tmp", "^/null", "^/urandom", "^/.cache", .libPaths())}, overwritable using option 'whirl.track_files_discards' or environment variable 'R_WHIRL_TRACK_FILES_DISCARDS')} diff --git a/man/run.Rd b/man/run.Rd index bd12283..c9c9c26 100644 --- a/man/run.Rd +++ b/man/run.Rd @@ -12,7 +12,8 @@ run( check_renv = options::opt("check_renv", env = "whirl"), verbosity_level = options::opt("verbosity_level", env = "whirl"), track_files = options::opt("track_files", env = "whirl"), - out_formats = options::opt("out_formats", env = "whirl") + out_formats = options::opt("out_formats", env = "whirl"), + log_dir = options::opt("log_dir", env = "whirl") ) } \arguments{ @@ -39,6 +40,8 @@ summary log will be stored.} \item{track_files}{Should files read and written be tracked? Currently only supported on Linux. (Defaults to \code{FALSE}, overwritable using option 'whirl.track_files' or environment variable 'R_WHIRL_TRACK_FILES')} \item{out_formats}{Which log format(s) to produce. Possiblities are \code{html}, \code{json}, and markdown formats:\code{gfm}, \code{commonmark}, and \code{markua}. (Defaults to \code{"html"}, overwritable using option 'whirl.out_formats' or environment variable 'R_WHIRL_OUT_FORMATS')} + +\item{log_dir}{The output directory of the log files. Default is the folder of the excuted script. log_dir can be a path as a character or it can be a function that takes the script path as input and returns the log directory. For more information se details. (Defaults to \code{dirname}, overwritable using option 'whirl.log_dir' or environment variable 'R_WHIRL_LOG_DIR')} } \value{ A tibble containing the execution results for all the scripts. @@ -71,4 +74,18 @@ run( ), n_workers = 2) + +# Re-directing the logs to a sub-folder by utilizing the log_dir argument in +# run(). This will require that the sub-folder exist and the code is therefore +# not executed + +\dontrun{ +# Specifying the path using a manually defined character +run("success.R", log_dir = getwd()) + +# Specifying the path with a generic function that can handle the scripts +# individually. +run("success.R", log_dir = function(x) {paste0(dirname(x), "/logs")}) +} + } From 60c149c89e044a78672aa7853728fb9dfea4d9a9 Mon Sep 17 00:00:00 2001 From: Troejelsgaard Date: Mon, 9 Dec 2024 12:46:09 +0100 Subject: [PATCH 5/9] Updating DESCRIPTION, NEWS and Vignette --- DESCRIPTION | 2 +- NEWS.md | 1 + vignettes/whirl.Rmd | 35 ++++++++++++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5c19ca9..69a566f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: whirl Title: Logging package -Version: 0.1.6.9000 +Version: 0.1.6.9001 Authors@R: c( person("Aksel", "Thomsen", , "oath@novonordisk.com", role = c("aut", "cre")), person("Lovemore", "Gakava", , "lvgk@novonordisk.com", role = "aut"), diff --git a/NEWS.md b/NEWS.md index bf898a2..3884558 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,5 @@ # whirl (development version) +* Enable redirection of logs through the `log_dir` argument in `run()`. * Changed the title on the individual logs to the script name and moved the path to a distinct section within the title-block. * Fixed a bug where the hyperlink in the summary files was not rendered correctly. diff --git a/vignettes/whirl.Rmd b/vignettes/whirl.Rmd index fb916b4..462e6eb 100644 --- a/vignettes/whirl.Rmd +++ b/vignettes/whirl.Rmd @@ -21,7 +21,7 @@ The whirl package provides functionalities for executing scripts in batch while # Ways to call the `run()` function -## Single and multiple files, and regular expressions +## Single and multiple files, and wild cards The `input` argument in the `run()` function can in the most simple case point to a single file for which an execution and log-generation is required. @@ -52,9 +52,9 @@ run( ) ``` -More information on how the wildcards are interpreted see `utils::glob2rx()`. +More information on how the wildcards are interpreted see `Sys.glob()`. -It is also possible to provide a character vector of several paths (either single files or regular expression) that should be executed. Note that whenever the `input` argument in is supplied with a character vector (e.g. `c("path/to/script1.R", "path/to/script2.R")`) it assumes that these can be executed independently and in parallel. If the elements needs to be executed sequentially this can be achieved by using a `list()` instead (see below). +It is also possible to provide a character vector of several paths (either single files or glob expression) that should be executed. Note that whenever the `input` argument in is supplied with a character vector (e.g. `c("path/to/script1.R", "path/to/script2.R")`) it assumes that these can be executed independently and in parallel. If the elements needs to be executed sequentially this can be achieved by using a `list()` instead (see below). ## Using `list()` as input @@ -133,3 +133,32 @@ run(input = "path/to/config.yaml", n_workers = 4) ``` Each steps in the config file will be executed independently while scrips within each step will be executed in parallel using the number of workers specified with the `n_workers` argument. + +# Adjusting the log directory +## How to use the `log_dir` argument to specify where to store the logs +When executing `run()` the default is to store logs in the directory where the individual scripts are located. For example, if we apply `run()` to a a vector of scripts with the following paths `c(path/to/dir1/script1.R, path/to/dir2/script2.R)`, the log of script1.R and script2.R will be stored in **path/to/dir1** and **path/to/dir2**, respectively. + +If the logs should be stored in a different directory, the `log_dir` argument can be used. +This argument can be supplied with a character string or a function. Note that in either case the directory that `log_dir` points to must exist before the execution is initiated. + +If the `log_dir` is supplied with a character pointing the a specific path the call could look like: + +```{r} +run(input = "path/to/script.R", log_dir = "path/to/logs") +``` + +In this example the log of script.R will be stored in **path/to/logs**. +Note that if multiple scripts are executed and `log_dir` is a character to a path, then every log will be redirected to the same directory - in this case **path/to/logs**. + + +If a more dynamic approach is needed the `log_dir` argument can also be supplied with a function that will be applied to the individual path of every script. +For example, if multiple script are executed and the logs needs to be stored in a sub-folder within the script directories this could be achieved by: + +```{r} +run(input = c("path/to/dir1/script1.R", "path/to/dir2/script2.R"), + log_dir = function(x) {paste0(dirname(x), "/logs")}) +``` + +In this exampel the log of script1.R will be stored in **path/to/dir1/logs** and the log of script2.R will be stored in **path/to/dir2/logs**. + +Note that **x** refer to the path of the script that is being executed. From df825c64e2f8d2a0c83cf7760fff3a58a6f11dd8 Mon Sep 17 00:00:00 2001 From: Troejelsgaard Date: Mon, 9 Dec 2024 13:21:53 +0100 Subject: [PATCH 6/9] Small update to documentation --- R/options.R | 2 +- man/options.Rd | 2 +- man/options_params.Rd | 2 +- man/run.Rd | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/options.R b/R/options.R index 9ebb645..9b9afab 100644 --- a/R/options.R +++ b/R/options.R @@ -86,6 +86,6 @@ options::define_option( options::define_option( option = "log_dir", default = dirname, - desc = "The output directory of the log files. Default is the folder of the excuted script. log_dir can be a path as a character or it can be a function that takes the script path as input and returns the log directory. For more information se details." + desc = "The output directory of the log files. Default is the folder of the excuted script. log_dir can be a path as a character or it can be a function that takes the script path as input and returns the log directory. For more information see the examples of `run()` or `vignette('whirl')`." ) diff --git a/man/options.Rd b/man/options.Rd index 4a21221..3d036b1 100644 --- a/man/options.Rd +++ b/man/options.Rd @@ -76,7 +76,7 @@ Number of simultanous workers used in the run function. A maximum of 128 workers }} \item{log_dir}{\describe{ -The output directory of the log files. Default is the folder of the excuted script. log_dir can be a path as a character or it can be a function that takes the script path as input and returns the log directory. For more information se details.\item{default: }{\preformatted{dirname}} +The output directory of the log files. Default is the folder of the excuted script. log_dir can be a path as a character or it can be a function that takes the script path as input and returns the log directory. For more information see the examples of \code{run()} or \code{vignette('whirl')}.\item{default: }{\preformatted{dirname}} \item{option: }{whirl.log_dir} \item{envvar: }{R_WHIRL_LOG_DIR (evaluated if possible, raw string otherwise)} }} diff --git a/man/options_params.Rd b/man/options_params.Rd index b01d71b..2d5f314 100644 --- a/man/options_params.Rd +++ b/man/options_params.Rd @@ -6,7 +6,7 @@ \arguments{ \item{verbosity_level}{How chatty should the log be? Possibilities are \code{quiet}, \code{minimal} and \code{verbose}. (Defaults to \code{"verbose"}, overwritable using option 'whirl.verbosity_level' or environment variable 'R_WHIRL_VERBOSITY_LEVEL')} -\item{log_dir}{The output directory of the log files. Default is the folder of the excuted script. log_dir can be a path as a character or it can be a function that takes the script path as input and returns the log directory. For more information se details. (Defaults to \code{dirname}, overwritable using option 'whirl.log_dir' or environment variable 'R_WHIRL_LOG_DIR')} +\item{log_dir}{The output directory of the log files. Default is the folder of the excuted script. log_dir can be a path as a character or it can be a function that takes the script path as input and returns the log directory. For more information see the examples of \code{run()} or \code{vignette('whirl')}. (Defaults to \code{dirname}, overwritable using option 'whirl.log_dir' or environment variable 'R_WHIRL_LOG_DIR')} \item{n_workers}{Number of simultanous workers used in the run function. A maximum of 128 workers is allowed. (Defaults to \code{1}, overwritable using option 'whirl.n_workers' or environment variable 'R_WHIRL_N_WORKERS')} diff --git a/man/run.Rd b/man/run.Rd index c9c9c26..e98b0e9 100644 --- a/man/run.Rd +++ b/man/run.Rd @@ -41,7 +41,7 @@ summary log will be stored.} \item{out_formats}{Which log format(s) to produce. Possiblities are \code{html}, \code{json}, and markdown formats:\code{gfm}, \code{commonmark}, and \code{markua}. (Defaults to \code{"html"}, overwritable using option 'whirl.out_formats' or environment variable 'R_WHIRL_OUT_FORMATS')} -\item{log_dir}{The output directory of the log files. Default is the folder of the excuted script. log_dir can be a path as a character or it can be a function that takes the script path as input and returns the log directory. For more information se details. (Defaults to \code{dirname}, overwritable using option 'whirl.log_dir' or environment variable 'R_WHIRL_LOG_DIR')} +\item{log_dir}{The output directory of the log files. Default is the folder of the excuted script. log_dir can be a path as a character or it can be a function that takes the script path as input and returns the log directory. For more information see the examples of \code{run()} or \code{vignette('whirl')}. (Defaults to \code{dirname}, overwritable using option 'whirl.log_dir' or environment variable 'R_WHIRL_LOG_DIR')} } \value{ A tibble containing the execution results for all the scripts. From bef62584ea98ae75af397607043194e026590825 Mon Sep 17 00:00:00 2001 From: akselthomsen Date: Mon, 9 Dec 2024 14:27:57 +0100 Subject: [PATCH 7/9] docs: use exampleif for better rendering in pkgdown --- R/run.R | 3 +-- man/run.Rd | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/R/run.R b/R/run.R index 9fe989f..7fa4abe 100644 --- a/R/run.R +++ b/R/run.R @@ -43,19 +43,18 @@ #' ), #' n_workers = 2) #' +#' @examplesIf FALSE #' #' # Re-directing the logs to a sub-folder by utilizing the log_dir argument in #' # run(). This will require that the sub-folder exist and the code is therefore #' # not executed #' -#' \dontrun{ #' # Specifying the path using a manually defined character #' run("success.R", log_dir = getwd()) #' #' # Specifying the path with a generic function that can handle the scripts #' # individually. #' run("success.R", log_dir = function(x) {paste0(dirname(x), "/logs")}) -#' } #' #' @export diff --git a/man/run.Rd b/man/run.Rd index e98b0e9..b57c86e 100644 --- a/man/run.Rd +++ b/man/run.Rd @@ -74,18 +74,17 @@ run( ), n_workers = 2) +\dontshow{if (FALSE) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} # Re-directing the logs to a sub-folder by utilizing the log_dir argument in # run(). This will require that the sub-folder exist and the code is therefore # not executed -\dontrun{ # Specifying the path using a manually defined character run("success.R", log_dir = getwd()) # Specifying the path with a generic function that can handle the scripts # individually. run("success.R", log_dir = function(x) {paste0(dirname(x), "/logs")}) -} - +\dontshow{\}) # examplesIf} } From 5ba7c2263f053c76afbf8ae115911774a5671a47 Mon Sep 17 00:00:00 2001 From: akselthomsen Date: Mon, 9 Dec 2024 14:28:51 +0100 Subject: [PATCH 8/9] tests: use withr::local_tempdir to avoid creating logs directory and make sure it is empty --- tests/testthat/test-run.R | 49 +++++++++++---------------------------- 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/tests/testthat/test-run.R b/tests/testthat/test-run.R index 8023413..ac9a8ac 100644 --- a/tests/testthat/test-run.R +++ b/tests/testthat/test-run.R @@ -89,17 +89,7 @@ test_that("Run yaml config file", { test_that("Change the log_dir to a path", { #Custom path - custom_path <- test_path("scripts/logs") |> - normalize_with_base(base = getwd()) - - #Create the dir if it does not exist - if (!file.exists(custom_path)) { - dir.create(custom_path) - } else { - #If it exists then clean the folder - list.files(custom_path, full.names = TRUE) |> - file.remove() - } + custom_path <- withr::local_tempdir() #Execute run() with log_dir = custom path res <- test_script("success.R") |> @@ -107,39 +97,26 @@ test_that("Change the log_dir to a path", { expect_no_error() #Check if the log file is created in the custom path - expect_true(file.exists(test_script("logs/success_log.html"))) - - #Clean the folder - list.files(custom_path, full.names = TRUE) |> - file.remove() - + file.path(custom_path, "success_log.html") |> + file.exists() |> + expect_true() }) - test_that("Change the log_dir with a function", { - #Custom path - custom_path <- test_path("scripts/logs") |> - normalize_with_base(base = getwd()) - - #Create the dir if it does not exist - if (!file.exists(custom_path)) { - dir.create(custom_path) - } else { - #If it exists then clean the folder - list.files(custom_path, full.names = TRUE) |> - file.remove() - } + #Custom path and copy script + custom_path <- withr::local_tempdir() + dir.create(file.path(custom_path, "logs")) + file.copy(from = test_script("warning.R"), to = custom_path) |> + expect_true() #Execute run() with log_dir as a function - res <- test_script("warning.R") |> + res <- file.path(custom_path, "warning.R") |> run(log_dir = function(x) {paste0(dirname(x), "/logs")}) |> expect_no_error() #Check if the log file is created in the correct folder - expect_true(file.exists(test_script("logs/warning_log.html"))) - - #Clean the folder - list.files(custom_path, full.names = TRUE) |> - file.remove() + file.path(custom_path, "logs", "warning_log.html") |> + file.exists() |> + expect_true() }) From 85c077878aaa1fb089edadbfb597636236658fe4 Mon Sep 17 00:00:00 2001 From: Troejelsgaard Date: Mon, 9 Dec 2024 15:23:55 +0100 Subject: [PATCH 9/9] Small typo --- vignettes/whirl.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vignettes/whirl.Rmd b/vignettes/whirl.Rmd index 462e6eb..f5b5b86 100644 --- a/vignettes/whirl.Rmd +++ b/vignettes/whirl.Rmd @@ -141,7 +141,7 @@ When executing `run()` the default is to store logs in the directory where the i If the logs should be stored in a different directory, the `log_dir` argument can be used. This argument can be supplied with a character string or a function. Note that in either case the directory that `log_dir` points to must exist before the execution is initiated. -If the `log_dir` is supplied with a character pointing the a specific path the call could look like: +If the `log_dir` is supplied with a character pointing to a specific path the call could look like: ```{r} run(input = "path/to/script.R", log_dir = "path/to/logs")