Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce decorators for
tm_missing_data
(#809)
Part of insightsengineering/teal#1370 <details><summary>Updated working example</summary> ```r # tm_missing_data pkgload::load_all("../teal") pkgload::load_all(".") plot_grob_decorator <- function(default_footnote = "I am a good decorator", variable_to_replace = "summary_plot") { teal_transform_module( label = "Plot", ui = function(id) shiny::textInput(shiny::NS(id, "footnote"), "Footnote", value = default_footnote), server = function(id, data) { moduleServer(id, function(input, output, session) { logger::log_info("🟠 plot_grob with default: {default_footnote}!", namespace = "teal.modules.general") reactive({ req(data(), input$footnote) logger::log_info("changing the footnote {default_footnote}", namespace = "teal.modules.general") teal.code::eval_code(data(), substitute( { footnote_grob <- grid::textGrob(footnote, x = 0, hjust = 0, gp = grid::gpar(fontsize = 10, fontface = "italic", col = "gray50")) # Arrange the plot and footnote variable_to_replace <- gridExtra::arrangeGrob( variable_to_replace, footnote_grob, ncol = 1, heights = grid::unit.c(grid::unit(1, "npc") - grid::unit(1, "lines"), grid::unit(1, "lines")) ) }, env = list( footnote = input$footnote, variable_to_replace = as.name(variable_to_replace) ))) }) }) } ) } caption_decorator <- teal_transform_module( ui = function(id) shiny::textInput(shiny::NS(id, "footnote"), "Footnote", value = "D"), server = make_teal_transform_server( expression(by_subject_plot <- by_subject_plot + ggplot2::labs(caption = footnote)) ) ) table_decorator_interactive <- teal_transform_module( label = "Table", ui = function(id) { selectInput( NS(id, "style"), "Table Style", choices = c("Default", "Striped", "Hover"), selected = "Default" ) }, server = function(id, data) { moduleServer(id, function(input, output, session) { logger::log_info("🔵 Footnote called to action!", namespace = "teal.modules.general") reactive({ req(data(), input$style) within(data(), { style_str <- style table <- switch( style, "Striped" = DT::formatStyle( table, columns = attr(table$x, "colnames")[-1], target = 'row', backgroundColor = '#f9f9f9' ), "Hover" = DT::formatStyle( table, columns = attr(table$x, "colnames")[-1], target = 'row', backgroundColor = '#f0f0f0' ), table ) }, style = input$style) }) }) } ) generic_decorator <- teal_transform_module( ui = function(id) shiny::textInput(shiny::NS(id, "footnote"), "Footnote", value = "D"), server = make_teal_transform_server( expression({ if (exists("by_subject_plot")) by_subject_plot <- by_subject_plot + ggplot2::labs(caption = footnote) if (exists("table", inherits = FALSE)) table <- DT::formatStyle(table, columns = attr(table$x, "colnames")[-1], target = 'row', backgroundColor = '#f9f9f9') if (exists("summary_plot")) { footnote_grob <- grid::textGrob(footnote, x = 0, hjust = 0, gp = grid::gpar(fontsize = 10, fontface = "italic", col = "gray50")) # Arrange the plot and footnote summary_plot <- gridExtra::arrangeGrob(summary_plot, footnote_grob, ncol = 1, heights = grid::unit.c(grid::unit(1, "npc") - grid::unit(1, "lines"), grid::unit(1, "lines"))) } if (exists("combination_plot")) { footnote_grob <- grid::textGrob(footnote, x = 0, hjust = 0, gp = grid::gpar(fontsize = 10, fontface = "italic", col = "gray50")) # Arrange the plot and footnote combination_plot <- gridExtra::arrangeGrob(combination_plot, footnote_grob, ncol = 1, heights = grid::unit.c(grid::unit(1, "npc") - grid::unit(1, "lines"), grid::unit(1, "lines"))) } }) ) ) # CDISC example data data <- teal_data() data <- within(data, { require(nestcolor) ADSL <- rADSL ADRS <- rADRS }) join_keys(data) <- default_cdisc_join_keys[names(data)] app <- init( data = data, modules = modules( tm_missing_data( label = "Flat list", decorators = list( summary_plot = plot_grob_decorator("A"), combination_plot = plot_grob_decorator("B", "combination_plot"), summary_table = table_decorator_interactive, by_subject_plot = caption_decorator ) ), tm_missing_data( label = "Complex list", decorators = list( summary_plot = list(plot_grob_decorator("A")), combination_plot = list(plot_grob_decorator("B", "combination_plot")), summary_table = list(table_decorator_interactive), by_subject_plot = list(caption_decorator) ) ), tm_missing_data( label = "Complex list", decorators = list(generic_decorator) ), example_module() ) ) if (interactive()) { shinyApp(app$ui, app$server) } ``` </details> <details><summary> Old ~Working~ Example </summary> ```r pkgload::load_all("../teal") pkgload::load_all(".") footnote_dec <- teal_transform_module( label = "Footnote", ui = function(id) shiny::textInput(shiny::NS(id, "footnote"), "Footnote for Combination Plot", value = "I am a good decorator"), server = function(id, data) { moduleServer(id, function(input, output, session) { logger::log_info("🟢 Footnote called to action!", namespace = "teal.modules.general") reactive( within( data(), { footnote_str <- footnote if (exists('combination_plot_top')) { combination_plot_top <- combination_plot_top + ggplot2::labs(caption = footnote_str) } }, footnote = input$footnote ) ) }) } ) # general example data data <- teal_data() data <- within(data, { require(nestcolor) add_nas <- function(x) { x[sample(seq_along(x), floor(length(x) * runif(1, .05, .17)))] <- NA x } iris <- iris mtcars <- mtcars iris[] <- lapply(iris, add_nas) mtcars[] <- lapply(mtcars, add_nas) mtcars[["cyl"]] <- as.factor(mtcars[["cyl"]]) mtcars[["gear"]] <- as.factor(mtcars[["gear"]]) }) app <- init( data = data, modules = modules( tm_missing_data(decorators = list(footnote_dec)) ) ) if (interactive()) { shinyApp(app$ui, app$server) } ``` </details> --------- Co-authored-by: André Veríssimo <[email protected]>
- Loading branch information