Skip to content

Commit

Permalink
Merge pull request daroczig#155 from m7pr/shiny_extensions_alternative
Browse files Browse the repository at this point in the history
Use `session` in `log_shiny_input_changes` to detect testing environment and utilize namespaces for more informative logging
  • Loading branch information
daroczig authored Aug 7, 2024
2 parents 37d2db0 + 42fce7e commit 3ef4b65
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
13 changes: 9 additions & 4 deletions R/hooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,19 @@ log_shiny_input_changes <- function(input,

fail_on_missing_package('shiny')
fail_on_missing_package('jsonlite')
if (!shiny::isRunning()) {

session <- shiny::getDefaultReactiveDomain()
ns <- ifelse(!is.null(session), session$ns(character(0)), '')

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')
}

input_values <- shiny::isolate(shiny::reactiveValuesToList(input))
assignInMyNamespace('shiny_input_values', input_values)
log_level(level, skip_formatter(paste(
log_level(level, skip_formatter(trimws(paste(ns,
'Default Shiny inputs initialized:',
as.character(jsonlite::toJSON(input_values, auto_unbox = TRUE)))), namespace = namespace)
as.character(jsonlite::toJSON(input_values, auto_unbox = TRUE))))), namespace = namespace)

shiny::observe({
old_input_values <- shiny_input_values
Expand All @@ -129,7 +133,8 @@ 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)
message <- trimws('{ns} Shiny input change detected in {name}: {old} -> {new}')
log_level(level, message, namespace = namespace)
}
}
assignInNamespace('shiny_input_values', new_input_values, ns = 'logger')
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/_snaps/CRANSKIP-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,28 @@
Code
writeLines(obs)
Output
INFO Default Shiny inputs initialized: {}
INFO mock-session Default Shiny inputs initialized: {}

# shiny input initialization is detected with different log-level

Code
writeLines(obs)
Output
ERROR Default Shiny inputs initialized: {}
ERROR mock-session Default Shiny inputs initialized: {}

# shiny input change is detected

Code
writeLines(obs)
Output
INFO Default Shiny inputs initialized: {}
INFO Shiny input change detected on a: NULL -> 2
INFO mock-session Default Shiny inputs initialized: {}
INFO mock-session Shiny input change detected in a: NULL -> 2

# shiny input change is logged with different level

Code
writeLines(obs)
Output
ERROR Default Shiny inputs initialized: {}
ERROR Shiny input change detected on a: NULL -> 2
ERROR mock-session Default Shiny inputs initialized: {}
ERROR mock-session Shiny input change detected in a: NULL -> 2

2 changes: 1 addition & 1 deletion tests/testthat/test-CRANSKIP-hooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test_that('log_warnings', {
})

test_that('log_errors', {
expect_snapshot({
expect_snapshot({
writeLines(eval_outside("log_errors()", 'stop(42)'))
writeLines(eval_outside("log_errors()", 'foobar'))
writeLines(eval_outside("log_errors()", 'f<-function(x) {42 * "foobar"}; f()'))
Expand Down

0 comments on commit 3ef4b65

Please sign in to comment.