Skip to content

Commit

Permalink
- add asserts on datanames in teal::init
Browse files Browse the repository at this point in the history
- more tests
- address @chlebowa comments
  • Loading branch information
gogonzo committed Nov 9, 2023
1 parent 724dcdb commit a98682d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 13 deletions.
3 changes: 2 additions & 1 deletion R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ init <- function(data,
hashables$data <- if (inherits(hashables$data, "teal_data")) {
as.list(hashables$data@env)
} else if (inherits(data, "teal_data_module")) {
# what?
body(data$server)
} else if (hashables$data$is_pulled()) {
sapply(get_dataname(hashables$data), simplify = FALSE, function(dn) {
hashables$data$get_dataset(dn)$get_raw_data()
Expand Down Expand Up @@ -198,6 +198,7 @@ init <- function(data,
is_filter_ok <- check_filter_datanames(filter, teal.data::datanames(data))
if (!isTRUE(is_filter_ok)) {
logger::log_warn(is_filter_ok)
warning(is_filter_ok)
# we allow app to continue if applied filters are outside
# of possible data range
}
Expand Down
7 changes: 3 additions & 4 deletions R/module_teal_with_splash.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ ui_teal_with_splash <- function(id,
# Shiny app does not time out.

splash_ui <- if (inherits(data, "teal_data_module")) {
data$ui(ns("data"))
data$ui(ns("teal_data_module"))
} else if (inherits(data, "teal_data")) {
div()
} else if (inherits(data, "TealDataAbstract") && teal.data::is_pulled(data)) {
Expand Down Expand Up @@ -71,10 +71,10 @@ srv_teal_with_splash <- function(id, data, modules, filter = teal_slices()) {
# raw_data contains teal_data object
# either passed to teal::init or returned from ddl
raw_data <- if (inherits(data, "teal_data_module")) {
ddl_out <- do.call(
do.call(
data$server,
append(
list(id = "data"),
list(id = "teal_data_module"),
attr(data, "server_args") # might be NULL or list() - both are fine
),
quote = TRUE
Expand Down Expand Up @@ -130,7 +130,6 @@ srv_teal_with_splash <- function(id, data, modules, filter = teal_slices()) {
return(NULL)
}


is_modules_ok <- check_modules_datanames(modules, teal.data::datanames(data))
is_filter_ok <- check_filter_datanames(filter, teal.data::datanames(data))

Expand Down
3 changes: 3 additions & 0 deletions R/teal_data_module.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#' `shiny` `ui` module with `id` argument
#' @param server (`function(id)`)\cr
#' `shiny` server function with `id` as argument. Module should return reactive `teal_data`.
#'
#' @return object of class `teal_data_module`
#'
#' @examples
#' data <- teal_data_module(
#' ui = function(id) {
Expand Down
10 changes: 5 additions & 5 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ check_modules_datanames <- function(modules, datanames) {
extra_datanames <- setdiff(modules$datanames, c("all", datanames))
if (length(extra_datanames)) {
sprintf(
"- Module %s has a different dataname than available in a 'data': %s not in %s",
"- Module '%s' has a different dataname than available in a 'data': %s not in %s",
modules$label,
toString(dQuote(extra_datanames, q = FALSE)),
toString(dQuote(datanames, q = FALSE))
Expand All @@ -174,10 +174,10 @@ check_filter_datanames <- function(filters, datanames) {
dataname <- shiny::isolate(filter$dataname)
if (!dataname %in% datanames) {
sprintf(
"- Filter %s has a different dataname than available in a 'data':\n %s not in %s",
filter$label,
dQuote(dataname),
toString(dQuote(datanames))
"- Filter '%s' has a different dataname than available in a 'data':\n %s not in %s",
shiny::isolate(filter$id),
dQuote(dataname, q = FALSE),
toString(dQuote(datanames, q = FALSE))
)
}
}
Expand Down
3 changes: 3 additions & 0 deletions man/teal_data_module.Rd

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

19 changes: 17 additions & 2 deletions tests/testthat/test-init.R
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,20 @@ testthat::test_that("init filter accepts `teal_slices`", {
)
})

# todo: when modules datanames not matching datanames(data)
# todo: when filters datanames not matching datanames(data)
testthat::test_that("init throws when incompatible module's datanames", {
testthat::expect_error(
init(data = teal_data(mtcars = mtcars), modules = list(example_module(datanames = "iris"))),
'"iris" not in "mtcars"'
)
})

testthat::test_that("init throws when incompatible filter's datanames", {
testthat::expect_warning(
init(
data = teal_data(mtcars = mtcars),
modules = modules(example_module()),
filter = teal_slices(teal_slice(dataname = "iris", varname = "Species"))
),
'"iris" not in "mtcars"'
)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-module_teal_with_splash.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ testthat::test_that("srv_teal_with_splash raw_data_checked returns NULL if incom
testthat::expect_is(raw_data_checked, "reactive")
testthat::expect_output(
testthat::expect_null(raw_data_checked()),
"iris not in mtcars"
'"iris" not in "mtcars"'
)
}
)
Expand Down

0 comments on commit a98682d

Please sign in to comment.