Skip to content

Commit

Permalink
chore: cleanup of @env get_env and other mentions
Browse files Browse the repository at this point in the history
  • Loading branch information
averissimo committed Nov 1, 2024
1 parent 53c423d commit 8c41179
Show file tree
Hide file tree
Showing 25 changed files with 56 additions and 75 deletions.
2 changes: 1 addition & 1 deletion R/dummy_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ example_module <- function(label = "example teal module", datanames = "all", tra
server = function(id, data) {
checkmate::assert_class(isolate(data()), "teal_data")
moduleServer(id, function(input, output, session) {
datanames_rv <- reactive(ls(teal.code::get_env((req(data())))))
datanames_rv <- reactive(ls((req(data()))))
observeEvent(datanames_rv(), {
selected <- input$dataname
if (identical(selected, "")) {
Expand Down
6 changes: 3 additions & 3 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,16 @@ init <- function(data,

## `data` - `modules`
if (inherits(data, "teal_data")) {
if (length(ls(teal.code::get_env(data))) == 0) {
if (length(ls(data)) == 0) {
stop("The environment of `data` is empty.")
}

is_modules_ok <- check_modules_datanames(modules, ls(teal.code::get_env(data)))
is_modules_ok <- check_modules_datanames(modules, ls(data))
if (!isTRUE(is_modules_ok) && length(unlist(extract_transformers(modules))) == 0) {
warning(is_modules_ok, call. = FALSE)
}

is_filter_ok <- check_filter_datanames(filter, ls(teal.code::get_env(data)))
is_filter_ok <- check_filter_datanames(filter, ls(data))
if (!isTRUE(is_filter_ok)) {
warning(is_filter_ok)
# we allow app to continue if applied filters are outside
Expand Down
5 changes: 3 additions & 2 deletions R/module_data_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ srv_data_summary <- function(id, teal_data) {

summary_table <- reactive({
req(inherits(teal_data(), "teal_data"))
if (!length(ls(teal.code::get_env(teal_data())))) {
if (!length(ls(teal_data()))) {
return(NULL)
}

Expand Down Expand Up @@ -138,7 +138,8 @@ srv_data_summary <- function(id, teal_data) {

#' @rdname module_data_summary
get_filter_overview <- function(teal_data) {
datanames <- teal.data::datanames(teal_data())
# Sort datanames in topological order
datanames <- intersect(names(teal_data()), ls(teal_data()))
joinkeys <- teal.data::join_keys(teal_data())

filtered_data_objs <- sapply(
Expand Down
4 changes: 2 additions & 2 deletions R/module_init_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ srv_init_data <- function(id, data) {
list(code = trimws(c(teal.code::get_code(data), hashes), which = "right")),
list(join_keys = teal.data::join_keys(data)),
sapply(
ls(teal.code::get_env(data)),
ls(data),
teal.code::get_var,
object = data,
simplify = FALSE
Expand All @@ -122,7 +122,7 @@ srv_init_data <- function(id, data) {
#' @return A character vector with the code lines.
#' @keywords internal
#'
.get_hashes_code <- function(data, datanames = ls(teal.code::get_env(data))) {
.get_hashes_code <- function(data, datanames = ls(data)) {
vapply(
datanames,
function(dataname, datasets) {
Expand Down
4 changes: 2 additions & 2 deletions R/module_nested_tabs.R
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,11 @@ srv_teal_module.teal_module <- function(id,
.resolve_module_datanames <- function(data, modules) {
stopifnot("data_rv must be teal_data object." = inherits(data, "teal_data"))
if (is.null(modules$datanames) || identical(modules$datanames, "all")) {
.topologically_sort_datanames(ls(teal.code::get_env(data)), teal.data::join_keys(data))
.topologically_sort_datanames(ls(data), teal.data::join_keys(data))
} else {
intersect(
.include_parent_datanames(modules$datanames, teal.data::join_keys(data)),
ls(teal.code::get_env(data))
ls(data)
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion R/module_teal.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ srv_teal <- function(id, data, modules, filter = teal_slices()) {
)
data_rv <- reactive({
req(inherits(data_validated(), "teal_data"))
is_filter_ok <- check_filter_datanames(filter, ls(teal.code::get_env(data_validated())))
is_filter_ok <- check_filter_datanames(filter, ls(data_validated()))
if (!isTRUE(is_filter_ok)) {
showNotification(
"Some filters were not applied because of incompatibility with data. Contact app developer.",
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 @@ -223,7 +223,7 @@ srv_check_shiny_warnings <- function(id, data, modules) {
output$message <- renderUI({
if (inherits(data(), "teal_data")) {
is_modules_ok <- check_modules_datanames_html(
modules = modules, datanames = ls(teal.code::get_env(data()))
modules = modules, datanames = ls(data())
)
if (!isTRUE(is_modules_ok)) {
tags$div(is_modules_ok, class = "teal-output-warning")
Expand Down
1 change: 0 additions & 1 deletion R/teal_data_module.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
#' dataset2 <- mtcars
#' }
#' )
#' datanames(data) <- c("dataset1", "dataset2")
#'
#' data
#' })
Expand Down
13 changes: 6 additions & 7 deletions R/teal_data_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#' `teal.code` and `teal.data` methods.
#'
#' @param data (`teal_data`)
#' @param code (`character`) code to append to `data@code`
#' @param objects (`list`) objects to append to `data@env`
#' @param code (`character`) code to append to the object's code slot.
#' @param objects (`list`) objects to append to object's environment.
#' @param datanames (`character`) names of the datasets
#' @return modified `teal_data`
#' @keywords internal
Expand All @@ -33,16 +33,16 @@ NULL
checkmate::assert_class(data, "teal_data")
checkmate::assert_class(objects, "list")
new_env <- list2env(objects, parent = .GlobalEnv)
rlang::env_coalesce(new_env, teal.code::get_env(data))
data@env <- new_env
rlang::env_coalesce(new_env, as.environment(data))
data@.xData <- new_env
data
}

#' @rdname teal_data_utilities
.subset_teal_data <- function(data, datanames) {
checkmate::assert_class(data, "teal_data")
checkmate::assert_class(datanames, "character")
datanames_corrected <- intersect(datanames, ls(teal.code::get_env(data)))
datanames_corrected <- intersect(datanames, ls(data))
datanames_corrected_with_raw <- c(datanames_corrected, ".raw_data")
if (!length(datanames_corrected)) {
return(teal_data())
Expand All @@ -51,14 +51,13 @@ NULL
new_data <- do.call(
teal.data::teal_data,
args = c(
mget(x = datanames_corrected_with_raw, envir = teal.code::get_env(data)),
mget(x = datanames_corrected_with_raw, envir = as.environment(data)),
list(
code = teal.code::get_code(data, names = datanames_corrected_with_raw),
join_keys = teal.data::join_keys(data)[datanames_corrected]
)
)
)
new_data@verified <- data@verified
teal.data::datanames(new_data) <- datanames_corrected
new_data
}
6 changes: 3 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ get_teal_bs_theme <- function() {
#' Create a `FilteredData` object from a `teal_data` object.
#'
#' @param x (`teal_data`) object
#' @param datanames (`character`) vector of data set names to include; must be subset of `datanames(x)`
#' @param datanames (`character`) vector of data set names to include; must be subset of `ls(x)`
#' @return A `FilteredData` object.
#' @keywords internal
teal_data_to_filtered_data <- function(x, datanames = ls(teal.code::get_env(x))) {
teal_data_to_filtered_data <- function(x, datanames = ls(x)) {
checkmate::assert_class(x, "teal_data")
checkmate::assert_character(datanames, min.chars = 1L, any.missing = FALSE)
# Otherwise, FilteredData will be created in the modules' scope later
Expand Down Expand Up @@ -328,7 +328,7 @@ create_app_id <- function(data, modules) {
checkmate::assert_class(modules, "teal_modules")

data <- if (inherits(data, "teal_data")) {
as.list(teal.code::get_env(data))
as.list(data)
} else if (inherits(data, "teal_data_module")) {
deparse1(body(data$server))
}
Expand Down
2 changes: 1 addition & 1 deletion man/dot-get_hashes_code.Rd

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

1 change: 0 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.

4 changes: 2 additions & 2 deletions man/teal_data_to_filtered_data.Rd

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

4 changes: 2 additions & 2 deletions man/teal_data_utilities.Rd

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

3 changes: 1 addition & 2 deletions tests/testthat/helper-shinytest2.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ simple_teal_data <- function() {
iris <- iris
mtcars <- mtcars
})
datanames(data) <- c("iris", "mtcars")
data
}

Expand All @@ -17,7 +16,7 @@ report_module <- function(label = "example teal module") {
reporter = reporter,
card_fun = function(card) card
)
updateSelectInput(session, "dataname", choices = isolate(datanames(data())))
updateSelectInput(session, "dataname", choices = isolate(ls(data())))
output$dataset <- renderPrint({
req(input$dataname)
data()[[input$dataname]]
Expand Down
39 changes: 18 additions & 21 deletions tests/testthat/test-module_teal.R
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,9 @@ testthat::describe("srv_teal teal_modules", {
expr = {
testthat::expect_null(modules_output$module_1())
session$setInputs(`data-teal_data_module-dataset` = "iris", `teal_modules-active_tab` = "module_1")
testthat::expect_setequal(ls(teal.code::get_env(modules_output$module_1()())), "iris")
testthat::expect_setequal(ls(modules_output$module_1()()), "iris")
session$setInputs(`data-teal_data_module-dataset` = "mtcars", `teal_modules-active_tab` = "module_2")
testthat::expect_setequal(ls(teal.code::get_env(modules_output$module_2()())), "mtcars")
testthat::expect_setequal(ls(modules_output$module_2()()), "mtcars")
}
)
})
Expand Down Expand Up @@ -545,7 +545,7 @@ testthat::describe("srv_teal teal_modules", {
),
expr = {
session$setInputs(`teal_modules-active_tab` = "module_1")
testthat::expect_identical(teal.data::datanames(modules_output$module_1()()), "iris")
testthat::expect_identical(ls(modules_output$module_1()()), "iris")
testthat::expect_identical(modules_output$module_1()()[["iris"]], iris)
}
)
Expand Down Expand Up @@ -675,12 +675,12 @@ testthat::describe("srv_teal teal_modules", {
),
expr = {
session$setInputs(`teal_modules-active_tab` = "module_1")
testthat::expect_identical(teal.data::datanames(modules_output$module_1()()), "iris")
testthat::expect_identical(ls(modules_output$module_1()()), "iris")
}
)
})

testthat::it("receives all objects from @env when module$datanames = \"all\"", {
testthat::it("receives all objects from teal_data when module$datanames = \"all\"", {
shiny::testServer(
app = srv_teal,
args = list(
Expand All @@ -696,7 +696,7 @@ testthat::describe("srv_teal teal_modules", {
expr = {
session$setInputs(`teal_modules-active_tab` = "module_1")
testthat::expect_identical(
teal.data::datanames(modules_output$module_1()()),
ls(modules_output$module_1()()),
c("iris", "iris_raw", "mtcars", "swiss")
)
}
Expand Down Expand Up @@ -760,7 +760,7 @@ testthat::describe("srv_teal teal_modules", {
),
expr = {
session$setInputs(`teal_modules-active_tab` = "module_1")
testthat::expect_identical(teal.data::datanames(modules_output$module_1()()), c("iris", "mtcars", "swiss"))
testthat::expect_identical(ls(modules_output$module_1()()), c("iris", "mtcars", "swiss"))
}
)
})
Expand Down Expand Up @@ -797,7 +797,7 @@ testthat::describe("srv_teal teal_modules", {
),
expr = {
session$setInputs(`teal_modules-active_tab` = "module_1")
testthat::expect_identical(teal.data::datanames(modules_output$module_1()()), c("iris", "mtcars", "swiss"))
testthat::expect_identical(ls(modules_output$module_1()()), c("iris", "mtcars", "swiss"))
}
)
})
Expand Down Expand Up @@ -858,7 +858,7 @@ testthat::describe("srv_teal teal_modules", {
),
expr = {
session$setInputs(`teal_modules-active_tab` = "module_1")
testthat::expect_identical(teal.data::datanames(modules_output$module_1()()), c("iris", "mtcars", "swiss"))
testthat::expect_identical(ls(modules_output$module_1()()), c("iris", "mtcars", "swiss"))
}
)
})
Expand Down Expand Up @@ -896,7 +896,7 @@ testthat::describe("srv_teal teal_modules", {
),
expr = {
session$setInputs(`teal_modules-active_tab` = "module_1")
testthat::expect_identical(teal.data::datanames(modules_output$module_1()()), c("iris", "mtcars"))
testthat::expect_identical(ls(modules_output$module_1()()), c("iris", "mtcars"))
}
)
})
Expand Down Expand Up @@ -2165,7 +2165,10 @@ testthat::describe("srv_teal summary table", {
})

testthat::it("displays parent before child when join_keys are provided", {
data <- teal.data::teal_data(parent = mtcars, child = data.frame(am = c(0, 1), test = c("a", "b")))
data <- teal.data::teal_data(
parent = mtcars,
child = data.frame(am = c(0, 1), test = c("a", "b"))
)

teal.data::join_keys(data) <- teal.data::join_keys(
teal.data::join_key("parent", "child", keys = c("am"))
Expand Down Expand Up @@ -2324,10 +2327,7 @@ testthat::describe("Datanames with special symbols", {
session$flushReact()

testthat::expect_setequal(
ls(
teal.code::get_env(modules_output$module_1()()),
all.names = TRUE
),
names(modules_output$module_1()()),
c(".raw_data", "iris", "%a_pipe%")
)
}
Expand Down Expand Up @@ -2355,11 +2355,9 @@ testthat::describe("Datanames with special symbols", {
expr = {
session$setInputs("teal_modules-active_tab" = "module_1")
session$flushReact()

testthat::expect_setequal(
ls(
teal.code::get_env(modules_output$module_1()()),
all.names = TRUE
),
names(modules_output$module_1()()),
c(".raw_data", "iris", "_a variable with spaces_")
)
}
Expand Down Expand Up @@ -2451,7 +2449,7 @@ testthat::describe("teal.data code with a function defined", {
}
})),
modules = modules(module("module_1", server = function(id, data) data))
), ,
),
expr = {
session$setInputs("teal_modules-active_tab" = "module_1")
session$flushReact()
Expand All @@ -2471,7 +2469,6 @@ testthat::describe("teal.data code with a function defined", {
envir = local_env
)
local(hash <- rlang::hash(deparse1(fun)), envir = local_env)

testthat::expect_setequal(
trimws(strsplit(
x = teal.code::get_code(modules_output$module_1()()),
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test-shinytest2-data_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ testthat::test_that(
}
)

datanames(data) <- c("CO2", "iris", "miniACC", "mtcars2", "mtcars1", "factors")

teal.data::join_keys(data) <- teal.data::join_keys(
teal.data::join_key("mtcars2", "mtcars1", keys = c("am"))
)
Expand All @@ -135,7 +133,7 @@ testthat::test_that(
}
)

testthat::test_that("e2e: data summary displays datasets by datanames() order if no join_keys", {
testthat::test_that("e2e: data summary displays datasets by ls() order if no join_keys", {
skip_if_too_deep(5)

data <- teal.data::teal_data(mtcars1 = mtcars, mtcars2 = data.frame(am = c(0, 1), test = c("a", "b")))
Expand Down
Loading

0 comments on commit 8c41179

Please sign in to comment.