From d0501b062adc8202b4498f6d2cf25c5ad9f5f2c8 Mon Sep 17 00:00:00 2001 From: m7pr Date: Tue, 14 May 2024 12:05:23 +0200 Subject: [PATCH] add new arguments to log_shiny_input_changes --- R/hooks.R | 16 ++++++++++++---- man/log_shiny_input_changes.Rd | 13 ++++++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/R/hooks.R b/R/hooks.R index eb375397..aa1bd90f 100644 --- a/R/hooks.R +++ b/R/hooks.R @@ -114,6 +114,11 @@ log_errors <- function(muffle = getOption('logger_muffle_errors', FALSE)) { #' @param level log level #' @param excluded_inputs character vector of input names to exclude from logging #' @param namespace the name of the namespace +#' @param allow allow to be used outside Shiny app +#' @param change_message custom message to be displayed during the input change - supports `glue` syntax where `{name}` +#' is the name of the input, and `{old}` and `{new}` are input values before the change and after the change +#' @param initialize_message custom message to be displayed during input initialization (this is followed by initialized +#' input values being printed in JSON format) #' @importFrom utils assignInMyNamespace assignInNamespace #' @examples \dontrun{ #' library(shiny) @@ -142,18 +147,21 @@ log_errors <- function(muffle = getOption('logger_muffle_errors', FALSE)) { log_shiny_input_changes <- function(input, level = INFO, namespace = NA_character_, - excluded_inputs = character()) { + excluded_inputs = character(), + allow = FALSE, + change_message = 'Shiny input change detected on {name}: {old} -> {new}', + initialize_message = 'Default Shiny inputs initialized:') { fail_on_missing_package('shiny') fail_on_missing_package('jsonlite') - if (!shiny::isRunning()) { + if (!shiny::isRunning() || allow) { stop('No Shiny app running, it makes no sense to call this function outside of a Shiny app') } input_values <- shiny::isolate(shiny::reactiveValuesToList(input)) assignInMyNamespace('shiny_input_values', input_values) log_level(level, skip_formatter(paste( - 'Default Shiny inputs initialized:', + initialize_message, as.character(jsonlite::toJSON(input_values, auto_unbox = TRUE)))), namespace = namespace) shiny::observe({ @@ -165,7 +173,7 @@ log_shiny_input_changes <- function(input, old <- old_input_values[name] new <- new_input_values[name] if (!identical(old, new)) { - log_level(level, 'Shiny input change detected on {name}: {old} -> {new}', namespace = namespace) + log_level(level, message, namespace = namespace) } } assignInNamespace('shiny_input_values', new_input_values, ns = 'logger') diff --git a/man/log_shiny_input_changes.Rd b/man/log_shiny_input_changes.Rd index f2509f1a..276495cb 100644 --- a/man/log_shiny_input_changes.Rd +++ b/man/log_shiny_input_changes.Rd @@ -8,7 +8,10 @@ log_shiny_input_changes( input, level = INFO, namespace = NA_character_, - excluded_inputs = character() + excluded_inputs = character(), + allow = FALSE, + change_message = "Shiny input change detected on {name}: {old} -> {new}", + initialize_message = "Default Shiny inputs initialized:" ) } \arguments{ @@ -19,6 +22,14 @@ log_shiny_input_changes( \item{namespace}{the name of the namespace} \item{excluded_inputs}{character vector of input names to exclude from logging} + +\item{allow}{allow to be used outside Shiny app} + +\item{change_message}{custom message to be displayed during the input change - supports `glue` syntax where `{name}` +is the name of the input, and `{old}` and `{new}` are input values before the change and after the change} + +\item{initialize_message}{custom message to be displayed during input initialization (this is followed by initialized +input values being printed in JSON format)} } \description{ This is to be called in the \code{server} section of the Shiny app.