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

Adds warning on reserved datanames ("all") #1416

Merged
merged 10 commits into from
Nov 18, 2024
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Possibility to download lockfile to restore app session for reproducibility. #479
* Introduced a function `set_datanames()` to change a `datanames` of the `teal_module`.
* Datasets which name starts with `.` are ignored when `module`'s `datanames` is set as `"all"`.
* Added warning when reserved `datanames`, such as `all` and `.raw_data` are being used.

### Breaking changes

Expand Down
55 changes: 52 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,40 @@ check_modules_datanames <- function(modules, datanames) {
}

#' @rdname check_modules_datanames
check_modules_datanames_html <- function(modules,
datanames) {
check_reserved_datanames <- function(datanames) {
reserved_datanames <- datanames[datanames %in% c("all", ".raw_data")]
if (length(reserved_datanames) == 0L) {
return(NULL)
}

tags$span(
to_html_code_list(reserved_datanames),
sprintf(
"%s reserved for internal use. Please avoid using %s as %s.",
pluralize(reserved_datanames, "is", "are"),
pluralize(reserved_datanames, "it", "them"),
pluralize(reserved_datanames, "a dataset name", "dataset names")
)
)
}

#' @rdname check_modules_datanames
check_modules_datanames_html <- function(modules, datanames) {
check_datanames <- check_modules_datanames_recursive(modules, datanames)
show_module_info <- inherits(modules, "teal_modules") # used in two contexts - module and app

reserved_datanames <- check_reserved_datanames(datanames)

if (!length(check_datanames)) {
return(TRUE)
out <- if (is.null(reserved_datanames)) {
TRUE
} else {
reserved_datanames
}
return(out)
llrs-roche marked this conversation as resolved.
Show resolved Hide resolved
}
shiny::tagList(
reserved_datanames,
lapply(
check_datanames,
function(mod) {
Expand Down Expand Up @@ -445,3 +471,26 @@ build_datanames_error_message <- function(label = NULL,
}
)
}

#' Pluralize a word depending on the size of the input
#'
#' @param x (`object`) to check length for plural.
#' @param singular (`character`) singular form of the word.
#' @param plural (optional `character`) plural form of the word. If not given an "s"
#' is added to the singular form.
#'
#' @return A `character` that correctly represents the size of the `x` argument.
#' @keywords internal
pluralize <- function(x, singular, plural = NULL) {
llrs-roche marked this conversation as resolved.
Show resolved Hide resolved
checkmate::assert_string(singular)
checkmate::assert_string(plural, null.ok = TRUE)
if (length(x) == 1L) { # Zero length object should use plural form.
singular
} else {
if (is.null(plural)) {
sprintf("%ss", singular)
} else {
plural
}
}
}
3 changes: 3 additions & 0 deletions man/check_modules_datanames.Rd

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

23 changes: 23 additions & 0 deletions man/pluralize.Rd

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

4 changes: 3 additions & 1 deletion man/teal_data_module.Rd

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

28 changes: 28 additions & 0 deletions tests/testthat/test-module_teal.R
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,35 @@ testthat::describe("srv_teal teal_modules", {
)
})


testthat::describe("warnings on missing datanames", {
testthat::it("warns when dataname is not available", {
testthat::skip_if_not_installed("rvest")
shiny::testServer(
app = srv_teal,
args = list(
id = "test",
data = teal_data(iris = iris, all = mtcars),
modules = modules(
module("module_1", server = function(id, data) data)
)
),
expr = {
session$setInputs("teal_modules-active_tab" = "module_1")
testthat::expect_equal(
trimws(
rvest::html_text2(
rvest::read_html(
output[["teal_modules-module_1-validate_datanames-shiny_warnings-message"]]$html
)
)
),
"all is reserved for internal use. Please avoid using it as a dataset name."
)
}
)
})

testthat::it("warns when dataname is not available", {
testthat::skip_if_not_installed("rvest")
shiny::testServer(
Expand Down
Loading