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

553 introduce internal log_shiny_input_changes function #84

Merged
merged 27 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
96babba
#51 unify page version to /latest-tag/
m7pr Aug 30, 2023
9019464
Merge branch 'main' of https://github.com/insightsengineering/teal.lo…
m7pr Aug 30, 2023
06297ed
Merge branch 'main' of https://github.com/insightsengineering/teal.lo…
m7pr Sep 4, 2023
236ae0a
Merge branch 'main' of https://github.com/insightsengineering/teal.lo…
m7pr Sep 11, 2023
74d4a99
Merge branch 'main' of https://github.com/insightsengineering/teal.lo…
m7pr Jan 23, 2024
4bcab17
Merge branch 'main' of https://github.com/insightsengineering/teal.lo…
m7pr Jun 13, 2024
c3c8c82
log_shiny_input_changes
m7pr Jun 18, 2024
c01a32a
session should be defined first
m7pr Jun 18, 2024
9abdb48
[skip style] [skip vbump] Restyle files
github-actions[bot] Jun 18, 2024
c593632
release the kraken
m7pr Jun 18, 2024
921c0f7
session
m7pr Jun 18, 2024
7cd44da
export log_shiny_input_changes
m7pr Jun 18, 2024
edb3219
[skip style] [skip vbump] Restyle files
github-actions[bot] Jun 18, 2024
507b6ec
Update R/log_shiny_input_changes.R
m7pr Jun 18, 2024
1849ba3
extend pkgdown
m7pr Jun 18, 2024
fa9f826
remove jsonlite dependency
m7pr Jun 18, 2024
b66a3ee
lintr and spellcheck
m7pr Jun 18, 2024
fe680dd
Update DESCRIPTION
m7pr Jun 19, 2024
9342748
Update R/log_shiny_input_changes.R
m7pr Jun 19, 2024
4b1f292
[skip style] [skip vbump] Restyle files
github-actions[bot] Jun 19, 2024
39c7e2d
use ns as glue syntax
m7pr Jun 19, 2024
908c9ef
Merge branch '553_log_shiny_input_changes@main' of https://github.com…
m7pr Jun 19, 2024
44e144b
bring back utils solution
m7pr Jun 19, 2024
bf57809
[skip style] [skip vbump] Restyle files
github-actions[bot] Jun 19, 2024
9a52a51
excluded_pattern as singular word
m7pr Jun 19, 2024
be9ad05
Merge branch '553_log_shiny_input_changes@main' of https://github.com…
m7pr Jun 19, 2024
049e630
typo in example
m7pr Jun 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
m7pr marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Imports:
logger (>= 0.2.0),
methods,
shiny (>= 1.6.0),
utils,
m7pr marked this conversation as resolved.
Show resolved Hide resolved
withr (>= 2.1.0)
Suggests:
knitr (>= 1.42),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(log_shiny_input_changes)
export(log_system_info)
export(register_handlers)
export(register_logger)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# teal.logger 0.2.0.9003

* New function `log_shiny_input_changes` based on `logger` implementation, but curated to `teal` needs.
It allows to track all shiny inputs changes in teal modules on `TRACE` level with appended namespace name.

# teal.logger 0.2.0

* New function `register_handlers` to register global handlers for logging messages, warnings and errors.
Expand Down
71 changes: 71 additions & 0 deletions R/log_shiny_input_changes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#' Auto logging input changes in Shiny app
#'
#' This is to be called in the \code{server} section of the Shiny app.
#'
#' Function having very similar behavior as [logger::log_shiny_input_changes()] but adjusted for `teal` needs.
#'
#' @param input passed from Shiny \code{server}
#' @param excluded_inputs character vector of input names to exclude from logging
#' @param excluded_patterns character of length one including a grep pattern of names to be excluded from logging
#' @param namespace the name of the namespace
#' @examples
#' \dontrun{
#' library(shiny)
#'
#' ui <- bootstrapPage(
#' numericInput("mean1", "mean1", 0),
#' numericInput("mean2", "mean2", 0),
#' numericInput("sd", "sd", 1),
#' textInput("title", "title", "title"),
#' textInput("foo", "This is not used at all, still gets logged", "foo"),
#' passwordInput("password", "Password not to be logged", "secret"),
#' plotOutput("plot")
#' )
#'
#' server <- function(input, output) {
#' log_shiny_input_changes(input, excluded_inputs = "password", exclude_patterns = "mean")
#'
#' output$plot <- renderPlot({
#' hist(rnorm(1e3, input$mean, input$sd), main = input$title)
#' })
#' }
#'
#' shinyApp(ui = ui, server = server)
#' }
#' @export
log_shiny_input_changes <- function(
input,
namespace = NA_character_,
excluded_inputs = character(),
excluded_patterns = "_width$") {
m7pr marked this conversation as resolved.
Show resolved Hide resolved
session <- shiny::getDefaultReactiveDomain()
m7pr marked this conversation as resolved.
Show resolved Hide resolved
if (!(shiny::isRunning() || inherits(session, "MockShinySession"))) {
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)
m7pr marked this conversation as resolved.
Show resolved Hide resolved

shiny::observe({
old_input_values <- shiny_input_values
new_input_values <- shiny::reactiveValuesToList(input)
names <- unique(c(names(old_input_values), names(new_input_values)))
names <- setdiff(names, excluded_inputs)
if (length(excluded_patterns)) {
names <- grep(excluded_patterns, names, invert = TRUE, value = TRUE)
}
for (name in names) {
old <- old_input_values[name]
new <- new_input_values[name]
if (!identical(old, new)) {
message <- trimws("{ns} Shiny input change detected in {name}: {old} -> {new}")
logger::log_trace(message, namespace = namespace)
}
}
utils::assignInNamespace("shiny_input_values", new_input_values, ns = "teal.logger")
})
}
shiny_input_values <- NULL
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ reference:
- register_logger
- register_handlers
- suppress_logs
- log_shiny_input_changes
53 changes: 53 additions & 0 deletions man/log_shiny_input_changes.Rd

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

Loading