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

Introduce teal_data class #925

Merged
merged 29 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
5c17ccf
following new teal_data class
gogonzo Oct 4, 2023
4051ded
reverting breaking changes and supporting teal_data
gogonzo Oct 5, 2023
c3eab5a
filtered data constructor in teal internals
gogonzo Oct 6, 2023
8164671
new_teal_data env to data
gogonzo Oct 6, 2023
354a79d
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Oct 6, 2023
0185fb7
Merge branch 'main' into teal_data@main
gogonzo Oct 17, 2023
34bad8c
fix tests
gogonzo Oct 19, 2023
e8c9d86
Merge remote-tracking branch 'origin/main' into teal_data@main
gogonzo Oct 19, 2023
8b3a082
Merge remote-tracking branch 'origin/main' into teal_data@main
gogonzo Oct 23, 2023
fc83084
fix 1 after merge
gogonzo Oct 23, 2023
2f11384
fix 2 after merge
gogonzo Oct 23, 2023
ec575f9
teal_data instead of new_teal_data
gogonzo Oct 23, 2023
a256bbd
Merge remote-tracking branch 'origin/main' into teal_data@main
gogonzo Oct 25, 2023
c1cf841
remove generic get_join_keys (duplicated with teal.data)
gogonzo Oct 25, 2023
29d171c
add docs
gogonzo Oct 25, 2023
62cb664
Merge 29d171cd631a655cd53df650810987ec9321a360 into 8b2653dc1b7f67120…
gogonzo Oct 25, 2023
a035f84
[skip actions] Restyle files
github-actions[bot] Oct 25, 2023
c7ce543
fix hashing of ddl
gogonzo Oct 25, 2023
96f8b1f
Merge branch 'main' into teal_data@main
gogonzo Oct 26, 2023
1e061f1
fix pkgdown
gogonzo Oct 26, 2023
56424e9
fix pkgdown
gogonzo Oct 27, 2023
6ec2c22
resolve_modules_datanames to utils.R
gogonzo Oct 27, 2023
77d9702
Merge 6ec2c22d9c6bc559f5aa4f8bb5e1019afa41c00c into a3cbbe13498c8a9ab…
gogonzo Oct 27, 2023
45d2985
[skip actions] Restyle files
github-actions[bot] Oct 27, 2023
1a6e11d
rerun
gogonzo Oct 27, 2023
e5ef022
fix spelling
gogonzo Oct 27, 2023
c593734
skipping lint of a long function
gogonzo Oct 27, 2023
2b13b23
addressing old comments
gogonzo Oct 27, 2023
2d688f2
fix typo
chlebowa Oct 30, 2023
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
25 changes: 20 additions & 5 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ init <- function(data,
footer = tags$p(),
id = character(0)) {
logger::log_trace("init initializing teal app with: data ({ class(data)[1] }).")
data <- teal.data::to_relational_data(data = data)

checkmate::assert_class(data, "TealData")
if (!inherits(data, c("TealData", "teal_data"))) {
data <- teal.data::to_relational_data(data = data)
}
pawelru marked this conversation as resolved.
Show resolved Hide resolved
checkmate::assert_multi_class(data, c("TealData", "teal_data"))
chlebowa marked this conversation as resolved.
Show resolved Hide resolved
checkmate::assert_multi_class(modules, c("teal_module", "list", "teal_modules"))
checkmate::assert_string(title, null.ok = TRUE)
checkmate::assert(
Expand All @@ -138,7 +140,7 @@ init <- function(data,

# resolve modules datanames
datanames <- teal.data::get_dataname(data)
join_keys <- data$get_join_keys()
join_keys <- teal.data::get_join_keys(data)
gogonzo marked this conversation as resolved.
Show resolved Hide resolved
resolve_modules_datanames <- function(modules) {
gogonzo marked this conversation as resolved.
Show resolved Hide resolved
if (inherits(modules, "teal_modules")) {
modules$children <- sapply(modules$children, resolve_modules_datanames, simplify = FALSE)
Expand All @@ -147,6 +149,17 @@ init <- function(data,
modules$datanames <- if (identical(modules$datanames, "all")) {
datanames
} else if (is.character(modules$datanames)) {
extra_datanames <- setdiff(modules$datanames, datanames)
if (length(extra_datanames)) {
stop(
sprintf(
"Module %s has datanames that are not available in a 'data':\n %s not in %s",
modules$label,
toString(extra_datanames),
toString(datanames)
)
)
}
datanames_adjusted <- intersect(modules$datanames, datanames)
include_parent_datanames(dataname = datanames_adjusted, join_keys = join_keys)
chlebowa marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -212,8 +225,10 @@ init <- function(data,
res <- list(
ui = ui_teal_with_splash(id = id, data = data, title = title, header = header, footer = footer),
server = function(input, output, session) {
# copy object so that load won't be shared between the session
data <- data$copy(deep = TRUE)
if (inherits(data, "TealDataAbstract")) {
# copy TealData so that load won't be shared between the session
data <- data$copy(deep = TRUE)
}
filter <- deep_copy_filter(filter)
srv_teal_with_splash(id = id, data = data, modules = modules, filter = filter)
}
Expand Down
14 changes: 8 additions & 6 deletions R/module_nested_tabs.R
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,14 @@ srv_nested_tabs.teal_module <- function(id, datasets, modules, is_module_specifi
data,
eventReactive(
trigger_data(),
c(
get_rcode_str_install(),
get_rcode_libraries(),
get_datasets_code(datanames, datasets, hashes),
teal.slice::get_filter_expr(datasets, datanames)
)
{
c(
get_rcode_str_install(),
get_rcode_libraries(),
get_datasets_code(datanames, datasets, hashes),
teal.slice::get_filter_expr(datasets, datanames)
)
}
gogonzo marked this conversation as resolved.
Show resolved Hide resolved
),
datasets$get_join_keys(),
metadata
Expand Down
27 changes: 4 additions & 23 deletions R/module_teal.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,7 @@ srv_teal <- function(id, modules, raw_data, filter = teal_slices()) {

# loading the data -----
env <- environment()
datasets_reactive <- reactive({
if (is.null(raw_data())) {
return(NULL)
}
datasets_reactive <- eventReactive(raw_data(), ignoreNULL = TRUE, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for explicitly setting ignoreNULL to default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I should remove it because eventReactive has ignoreNULL = TRUE by default, which is exactly what we need here.

env$progress <- shiny::Progress$new(session)
env$progress$set(0.25, message = "Setting data")

Expand All @@ -184,25 +181,9 @@ srv_teal <- function(id, modules, raw_data, filter = teal_slices()) {
} else if (isTRUE(attr(filter, "module_specific"))) {
# we should create FilteredData even if modules$datanames is null
# null controls a display of filter panel but data should be still passed
datanames <- if (is.null(modules$datanames)) raw_data()$get_datanames() else modules$datanames
data_objects <- sapply(
datanames,
function(dataname) {
dataset <- raw_data()$get_dataset(dataname)
list(
dataset = dataset$get_raw_data(),
metadata = dataset$get_metadata(),
label = dataset$get_dataset_label()
)
},
simplify = FALSE
)
datasets_module <- teal.slice::init_filtered_data(
data_objects,
join_keys = raw_data()$get_join_keys(),
code = raw_data()$get_code_class(),
check = raw_data()$get_check()
)
datanames <- if (is.null(modules$datanames)) teal.data::get_dataname(raw_data()) else modules$datanames
# todo: subset tdata object to datanames
datasets_module <- teal.slice::init_filtered_data(raw_data())

# set initial filters
slices <- Filter(x = filter, f = function(x) {
Expand Down
34 changes: 25 additions & 9 deletions R/module_teal_with_splash.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ ui_teal_with_splash <- function(id,
title,
header = tags$p("Add Title Here"),
footer = tags$p("Add Footer Here")) {
checkmate::assert_class(data, "TealDataAbstract")
is_pulled_data <- teal.data::is_pulled(data)
checkmate::assert_multi_class(data, c("TealDataAbstract", "teal_data"))
ns <- NS(id)

# Startup splash screen for delayed loading
# We use delayed loading in all cases, even when the data does not need to be fetched.
# This has the benefit that when filtering the data takes a lot of time initially, the
# Shiny app does not time out.
splash_ui <- if (is_pulled_data) {
# blank ui if data is already pulled
splash_ui <- if (inherits(data, "teal_data")) {
div()
} else if (teal.data::is_pulled(data)) {
div()
} else {
message("App was initialized with delayed data loading.")
Expand All @@ -55,7 +55,7 @@ ui_teal_with_splash <- function(id,
#' @return `reactive`, return value of [srv_teal()]
#' @export
srv_teal_with_splash <- function(id, data, modules, filter = teal_slices()) {
checkmate::assert_class(data, "TealDataAbstract")
checkmate::assert_multi_class(data, c("TealDataAbstract", "teal_data"))
moduleServer(id, function(input, output, session) {
logger::log_trace(
"srv_teal_with_splash initializing module with data { paste(data$get_datanames(), collapse = ' ')}."
Expand All @@ -65,17 +65,33 @@ srv_teal_with_splash <- function(id, data, modules, filter = teal_slices()) {
shinyjs::showLog()
}

is_pulled_data <- teal.data::is_pulled(data)
# raw_data contains TealDataAbstract, i.e. R6 object and container for data
# reactive to get data through delayed loading
# we must leave it inside the server because of callModule which needs to pick up the right session
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this still true?

if (is_pulled_data) {
raw_data <- reactiveVal(data) # will trigger by setting it
raw_data <- if (inherits(data, "teal_data")) {
reactiveVal(data)
} else if (teal.data::is_pulled(data)) {
gogonzo marked this conversation as resolved.
Show resolved Hide resolved
new_data <- new_teal_data(
env = lapply(data$get_datasets(), function(x) x$get_raw_data()),
code = data$get_code(),
keys = data$get_join_keys()
)
reactiveVal(new_data) # will trigger by setting it
} else {
raw_data <- data$get_server()(id = "startapp_module")
raw_data_old <- data$get_server()(id = "startapp_module")
raw_data <- reactive({
data <- raw_data_old()
new_teal_data(
env = lapply(data$get_datasets(), function(x) x$get_raw_data()),
code = data$get_code(),
keys = data$get_join_keys()
)
})

if (!is.reactive(raw_data)) {
stop("The delayed loading module has to return a reactive object.")
}
raw_data
}

res <- srv_teal(id = "teal", modules = modules, raw_data = raw_data, filter = filter)
Expand Down
Loading