Skip to content

Commit

Permalink
decouple ls from parent inclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
dependabot-preview[bot] committed Sep 24, 2024
1 parent 84c31e9 commit 97f0c3c
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 39 deletions.
5 changes: 1 addition & 4 deletions R/dummy_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +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({
.teal_data_ls(req(data()))
})

datanames_rv <- reactive(ls(teal.code::get_env((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(.teal_data_ls(data)) == 0) {
if (length(ls(teal.code::get_env(data))) == 0) {
stop("The environment of `data` is empty.")
}

is_modules_ok <- check_modules_datanames(modules, .teal_data_ls(data))
is_modules_ok <- check_modules_datanames(modules, ls(teal.code::get_env(data)))
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_ls(data))
is_filter_ok <- check_filter_datanames(filter, ls(teal.code::get_env(data)))
if (!isTRUE(is_filter_ok)) {
warning(is_filter_ok)
# we allow app to continue if applied filters are outside
Expand Down
7 changes: 3 additions & 4 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(.teal_data_ls(teal_data()))) {
if (!length(ls(teal.code::get_env(teal_data())))) {
return(NULL)
}

Expand Down Expand Up @@ -143,16 +143,15 @@ get_filter_overview <- function(teal_data) {

filtered_data_objs <- sapply(
datanames,
function(name) teal.code::get_env(teal_data())[[name]],
function(name) teal.code::get_var(teal_data(), name),
simplify = FALSE
)
unfiltered_data_objs <- teal.code::get_env(teal_data())[[".raw_data"]]
unfiltered_data_objs <- teal.code::get_var(teal_data(), ".raw_data")

rows <- lapply(
datanames,
function(dataname) {
parent <- teal.data::parent(joinkeys, dataname)

# todo: what should we display for a parent dataset?
# - Obs and Subjects
# - Obs only
Expand Down
6 changes: 3 additions & 3 deletions R/module_init_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ srv_init_data <- function(id, data, modules, filter = teal_slices()) {
)
}

is_filter_ok <- check_filter_datanames(filter, .teal_data_ls(data_validated()))
is_filter_ok <- check_filter_datanames(filter, ls(teal.code::get_env(data_validated())))
if (!isTRUE(is_filter_ok)) {
showNotification(
"Some filters were not applied because of incompatibility with data. Contact app developer.",
Expand Down Expand Up @@ -154,7 +154,7 @@ srv_init_data <- function(id, data, modules, filter = teal_slices()) {
list(code = trimws(c(teal.code::get_code(data), hashes), which = "right")),
list(join_keys = teal.data::join_keys(data)),
sapply(
.teal_data_ls(data),
ls(teal.code::get_env(data)),
teal.code::get_var,
object = data,
simplify = FALSE
Expand All @@ -174,7 +174,7 @@ srv_init_data <- function(id, data, modules, filter = teal_slices()) {
#' @return A character vector with the code lines.
#' @keywords internal
#'
.get_hashes_code <- function(data, datanames = .teal_data_ls(data)) {
.get_hashes_code <- function(data, datanames = ls(teal.code::get_env(data))) {
vapply(
datanames,
function(dataname, datasets) {
Expand Down
6 changes: 3 additions & 3 deletions R/module_nested_tabs.R
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,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")) {
.teal_data_ls(data)
.topologically_sort_datanames(ls(teal.code::get_env(data)), teal.data::join_keys(data))
} else {
intersect(
include_parent_datanames(modules$datanames, teal.data::join_keys(data)),
.teal_data_ls(data)
.include_parent_datanames(modules$datanames, teal.data::join_keys(data)),
ls(teal.code::get_env(data))
)
}
}
5 changes: 4 additions & 1 deletion R/module_teal_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ srv_validate_reactive_teal_data <- function(id, # nolint: object_length

output$shiny_warnings <- renderUI({
if (inherits(data_out_r(), "teal_data")) {
is_modules_ok <- check_modules_datanames(modules = modules, datanames = .teal_data_ls(data_validated()))
is_modules_ok <- check_modules_datanames(
modules = modules,
datanames = ls(teal.code::get_env(data_validated()))
)
if (!isTRUE(is_modules_ok)) {
tags$div(
is_modules_ok$html(
Expand Down
13 changes: 2 additions & 11 deletions R/teal_data_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ NULL
.subset_teal_data <- function(data, datanames) {
checkmate::assert_class(data, "teal_data")
checkmate::assert_class(datanames, "character")
datanames_corrected <- intersect(datanames, .teal_data_ls(data))
datanames_corrected <- intersect(datanames, ls(teal.code::get_env(data)))
datanames_corrected_with_raw <- c(datanames_corrected, ".raw_data")
if (!length(datanames)) {
if (!length(datanames_corrected)) {
return(teal_data())
}

Expand All @@ -67,12 +67,3 @@ NULL
teal.data::datanames(new_data) <- datanames_corrected
new_data
}

#' @rdname teal_data_utilities
.teal_data_ls <- function(data) {
datanames <- ls(
teal.code::get_env(data),
all.names = FALSE # doesn't consider objects prefixed by `.` as datanames (including filtered datanames)
)
include_parent_datanames(datanames, teal.data::join_keys(data)) # for topological sort
}
19 changes: 14 additions & 5 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,28 @@ get_teal_bs_theme <- function() {
#' Return parentnames along with datanames.
#' @noRd
#' @keywords internal
include_parent_datanames <- function(dataname, join_keys) {
ordered_datanames <- dataname
for (i in dataname) {
.include_parent_datanames <- function(datanames, join_keys) {
ordered_datanames <- datanames
for (i in datanames) {
parents <- character(0)
while (length(i) > 0) {
parent_i <- teal.data::parent(join_keys, i)
parents <- c(parent_i, parents)
i <- parent_i
}
ordered_datanames <- c(parents, dataname, ordered_datanames)
ordered_datanames <- c(parents, ordered_datanames)
}
unique(ordered_datanames)
}

#' Return topologicaly sorted datanames
#' @noRd
#' @keywords internal
.topologically_sort_datanames <- function(datanames, join_keys) {
datanames_with_parents <- .include_parent_datanames(datanames, join_keys)
intersect(datanames, datanames_with_parents)
}

#' Create a `FilteredData`
#'
#' Create a `FilteredData` object from a `teal_data` object.
Expand All @@ -66,7 +74,7 @@ include_parent_datanames <- function(dataname, join_keys) {
#' @param datanames (`character`) vector of data set names to include; must be subset of `datanames(x)`
#' @return A `FilteredData` object.
#' @keywords internal
teal_data_to_filtered_data <- function(x, datanames = .teal_data_ls(x)) {
teal_data_to_filtered_data <- function(x, datanames = ls(teal.code::get_env(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 All @@ -79,6 +87,7 @@ teal_data_to_filtered_data <- function(x, datanames = .teal_data_ls(x)) {
)
}


#' Template function for `TealReportCard` creation and customization
#'
#' This function generates a report card with a title,
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.

2 changes: 1 addition & 1 deletion man/teal_data_to_filtered_data.Rd

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

3 changes: 0 additions & 3 deletions man/teal_data_utilities.Rd

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

0 comments on commit 97f0c3c

Please sign in to comment.