Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix log shiny inputs@main #86

Merged
merged 15 commits into from
Sep 9, 2024
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ Encoding: UTF-8
Language: en-US
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
38 changes: 20 additions & 18 deletions R/log_shiny_input_changes.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @param excluded_inputs character vector of input names to exclude from logging
#' @param excluded_pattern character of length one including a grep pattern of names to be excluded from logging
#' @param namespace the name of the namespace
#' @param session the Shiny session
#' @examples
#' \dontrun{
#' library(shiny)
Expand Down Expand Up @@ -37,35 +38,36 @@ log_shiny_input_changes <- function(
input,
namespace = NA_character_,
excluded_inputs = character(),
excluded_pattern = "_width$") {
session <- shiny::getDefaultReactiveDomain()
excluded_pattern = "_width$",
gogonzo marked this conversation as resolved.
Show resolved Hide resolved
session = shiny::getDefaultReactiveDomain()) {
if (!(shiny::isRunning() || inherits(session, "MockShinySession") || inherits(session, "session_proxy"))) {
stop("No Shiny app running, it makes no sense to call this function outside of a Shiny app")
}
ns <- ifelse(!is.null(session), session$ns(character(0)), "")

# utils::assignInMyNamespace and utils::assignInNamespace are needed
# so that observer is executed only once, not twice.
input_values <- shiny::isolate(shiny::reactiveValuesToList(input))
utils::assignInMyNamespace("shiny_input_values", input_values)
if (logger::TRACE > logger::as.loglevel(get_val("TEAL.LOG_LEVEL", "teal.log_level", "INFO"))) {
gogonzo marked this conversation as resolved.
Show resolved Hide resolved
# to avoid setting observers when not needed
return(invisible(NULL))
}

ns <- ifelse(!is.null(session), session$ns(character(0)), "")
pawelru marked this conversation as resolved.
Show resolved Hide resolved
reactive_input_list <- shiny::reactive({
input_list <- shiny::reactiveValuesToList(input)
input_list[!grepl(excluded_pattern, names(input_list))]
})
shiny_input_values <- shiny::reactiveVal(shiny::isolate(reactive_input_list()))
gogonzo marked this conversation as resolved.
Show resolved Hide resolved

shiny::observe({
old_input_values <- shiny_input_values
new_input_values <- shiny::reactiveValuesToList(input)
shiny::observeEvent(reactive_input_list(), {
old_input_values <- shiny_input_values()
new_input_values <- reactive_input_list()
names <- unique(c(names(old_input_values), names(new_input_values)))
pawelru marked this conversation as resolved.
Show resolved Hide resolved
names <- setdiff(names, excluded_inputs)
if (length(excluded_pattern)) {
names <- grep(excluded_pattern, names, invert = TRUE, value = TRUE)
}
for (name in names) {
old <- old_input_values[name]
new <- new_input_values[name]
old <- unname(old_input_values[name])
pawelru marked this conversation as resolved.
Show resolved Hide resolved
new <- unname(new_input_values[name])
if (!identical(old, new)) {
message <- trimws("{ns} Shiny input change detected in {name}: {old} -> {new}")
pawelru marked this conversation as resolved.
Show resolved Hide resolved
gogonzo marked this conversation as resolved.
Show resolved Hide resolved
logger::log_trace(message, namespace = namespace)
}
}
utils::assignInNamespace("shiny_input_values", new_input_values, ns = "teal.logger")
shiny_input_values(new_input_values)
})
}
shiny_input_values <- NULL
5 changes: 4 additions & 1 deletion man/log_shiny_input_changes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading