Skip to content

Commit

Permalink
503 shinytest2 fixes bug with setting numeric range (#1152)
Browse files Browse the repository at this point in the history
# Pull Request

<!--- Replace `#nnn` with your issue link for reference. -->

Fixes #1151 

#### Changes description

- `shinyWidgets::numericRangeInput` uses a custom handler and seems to
require a `js: Shiny.setInputValue` call
- ~Change of explicit arguments in `{s,g}et_active_filter_selection` to
`type` to reflect this logic and allow for further extensions.~
- Removed `is_numeric` argument in favor of auto-detection of slice type
(categorical / numerical range)
  - from `{s,g}et_active_filter_selection`

---------

Co-authored-by: 27856297+dependabot-preview[bot]@users.noreply.github.com <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: Vedha Viyash <[email protected]>
  • Loading branch information
3 people authored Mar 20, 2024
1 parent 3c8bbda commit 3a209f3
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 52 deletions.
94 changes: 67 additions & 27 deletions R/TealAppDriver.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,24 +258,28 @@ TealAppDriver <- R6::R6Class( # nolint: object_name.
#'
#' @param dataset_name (character) The name of the dataset to get the filter values from.
#' @param var_name (character) The name of the variable to get the filter values from.
#' @param is_numeric (logical) If the variable is numeric or not.
#'
#' @return The value of the active filter selection.
get_active_filter_selection = function(dataset_name, var_name, is_numeric = FALSE) {
get_active_filter_selection = function(dataset_name, var_name) {
checkmate::check_string(dataset_name)
checkmate::check_string(var_name)
checkmate::check_flag(is_numeric)
selection_suffix <- ifelse(is_numeric, "selection_manual", "selection")
self$get_value(
input = sprintf(
"%s-active-%s-filter-%s_%s-inputs-%s",
self$active_filters_ns(),
dataset_name,
dataset_name,
var_name,
selection_suffix
)
input_id_prefix <- sprintf(
"%s-active-%s-filter-%s_%s-inputs",
self$active_filters_ns(),
dataset_name,
dataset_name,
var_name
)

# Find the type of filter (categorical or range)
supported_suffix <- c("selection", "selection_manual")
for (suffix in supported_suffix) {
if (!is.null(self$get_html(sprintf("#%s-%s", input_id_prefix, suffix)))) {
return(self$get_value(input = sprintf("%s-%s", input_id_prefix, suffix)))
}
}

NULL # If there are not any supported filters
},
#' @description
#' Add a new variable from the dataset to be filtered.
Expand Down Expand Up @@ -340,27 +344,63 @@ TealAppDriver <- R6::R6Class( # nolint: object_name.
#' @param dataset_name (character) The name of the dataset to set the filter value for.
#' @param var_name (character) The name of the variable to set the filter value for.
#' @param input The value to set the filter to.
#' @param is_numeric (logical) If the variable is numeric or not.
#' @param type (character) The type of the filter to get the value from. Default is `categorical`.
#'
#' @return The `TealAppDriver` object invisibly.
set_active_filter_selection = function(dataset_name, var_name, input, is_numeric = FALSE) {
set_active_filter_selection = function(dataset_name, var_name, input) {
checkmate::check_string(dataset_name)
checkmate::check_string(var_name)
checkmate::check_string(input)
checkmate::check_flag(is_numeric)

selection_suffix <- ifelse(is_numeric, "selection_manual", "selection")
self$set_input(
sprintf(
"%s-active-%s-filter-%s_%s-inputs-%s",
self$active_filters_ns(),
dataset_name,
dataset_name,
var_name,
selection_suffix
),
input
input_id_prefix <- sprintf(
"%s-active-%s-filter-%s_%s-inputs",
self$active_filters_ns(),
dataset_name,
dataset_name,
var_name
)

# Find the type of filter (based on filter panel)
supported_suffix <- c("selection", "selection_manual")
slices_suffix <- supported_suffix[
match(
TRUE,
vapply(
supported_suffix,
function(suffix) {
!is.null(self$get_html(sprintf("#%s-%s", input_id_prefix, suffix)))
},
logical(1)
)
)
]

# Generate correct namespace
slices_input_id <- sprintf(
"%s-active-%s-filter-%s_%s-inputs-%s",
self$active_filters_ns(),
dataset_name,
dataset_name,
var_name,
slices_suffix
)

if (identical(slices_suffix, "selection_manual")) {
checkmate::assert_numeric(input, len = 2)
self$run_js(
sprintf(
"Shiny.setInputValue('%s:sw.numericRange', [%f, %f], {priority: 'event'})",
slices_input_id,
input[[1]],
input[[2]]
)
)
} else if (identical(slices_suffix, "selection")) {
self$set_input(slices_input_id, input)
} else {
stop("Filter selection set not supported for this slice.")
}

invisible(self)
},
#' @description
Expand Down
17 changes: 3 additions & 14 deletions man/TealAppDriver.Rd

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

14 changes: 3 additions & 11 deletions tests/testthat/test-shinytest2-teal_slices.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ testthat::test_that("e2e: teal_slices filters are initialized when global filter
c("4", "6")
)
testthat::expect_identical(
app$get_active_filter_selection("mtcars", "drat", is_numeric = TRUE),
app$get_active_filter_selection("mtcars", "drat"),
c(3, 4)
)
testthat::expect_identical(
Expand Down Expand Up @@ -67,7 +67,7 @@ testthat::test_that("e2e: teal_slices filters are initialized when module specif
app$get_active_filter_selection("mtcars", "cyl"),
c("4", "6")
)
testthat::expect_null(app$get_active_filter_selection("mtcars", "drat", is_numeric = TRUE))
testthat::expect_null(app$get_active_filter_selection("mtcars", "drat"))
testthat::expect_null(app$get_active_filter_selection("mtcars", "gear"))

app$navigate_teal_tab("Module_2")
Expand All @@ -80,7 +80,7 @@ testthat::test_that("e2e: teal_slices filters are initialized when module specif
c("setosa", "versicolor", "virginica")
)
testthat::expect_identical(
app$get_active_filter_selection("mtcars", "drat", is_numeric = TRUE),
app$get_active_filter_selection("mtcars", "drat"),
c(3, 4)
)
testthat::expect_identical(
Expand All @@ -89,13 +89,5 @@ testthat::test_that("e2e: teal_slices filters are initialized when module specif
)
testthat::expect_null(app$get_active_filter_selection("mtcars", "cyl"))

app$set_active_filter_selection("iris", "Species", "setosa")
app$navigate_teal_tab("Module_1")
app$wait_for_idle(timeout = default_idle_timeout)

testthat::expect_identical(
app$get_active_filter_selection("iris", "Species"),
"setosa"
)
app$stop()
})

0 comments on commit 3a209f3

Please sign in to comment.