library(random.cdisc.data)
library(teal)
library(teal.reporter)
library(teal.transform)
library(teal.widgets)
library(tern)
library(dplyr)
<- list(
modules module(
label = "Adhoc module",
server = function(id, data, reporter, filter_panel_api){
moduleServer(id, function(input, output, session){
<- function(x) {
s_summary if (is.numeric(x)) {
in_rows(
"n" = rcell(sum(!is.na(x)), format = "xx"),
"Mean (sd)" = rcell(c(mean(x, na.rm = TRUE), sd(x, na.rm = TRUE)), format = "xx.xx (xx.xx)"),
"IQR" = rcell(IQR(x, na.rm = TRUE), format = "xx.xx"),
"min - max" = rcell(range(x, na.rm = TRUE), format = "xx.xx - xx.xx")
)else if (is.factor(x)) {
} <- as.list(table(x))
vs do.call(in_rows, lapply(vs, rcell, format = "xx"))
else {
} stop("type not supported")
}
}
observe({
<- get_var(data(), "ADSL")
ADSL req(ADSL)
updateSelectInput(
inputId = "param",
choices = variable_choices(ADSL),
selected = c("AGE"),
)
})
<- reactive({
table_q data() |>
within(
{<- my_summary
s_summary <- basic_table() %>%
summary_lyt split_cols_by(var = "ARM") %>%
analyze(param, afun = s_summary)
<- build_table(summary_lyt, ADSL)
summary_tbl
summary_tbl
},my_summary = s_summary,
param = input$param
)
})
$table = renderUI({
outputrenderPrint(table_q()[["summary_tbl"]])
})
# ----------
# reproducibility
# ----------
observeEvent(input$src, {
showModal(
ui = modalDialog(
title = "Reproducible R code",
$pre(
tagsget_code(table_q())
)
),session = session
)
})
# ----------
# reporter
# ----------
simple_reporter_srv(
"simple_reporter",
reporter = reporter,
card_fun = function(card = TealReportCard$new(), comment) {
$set_name("Patient demographics")
card$append_text(toString(table_q()[["summary_tbl"]]), "verbatim")
card$append_fs(filter_panel_api$get_filter_state())
card$append_encodings(list(param = input$param))
cardif (!comment == "") {
$append_text("Comment", "header3")
card$append_text(comment)
card
}$append_src(get_code(table_q()))
card
card
}
)
})
},ui = function(id) {
<- NS(id)
ns
standard_layout(
output = div(
fluidRow(column(
width = 12,
br(), hr(),
uiOutput(ns("table"))
))
),encoding = div(
simple_reporter_ui(ns("simple_reporter")),
br(),
$label('Encodings', class = 'text-primary'),
tagshelpText('Analysis Data:', tags$code('ADSL')),
selectInput(
inputId = ns('param'),
label = 'Demographic Parameter',
choices = NULL,
selected = NULL,
multiple = TRUE
),hr(),
actionButton(
inputId = ns("src"),
label = "Show R code",
width = "100%"
)
)
)
},datanames = c("ADSL")
)
)
<- teal_data()
data <- within(data, {
data <- radsl(cached = TRUE)
ADSL
})datanames(data) <- c("ADSL")
<- init(
app data = data,
modules = modules
)
if (Sys.getenv("QUARTO_ROOT") == "") {
shinyApp(app$ui, app$server)
}
Exercise 4
Further enhance your custom module by reproducibility and reporter feature!
- read
"qenv"
article on how to interact with internalqenv
object - in particular:teal.code::get_code()
function - read “Getting started with teal.reporter” vignette to get to know how to implement reporter
- add
reporter
andfilter_panel_api
to the list of arguments of module server function - use
teal.reporter::simple_reporter_srv()
module and create a report a custom card function usingteal::TealReportCard
class methods
- add