Skip to content

Commit

Permalink
@chlebowa @ruckip review
Browse files Browse the repository at this point in the history
  • Loading branch information
gogonzo committed Nov 10, 2023
1 parent 9d63d40 commit f84ad0b
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 34 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### New features

* `data` argument in `init` accepts now `teal_data` and `teal_data_module`.
* `data` argument in `init` now accepts `teal_data` and `teal_data_module`.
* Added `landing_popup_module` function which creates a module that will display a popup when the app starts. The popup will block access to the app until it is dismissed.
* Filter state snapshots can now be uploaded from file. See `?snapshot`.

Expand Down
7 changes: 2 additions & 5 deletions R/dummy_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ example_datasets <- function() { # nolint
#'
#' @description `r lifecycle::badge("experimental")`
#' @inheritParams module
#' @param src (`logical(1)`) whether to display reproducible R code in the module.
#' @return A `teal` module which can be included in the `modules` argument to [teal::init()].
#' @examples
#' app <- init(
Expand All @@ -89,7 +88,7 @@ example_datasets <- function() { # nolint
#' shinyApp(app$ui, app$server)
#' }
#' @export
example_module <- function(label = "example teal module", datanames = "all", src = TRUE) {
example_module <- function(label = "example teal module", datanames = "all") {
checkmate::assert_string(label)
module(
label,
Expand All @@ -110,9 +109,7 @@ example_module <- function(label = "example teal module", datanames = "all", src
output = verbatimTextOutput(ns("text")),
encoding = div(
selectInput(ns("dataname"), "Choose a dataset", choices = names(data)),
if (src) {
teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")
}
teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")
)
)
},
Expand Down
5 changes: 4 additions & 1 deletion R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ init <- function(data,
}

if (inherits(data, "teal_data")) {
if (length(teal.data::datanames(data)) == 0) {
stop("`data` object has no datanames. Specify `datanames(data)` and try again.")
}

# in case of teal_data_module this check is postponed to the srv_teal_with_splash
is_modules_ok <- check_modules_datanames(modules, teal.data::datanames(data))
if (!isTRUE(is_modules_ok)) {
Expand All @@ -200,7 +204,6 @@ 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
19 changes: 11 additions & 8 deletions R/module_teal_with_splash.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ srv_teal_with_splash <- function(id, data, modules, filter = teal_slices()) {
}

if (!is.reactive(raw_data)) {
stop("The delayed loading module has to return a reactive object.")
stop("The `teal_data_module` has to return a reactive object.", call. = FALSE)
}

raw_data_checked <- reactive({
Expand All @@ -128,9 +128,9 @@ srv_teal_with_splash <- function(id, data, modules, filter = teal_slices()) {
need(
FALSE,
paste(
"Error when executing `teal_data_module`:\n",
"Error when executing `teal_data_module`:\n ",
data$message,
"\n Check your inputs or contact app developer if error persist"
"\n Check your inputs or contact app developer if error persists"
)
)
)
Expand All @@ -142,9 +142,9 @@ srv_teal_with_splash <- function(id, data, modules, filter = teal_slices()) {
need(
FALSE,
paste0(
"Error when executing `teal_data_module`:",
"Error when executing `teal_data_module`:\n ",
attr(data, "condition")$message,
"\n Check your inputs or contact app developer if error persist"
"\n Check your inputs or contact app developer if error persists"
)
)
)
Expand All @@ -154,19 +154,22 @@ srv_teal_with_splash <- function(id, data, modules, filter = teal_slices()) {
need(
inherits(data, "teal_data"),
paste(
"Error: `teal_data_module` didn't return `teal_data` object",
"\n Check your inputs or contact app developer if error persist"
"Error: `teal_data_module` did not return `teal_data` object",
"\n Check your inputs or contact app developer if error persists"
)
)
)

validate(need(teal.data::datanames(data), "Data has no datanames. Contact app developer"))


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

validate(need(isTRUE(is_modules_ok), is_modules_ok))

if (!isTRUE(is_filter_ok)) {
showNotification(is_filter_ok, type = "warning")
showNotification(is_filter_ok, type = "warning", duration = 10)
logger::log_warn(is_filter_ok)
}

Expand Down
14 changes: 8 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ check_modules_datanames <- function(modules, datanames) {
extra_datanames <- setdiff(modules$datanames, c("all", datanames))
if (length(extra_datanames)) {
sprintf(
"- Module '%s' uses different datanames than available in the 'data': (%s) not in (%s)",
"- Module '%s' uses datanames not available in the 'data': (%s) not in (%s)",
modules$label,
toString(dQuote(extra_datanames, q = FALSE)),
toString(dQuote(datanames, q = FALSE))
Expand All @@ -178,7 +178,7 @@ check_modules_datanames <- function(modules, datanames) {

#' Check `datanames` in filters
#'
#' This function check `datanames` in filters correspond to those in `data`,
#' This function checks whether `datanames` in filters correspond to those in `data`,
#' returning character vector with error messages or TRUE if all checks pass.
#'
#' @param filters (`teal_slices`) object
Expand All @@ -188,20 +188,22 @@ check_modules_datanames <- function(modules, datanames) {
#' @keywords internal
check_filter_datanames <- function(filters, datanames) {
# check teal_slices against datanames
out <- sapply(
out <- unlist(sapply(
filters, function(filter) {
dataname <- shiny::isolate(filter$dataname)
if (!dataname %in% datanames) {
sprintf(
"- Filter '%s' refers to dataname that in unavailable to 'data':\n %s not in (%s)",
"- Filter '%s' refers to dataname not available in 'data':\n %s not in (%s)",
shiny::isolate(filter$id),
dQuote(dataname, q = FALSE),
toString(dQuote(datanames, q = FALSE))
)
}
}
)
if (length(unlist(out))) {
))


if (length(out)) {
paste(out, collapse = "\n")
} else {
TRUE
Expand Down
2 changes: 1 addition & 1 deletion man/check_filter_datanames.Rd

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

4 changes: 1 addition & 3 deletions man/example_module.Rd

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

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

testthat::test_that("init throws when incompatible module's datanames", {
testthat::test_that("init throws when data has no datanames", {
testthat::expect_error(
init(data = teal_data(mtcars = mtcars), modules = list(example_module(datanames = "iris"))),
"Module 'example teal module' uses different datanames than available in the 'data'"
init(data = teal_data(), modules = list(example_module())),
"has no datanames"
)
})

testthat::test_that("init throws when incompatible module's datanames", {
msg <- "Module 'example teal module' uses datanames not available in the 'data'"
testthat::expect_output(
testthat::expect_error(
init(data = teal_data(mtcars = mtcars), modules = list(example_module(datanames = "iris"))),
msg
),
msg
)
})

testthat::test_that("init throws when incompatible filter's datanames", {
testthat::expect_warning(
testthat::expect_output(
init(
data = teal_data(mtcars = mtcars),
modules = modules(example_module()),
filter = teal_slices(teal_slice(dataname = "iris", varname = "Species"))
),
"Filter 'iris Species' refers to dataname that in unavailable to 'data'"
"Filter 'iris Species' refers to dataname not available in 'data'"
)
})
22 changes: 18 additions & 4 deletions tests/testthat/test-module_teal_with_splash.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ testthat::test_that("srv_teal_with_splash throws when teal_data_module doesn't r
),
expr = {}
),
"The delayed loading module has to return a reactive object."
"The `teal_data_module` has to return a reactive object."
)
})

Expand All @@ -47,7 +47,7 @@ testthat::test_that("srv_teal_with_splash passes teal_data to reactiveVal", {
app = srv_teal_with_splash,
args = list(
id = "test",
data = teal_data(),
data = teal_data(iris = iris),
modules = modules(example_module())
),
expr = {
Expand All @@ -57,6 +57,20 @@ testthat::test_that("srv_teal_with_splash passes teal_data to reactiveVal", {
)
})

testthat::test_that("srv_teal_with_splash throws when datanames are empty", {
shiny::testServer(
app = srv_teal_with_splash,
args = list(
id = "test",
data = teal_data(),
modules = modules(example_module())
),
expr = {
testthat::expect_error(raw_data_checked(), "Data has no datanames")
}
)
})

testthat::test_that("srv_teal_with_splash raw_data_checked throws when teal_data_module returns error", {
shiny::testServer(
app = srv_teal_with_splash,
Expand Down Expand Up @@ -106,7 +120,7 @@ testthat::test_that("srv_teal_with_splash raw_data_checked throws when teal_data
),
expr = {
testthat::expect_is(raw_data_checked, "reactive")
testthat::expect_error(raw_data_checked(), "didn't return `teal_data`")
testthat::expect_error(raw_data_checked(), "did not return `teal_data`")
}
)
})
Expand Down Expand Up @@ -158,7 +172,7 @@ testthat::test_that("srv_teal_with_splash raw_data_checked throws when incompati
testthat::expect_is(raw_data_checked, "reactive")
testthat::expect_error(
raw_data_checked(),
"Module 'example teal module' uses different datanames than available in the 'data'"
"Module 'example teal module' uses datanames not available in the 'data'"
)
}
)
Expand Down

0 comments on commit f84ad0b

Please sign in to comment.