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

1299 extend extra_datanames check with names from transformers #1320

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
4 changes: 2 additions & 2 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ init <- function(data,
}

is_modules_ok <- check_modules_datanames(modules, .teal_data_datanames(data))
if (!isTRUE(is_modules_ok)) {
lapply(is_modules_ok$string, logger::log_warn)
if (!isTRUE(is_modules_ok) && length(unlist(extract_transformers(modules))) == 0) {
lapply(is_modules_ok$string, warning, call. = FALSE)
}

is_filter_ok <- check_filter_datanames(filter, .teal_data_datanames(data))
Expand Down
2 changes: 1 addition & 1 deletion R/module_teal_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ srv_validate_reactive_teal_data <- function(id, # nolint: object_length
output$shiny_warnings <- renderUI({
if (inherits(data_out_rv(), "teal_data")) {
is_modules_ok <- check_modules_datanames(modules = modules, datanames = .teal_data_ls(data_validated()))
if (!isTRUE(is_modules_ok)) {
if (!isTRUE(is_modules_ok) && length(unlist(extract_transformers(modules))) == 0) {
tags$div(
is_modules_ok$html(
# Show modules prefix on message only in teal_data_module tab
Expand Down
14 changes: 14 additions & 0 deletions R/teal_data_module.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,17 @@ teal_transform_module <- function(ui, server, label = "transform module") {
class = c("teal_transform_module", "teal_data_module")
)
}


#' Extract all `transformers` from `modules`.
#'
#' @param modules `teal_modules` or `teal_module`
#' @return A list of `teal_transform_module` nested in the same way as input `modules`.
#' @keywords internal
extract_transformers <- function(modules) {
if (inherits(modules, "teal_module")) {
modules$transformers
} else if (inherits(modules, "teal_modules")) {
lapply(modules$children, extract_transformers)
}
}
5 changes: 4 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ check_modules_datanames <- function(modules, datanames) {
}
)
} else {
extra_datanames <- setdiff(modules$datanames, c("all", datanames))
extra_datanames <- unique(c(
setdiff(modules$datanames, c("all", datanames)),
setdiff(unlist(lapply(modules$transformers, function(x) x$datanames)), c("all", datanames))
))
if (length(extra_datanames)) {
list(
string = build_datanames_error_message(
Expand Down
18 changes: 18 additions & 0 deletions man/extract_transformers.Rd

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

47 changes: 37 additions & 10 deletions tests/testthat/test-init.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,44 @@ testthat::test_that("init throws when an empty `data` is used", {
)
})

testthat::test_that("init throws warning when datanames in modules incompatible w/ datanames in data", {
testthat::local_mocked_bindings(log_warn = warning, .package = "logger")
testthat::test_that(
"init throws warning when datanames in modules incompatible w/ datanames in data and there is no transformers",
{
testthat::expect_warning(
init(
data = teal.data::teal_data(mtcars = mtcars),
modules = list(example_module(datanames = "iris"))
),
"Dataset \"iris\" is missing for tab 'example teal module'. Dataset available in data: \"mtcars\"."
)
}
)

testthat::expect_warning(
init(
data = teal.data::teal_data(mtcars = mtcars),
modules = list(example_module(datanames = "iris"))
),
"Dataset \"iris\" is missing for tab 'example teal module'. Dataset available in data: \"mtcars\"."
)
})
testthat::test_that(
"init does not throw warning when datanames in modules incompatible w/ datanames in data and there are transformers",
{
testthat::expect_no_warning(
init(
data = teal.data::teal_data(mtcars = mtcars),
modules = list(
example_module(
datanames = "iris",
transformers = list(
teal_transform_module(
ui = function(id) NULL,
server = function(id, data) {
moduleServer(id, function(input, output, session) {
NULL
})
}
)
)
)
)
)
)
}
)

testthat::test_that("init throws when dataname in filter incompatible w/ datanames in data", {
testthat::expect_warning(
Expand Down
36 changes: 36 additions & 0 deletions tests/testthat/test-module_teal.R
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,42 @@ testthat::describe("srv_teal teal_modules", {
)
})

testthat::it("does not throw a warning when datanames are insufficient but transformers are provided", {
testthat::skip_if_not_installed("rvest")
shiny::testServer(
app = srv_teal,
args = list(
id = "test",
data = teal_data(mtcars = mtcars),
modules = modules(
module(
"module_1",
server = function(id, data) data,
datanames = c("iris"),
transformers = list(
teal_transform_module(
ui = function(id) NULL,
server = function(id, data) {
moduleServer(id, function(input, output, session) {
reactive({
NULL
})
})
}
)
)
)
)
),
expr = {
session$setInputs(`teal_modules-active_tab` = "module_1")
testthat::expect_null(
output[["teal_modules-module_1-validate_datanames-shiny_warnings"]]
)
}
)
})

testthat::it("is called and receives data even if datanames in `teal_data` are not sufficient", {
data <- teal_data(iris = iris)
teal.data::datanames(data) <- "iris"
Expand Down
Loading