-
-
Notifications
You must be signed in to change notification settings - Fork 15
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 decorators for tm_missing_data
#809
Merged
averissimo
merged 11 commits into
1187_decorate_output@main
from
tm_missing_data_module@1187_decorate_output@main
Nov 26, 2024
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cca4193
this is not there yet!
m7pr ad975fd
Merge branch '1187_decorate_output@main' into tm_missing_data_module@…
m7pr 8f0687b
chore: rename ui/srv_teal_transform_data to ui/srv_transform_teal_data
averissimo e5f7864
Merge branch '1187_decorate_output@main' into tm_missing_data_module@…
averissimo 2c3c297
fix: move all decoration to 1 chunk and fix problems
averissimo 5a27da7
Merge branch '1187_decorate_output@main' into tm_missing_data_module@…
m7pr 314e06c
remove browser
m7pr 524da4c
chore: cleanup and remove warning
averissimo 1368c2c
feat: add wrapper function
averissimo 4243950
fix: error on module and add subset_decorator
averissimo 06f938e
Merge branch '1187_decorate_output@main' into tm_missing_data_module@…
averissimo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,18 @@ | |
#' | ||
#' @inherit shared_params return | ||
#' | ||
#' @section Decorating `tm_missing_data`: | ||
#' | ||
#' This module generates the following objects, which can be modified in place using decorators: | ||
#' - `summary_plot_top` (`ggplot2`) | ||
#' - `summary_plot_bottom` (`ggplot2`) | ||
#' - `combination_plot_top` (`ggplot2`) | ||
#' - `combination_plot_bottom` (`ggplot2`) | ||
#' - `table` ([DT::datatable()]) | ||
#' | ||
#' For additional details and examples of decorators, refer to the vignette | ||
#' `vignette("decorate-modules-output", package = "teal")` or the [`teal_transform_module()`] documentation. | ||
#' | ||
#' @examplesShinylive | ||
#' library(teal.modules.general) | ||
#' interactive <- function() TRUE | ||
|
@@ -87,7 +99,8 @@ tm_missing_data <- function(label = "Missing data", | |
"Combinations Main" = teal.widgets::ggplot2_args(labs = list(title = NULL)) | ||
), | ||
pre_output = NULL, | ||
post_output = NULL) { | ||
post_output = NULL, | ||
decorators = NULL) { | ||
message("Initializing tm_missing_data") | ||
|
||
# Requires Suggested packages | ||
|
@@ -121,14 +134,19 @@ tm_missing_data <- function(label = "Missing data", | |
|
||
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE) | ||
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE) | ||
checkmate::assert_list(decorators, "teal_transform_module", null.ok = TRUE) | ||
# End of assertions | ||
|
||
ans <- module( | ||
label, | ||
server = srv_page_missing_data, | ||
server_args = list( | ||
parent_dataname = parent_dataname, plot_height = plot_height, | ||
plot_width = plot_width, ggplot2_args = ggplot2_args, ggtheme = ggtheme | ||
parent_dataname = parent_dataname, | ||
plot_height = plot_height, | ||
plot_width = plot_width, | ||
ggplot2_args = ggplot2_args, | ||
ggtheme = ggtheme, | ||
decorators = decorators | ||
), | ||
ui = ui_page_missing_data, | ||
datanames = "all", | ||
|
@@ -165,7 +183,7 @@ ui_page_missing_data <- function(id, pre_output = NULL, post_output = NULL) { | |
|
||
# Server function for the missing data module (all datasets) | ||
srv_page_missing_data <- function(id, data, reporter, filter_panel_api, parent_dataname, | ||
plot_height, plot_width, ggplot2_args, ggtheme) { | ||
plot_height, plot_width, ggplot2_args, ggtheme, decorators) { | ||
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") | ||
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI") | ||
moduleServer(id, function(input, output, session) { | ||
|
@@ -215,7 +233,8 @@ srv_page_missing_data <- function(id, data, reporter, filter_panel_api, parent_d | |
id = ns(x), | ||
summary_per_patient = if_subject_plot, | ||
ggtheme = ggtheme, | ||
datanames = datanames | ||
datanames = datanames, | ||
decorators = decorators | ||
) | ||
) | ||
} | ||
|
@@ -248,7 +267,8 @@ srv_page_missing_data <- function(id, data, reporter, filter_panel_api, parent_d | |
parent_dataname = parent_dataname, | ||
plot_height = plot_height, | ||
plot_width = plot_width, | ||
ggplot2_args = ggplot2_args | ||
ggplot2_args = ggplot2_args, | ||
decorators = decorators | ||
) | ||
} | ||
) | ||
|
@@ -326,7 +346,7 @@ ui_missing_data <- function(id, by_subject_plot = FALSE) { | |
} | ||
|
||
# UI encoding for the missing data module (all datasets) | ||
encoding_missing_data <- function(id, summary_per_patient = FALSE, ggtheme, datanames) { | ||
encoding_missing_data <- function(id, summary_per_patient = FALSE, ggtheme, datanames, decorators) { | ||
ns <- NS(id) | ||
|
||
tagList( | ||
|
@@ -401,6 +421,7 @@ encoding_missing_data <- function(id, summary_per_patient = FALSE, ggtheme, data | |
) | ||
) | ||
), | ||
ui_transform_teal_data(ns("decorator"), transformators = decorators), | ||
teal.widgets::panel_item( | ||
title = "Plot settings", | ||
selectInput( | ||
|
@@ -416,7 +437,7 @@ encoding_missing_data <- function(id, summary_per_patient = FALSE, ggtheme, data | |
|
||
# Server function for the missing data (single dataset) | ||
srv_missing_data <- function(id, data, reporter, filter_panel_api, dataname, parent_dataname, | ||
plot_height, plot_width, ggplot2_args) { | ||
plot_height, plot_width, ggplot2_args, decorators) { | ||
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") | ||
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI") | ||
checkmate::assert_class(data, "reactive") | ||
|
@@ -718,7 +739,7 @@ srv_missing_data <- function(id, data, reporter, filter_panel_api, dataname, par | |
qenv <- teal.code::eval_code( | ||
qenv, | ||
substitute( | ||
p1 <- summary_plot_obs %>% | ||
summary_plot_top <- summary_plot_obs %>% | ||
ggplot() + | ||
aes( | ||
x = factor(create_cols_labels(col), levels = x_levels), | ||
|
@@ -800,7 +821,7 @@ srv_missing_data <- function(id, data, reporter, filter_panel_api, dataname, par | |
qenv <- teal.code::eval_code( | ||
qenv, | ||
substitute( | ||
p2 <- summary_plot_patients %>% | ||
summary_plot_bottom <- summary_plot_patients %>% | ||
ggplot() + | ||
aes_( | ||
x = ~ factor(create_cols_labels(col), levels = x_levels), | ||
|
@@ -833,33 +854,44 @@ srv_missing_data <- function(id, data, reporter, filter_panel_api, dataname, par | |
ggthemes = parsed_ggplot2_args$ggtheme | ||
) | ||
) | ||
) %>% | ||
teal.code::eval_code( | ||
quote({ | ||
g1 <- ggplotGrob(p1) | ||
g2 <- ggplotGrob(p2) | ||
g <- gridExtra::gtable_cbind(g1, g2, size = "first") | ||
g$heights <- grid::unit.pmax(g1$heights, g2$heights) | ||
grid::grid.newpage() | ||
}) | ||
) | ||
) | ||
} | ||
qenv | ||
}) | ||
|
||
|
||
decorated_summary_plot_q <- srv_transform_teal_data(id = "decorator", data = summary_plot_q, transformators = decorators) | ||
decorated_summary_plot_grob_q <- reactive({ | ||
q <- if (isTRUE(input$if_patients_plot)) { | ||
within( | ||
decorated_summary_plot_q(), | ||
{ | ||
g1 <- ggplotGrob(summary_plot_top) | ||
g2 <- ggplotGrob(summary_plot_bottom) | ||
g <- gridExtra::gtable_cbind(g1, g2, size = "first") | ||
g$heights <- grid::unit.pmax(g1$heights, g2$heights) | ||
grid::grid.newpage() | ||
} | ||
) | ||
} else { | ||
qenv <- teal.code::eval_code( | ||
qenv, | ||
quote({ | ||
g <- ggplotGrob(p1) | ||
within( | ||
decorated_summary_plot_q(), | ||
{ | ||
g <- ggplotGrob(summary_plot_top) | ||
grid::grid.newpage() | ||
}) | ||
} | ||
) | ||
} | ||
|
||
teal.code::eval_code( | ||
qenv, | ||
q, | ||
quote(grid::grid.draw(g)) | ||
) | ||
}) | ||
|
||
summary_plot_r <- reactive(summary_plot_q()[["g"]]) | ||
summary_plot_r <- reactive({ | ||
req(summary_plot_q()) | ||
decorated_summary_plot_grob_q()[["g"]] | ||
}) | ||
|
||
combination_cutoff_q <- reactive({ | ||
req(common_code_q()) | ||
|
@@ -976,7 +1008,7 @@ srv_missing_data <- function(id, data, reporter, filter_panel_api, dataname, par | |
qenv, | ||
substitute( | ||
expr = { | ||
p1 <- data_combination_plot_cutoff %>% | ||
combination_plot_top <- data_combination_plot_cutoff %>% | ||
dplyr::select(id, n) %>% | ||
dplyr::distinct() %>% | ||
ggplot(aes(x = id, y = n)) + | ||
|
@@ -994,7 +1026,7 @@ srv_missing_data <- function(id, data, reporter, filter_panel_api, dataname, par | |
graph_number_rows <- length(unique(data_combination_plot_cutoff$id)) | ||
graph_number_cols <- nrow(data_combination_plot_cutoff) / graph_number_rows | ||
|
||
p2 <- data_combination_plot_cutoff %>% ggplot() + | ||
combination_plot_bottom <- data_combination_plot_cutoff %>% ggplot() + | ||
aes(x = create_cols_labels(key), y = id - 0.5, fill = value) + | ||
geom_tile(alpha = 0.85, height = 0.95) + | ||
scale_fill_manual( | ||
|
@@ -1008,14 +1040,6 @@ srv_missing_data <- function(id, data, reporter, filter_panel_api, dataname, par | |
labs2 + | ||
ggthemes2 + | ||
themes2 | ||
|
||
g1 <- ggplotGrob(p1) | ||
g2 <- ggplotGrob(p2) | ||
|
||
g <- gridExtra::gtable_rbind(g1, g2, size = "last") | ||
g$heights[7] <- grid::unit(0.2, "null") # rescale to get the bar chart smaller | ||
grid::grid.newpage() | ||
grid::grid.draw(g) | ||
}, | ||
env = list( | ||
labs1 = parsed_ggplot2_args1$labs, | ||
|
@@ -1029,7 +1053,26 @@ srv_missing_data <- function(id, data, reporter, filter_panel_api, dataname, par | |
) | ||
}) | ||
|
||
combination_plot_r <- reactive(combination_plot_q()[["g"]]) | ||
decorated_combination_plot_q <- srv_transform_teal_data(id = "decorator", data = combination_plot_q, transformators = decorators) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @m7pr I converted this PR to the new |
||
decorated_combination_plot_grob_q <- reactive({ | ||
within( | ||
decorated_combination_plot_q(), | ||
{ | ||
g1 <- ggplotGrob(combination_plot_top) | ||
g2 <- ggplotGrob(combination_plot_bottom) | ||
|
||
g <- gridExtra::gtable_rbind(g1, g2, size = "last") | ||
g$heights[7] <- grid::unit(0.2, "null") # rescale to get the bar chart smaller | ||
grid::grid.newpage() | ||
grid::grid.draw(g) | ||
} | ||
) | ||
}) | ||
|
||
combination_plot_r <- reactive({ | ||
req(combination_plot_q()) | ||
decorated_combination_plot_grob_q()[["g"]] | ||
}) | ||
|
||
summary_table_q <- reactive({ | ||
req( | ||
|
@@ -1108,10 +1151,15 @@ srv_missing_data <- function(id, data, reporter, filter_panel_api, dataname, par | |
) | ||
} | ||
|
||
teal.code::eval_code(qenv, quote(summary_data)) | ||
teal.code::eval_code(qenv, quote(table <- DT::datatable(summary_data))) | ||
}) | ||
|
||
summary_table_r <- reactive(summary_table_q()[["summary_data"]]) | ||
decorated_summary_table_q <- | ||
srv_transform_teal_data(id = "decorator", data = summary_table_q, transformators = decorators) | ||
summary_table_r <- reactive({ | ||
req(summary_table_q()) | ||
decorated_summary_table_q()[["table"]] | ||
}) | ||
|
||
by_subject_plot_q <- reactive({ | ||
# needed to trigger show r code update on tab change | ||
|
@@ -1188,7 +1236,7 @@ srv_missing_data <- function(id, data, reporter, filter_panel_api, dataname, par | |
teal.code::eval_code( | ||
substitute( | ||
expr = { | ||
g <- ggplot(summary_plot_patients, aes( | ||
plot <- ggplot(summary_plot_patients, aes( | ||
x = factor(id, levels = order_subjects), | ||
y = factor(col, levels = ordered_columns[["column"]]), | ||
fill = isna | ||
|
@@ -1209,7 +1257,6 @@ srv_missing_data <- function(id, data, reporter, filter_panel_api, dataname, par | |
labs + | ||
ggthemes + | ||
themes | ||
print(g) | ||
}, | ||
env = list( | ||
labs = parsed_ggplot2_args$labs, | ||
|
@@ -1220,7 +1267,13 @@ srv_missing_data <- function(id, data, reporter, filter_panel_api, dataname, par | |
) | ||
}) | ||
|
||
by_subject_plot_r <- reactive(by_subject_plot_q()[["g"]]) | ||
decorated_by_subject_plot_q <- srv_transform_teal_data(id = "decorator", data = by_subject_plot_q, transformators = decorators) | ||
decorated_by_subject_plot_print_q <- reactive(within(decorated_by_subject_plot_q(), print(plot))) | ||
|
||
by_subject_plot_r <- reactive({ | ||
req(by_subject_plot_q()) # Ensure original errors are displayed | ||
decorated_by_subject_plot_print_q()[["plot"]] | ||
}) | ||
|
||
output$levels_table <- DT::renderDataTable( | ||
expr = { | ||
|
@@ -1272,7 +1325,7 @@ srv_missing_data <- function(id, data, reporter, filter_panel_api, dataname, par | |
|
||
teal.widgets::verbatim_popup_srv( | ||
id = "rcode", | ||
verbatim_content = reactive(teal.code::get_code(final_q())), | ||
verbatim_content = reactive(teal.code::get_code(req(final_q()))), | ||
title = "Show R Code for Missing Data" | ||
) | ||
|
||
|
@@ -1308,7 +1361,7 @@ srv_missing_data <- function(id, data, reporter, filter_panel_api, dataname, par | |
card$append_text("Comment", "header3") | ||
card$append_text(comment) | ||
} | ||
card$append_src(teal.code::get_code(final_q())) | ||
card$append_src(teal.code::get_code(req(final_q()))) | ||
card | ||
} | ||
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's a problem with this reactive.
Running with your example shows some errors