teal v1.1.0 migration guide for modules #1643
Unanswered
averissimo
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
tealv1.1.0introduces several new features that may require minor updates to existing modules.These changes simplify module development, improve reproducibility, and provide a unified reporting interface.
Key changes
🆕 A new
teal_reporterobject is passed to the module server functionsteal_data, keeping all existing functionality and adds ateal_cardslot to store a report (teal_cardobject).teal_datatoteal_reporterhappens internally inteal, so app and module developers do not need to worry about it.🆕
teal_cardobject is now used to represent report cards in the reportercharacter,ggplot,data.frame, etc.The previous
ReportCardR6 class is now deprecated and will be removed in a future release.See the section "Adding arbitrary markdown content to the reporter" for more details on how to use the new
teal_cardclass.🆕
tealalways displays the Reporter buttons in the main UIteal::init(reporter = NULL)argument.reporterargument in the module's server function.teal::disable_report().🆕 Code reproducibility is now handled automatically by
tealShow R codebutton is always displayed, but becomes active only when the module returns aqenv-based object (such asteal_dataorteal_reporter).options(teal.show_src = FALSE).teal::disable_src()around a single module or group.Backward compatibility
Existing modules will continue to function as before.
The new
Add to ReportandShow R codebuttons will appear in the UI but remain disabled until the module is updated.Migration steps for module developers
Follow these steps to update your custom modules and take advantage of the new reporter and code reproducibility features (only required for modules that need reporter or reproducibility integration).
To take advantage of the new features module developers may need to make the following adjustments:
dataargument at the end of the server function and make sure it's reactiveAdd to ReportandShow R codebuttons from module's UIreporterandfilter_panel_apiarguments from the module's server functionteal.reporter::teal_card()functionHere is an example of how the
tm_a_regressionmodule was updated inteal.modules.generalpackage:Note: The code examples below are simplified for illustration purposes and may not show the complete module implementation. The actual updated modules include additional internal utility functions for decorators and image dimension preservation that are omitted here for clarity.
1. Return the modified
dataargument at the end of the server functionFor new modules this is the only required step to enable the new features.
Ensure that the returned
dataargument is reactive.@@ srv_a_regression <- function(id, paste(utils::capture.output(summary(fitted()))[-1], collapse = "\n") }) + decorated_output_q }) }2. Remove "Add to Report" and "Show R code" buttons from module's UI
@@ ui_a_regression <- function(id, ...) { tags$div(verbatimTextOutput(ns("text"))) )), encoding = tags$div( - ### Reporter - teal.reporter::add_card_button_ui(ns("add_reporter"), label = "Add Report Card"), - tags$br(), tags$br(), - ### tags$label("Encodings", class = "text-primary"), tags$br(), teal.transform::datanames_input(args[c("response", "regressor")]), teal.transform::data_extract_ui( @@ ui_a_regression <- function(id, ...) { ) ) ), - forms = tagList( - teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code") - ), pre_output = args$pre_output, post_output = args$post_output )The next section shows what to remove on the server side.
3. Remove the
reporterandfilter_panel_apiarguments in the serverRemove the arguments and their usage:
@@ ui_a_regression <- function(id, ...) { # Server function for the regression module srv_a_regression <- function(id, data, - reporter, - filter_panel_api, response, regressor, plot_height, @@ srv_a_regression <- function(id, ggplot2_args, default_outlier_label, decorators) { - with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") - with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI") checkmate::assert_class(data, "reactive") checkmate::assert_class(isolate(data()), "teal_data") moduleServer(id, function(input, output, session) {The server logic that handles the reporter and R code buttons should also be removed.
@@ srv_a_regression <- function(id, paste(utils::capture.output(summary(fitted()))[-1], collapse = "\n") }) - # Render R code. - source_code_r <- reactive(teal.code::get_code(req(decorated_output_q()))) - - teal.widgets::verbatim_popup_srv( - id = "rcode", - verbatim_content = source_code_r, - title = "R code for the regression plot", - ) - - ### REPORTER - if (with_reporter) { - card_fun <- function(comment, label) { - card <- teal::report_card_template( - title = "Linear Regression Plot", - label = label, - with_filter = with_filter, - filter_panel_api = filter_panel_api - ) - card$append_text("Plot", "header3") - card$append_plot(plot_r(), dim = pws$dim()) - if (!comment == "") { - card$append_text("Comment", "header3") - card$append_text(comment) - } - card$append_src(source_code_r()) - card - } - teal.reporter::add_card_button_srv("add_reporter", reporter = reporter, card_fun = card_fun) - } - ### })Note that in the released
tm_a_regressionwe are saving the plot size with a private utility functionset_chunk_dims().This takes the plot dimension size from the
plot_with_settingswidget (teal.widgets::plot_with_settings_srv()) and stores it in the metadata of the lastteal_cardelement.4. Add required title and extra content to the reporter during code evaluation
Note that we are adding a header named
Module's output(s)to the report card using theteal.reporter::teal_card()function.@@ srv_a_regression <- function(id, ) }) - qenv <- reactive( - teal.code::eval_code(data(), "library(ggplot2);library(dplyr)") - ) + qenv <- reactive({ + obj <- data() + teal.reporter::teal_card(obj) <- + c( + teal.reporter::teal_card(obj), + teal.reporter::teal_card("## Module's output(s)") + ) + teal.code::eval_code(obj, "library(ggplot2);library(dplyr)") + }) anl_merged_q <- reactive({ req(anl_merged_input())teal.reporter::teal_card(obj)is reused as it already contains data filtering code and the code passed toteal_dataobject, before it was used inteal::init(data = teal_data)5. Ensure that the code evaluation generates outputs
The code evaluation that generates the model summary is added to the reporter automatically as it generates an output.
Alternatively, you could also use
print(),plot()or other functions to generate outputs that will be captured by the reporter.@@ srv_a_regression <- function(id, ) } })) %>% teal.code::eval_code(quote(summary(fit))) })We also add a header named
Plotto the report card using theteal.repoter::teal_card()function before the plot output is generated.@@ srv_a_regression <- function(id, "Cook's distance" = output_plot_4(), "Residuals vs Leverage" = output_plot_5(), "Cook's dist vs Leverage" = output_plot_6() ) }) - output_final_q <- reactive(within(output_q(), plot)) + output_final_q <- reactive({ + qenv <- output_q() + teal.reporter::teal_card(qenv) <- c(teal.reporter::teal_card(qenv), "### Plot") + within(qenv, plot) + })Summary
The new features simplify how modules generate and manage reportable outputs.
By updating existing modules as described above, developers gain automatic integration with reproducibility and reporting features.
To understand how to use the new
teal_reporterandteal_cardclasses in more detail, please refer to the following documentation:teal_reporterclass vignetteFor detailed API documentation, see:
teal_reporterreferenceteal_cardreferenceBeta Was this translation helpful? Give feedback.
All reactions