The Report previewer is an advanced shiny module designed for
visualization, editing, and downloading of report cards. It extended the
base modules introduced in the simpleReporter
vignette,
enhancing interactivity and user engagement with report content.
The five essential steps for implementing the report previewer
include integrating it within a shiny application. Key code segments are
highlighted in ### REPORTER
code blocks.
tabsetPanel
within main app with the
previewer.card
and comment
. This function must return a
ReportCard
object. The ReportCard
object
should be built step by step, assuming that it is empty at the
beginning.
comment
argument is provided, it should be added
to the card. If not, it should be added automatically at the end of the
card.card
argument is provided, the
ReportCard
instance should be automatically created for the
user. If not, the function should create the card itself. Please
note that the document page’s design is up to the developer’s
imagination.Reporter
instance and the
function to create the ReportCard
instance.The code added to introduce the reporter is wrapped in the
### REPORTER
code blocks.
First, load the required packages:
library(shiny)
library(teal.reporter)
library(ggplot2)
library(rtables)
library(DT)
library(bslib)
A basic shiny
app with the previewer module:
<- fluidPage(
ui # please, specify specific bootstrap version and theme
theme = bs_theme(version = "4"),
titlePanel(""),
tabsetPanel(
tabPanel(
"main App",
$br(),
tagssidebarLayout(
sidebarPanel(
uiOutput("encoding")
),mainPanel(
tabsetPanel(
id = "tabs",
tabPanel("Plot", plotOutput("dist_plot")),
tabPanel("Table", verbatimTextOutput("table")),
tabPanel("Table DataFrame", verbatimTextOutput("table2")),
tabPanel("Table DataTable", dataTableOutput("table3"))
)
)
)
),### REPORTER
tabPanel(
"Previewer",
reporter_previewer_ui("prev")
)###
)
)<- function(input, output, session) {
server $encoding <- renderUI({
outputtagList(
### REPORTER
simple_reporter_ui("simple_reporter"),
###
if (input$tabs == "Plot") {
sliderInput(
"binwidth",
"binwidth",
min = 2,
max = 10,
value = 8
)else if (input$tabs %in% c("Table", "Table DataFrame", "Table DataTable")) {
} selectInput(
"stat",
label = "Statistic",
choices = c("mean", "median", "sd"),
"mean"
)else {
} NULL
}
)
})<- reactive({
plot req(input$binwidth)
<- mtcars$mpg
x ggplot(data = mtcars, aes(x = mpg)) +
geom_histogram(binwidth = input$binwidth)
})$dist_plot <- renderPlot(plot())
output
<- reactive({
table req(input$stat)
<- basic_table() %>%
lyt split_rows_by("Month", label_pos = "visible") %>%
analyze("Ozone", afun = eval(str2expression(input$stat)))
build_table(lyt, airquality)
})$table <- renderPrint(table())
output
<- reactive({
table2 req(input$stat)
<- aggregate(
data c("Ozone"), drop = FALSE], list(Month = airquality$Month), get(input$stat),
airquality[, na.rm = TRUE
)colnames(data) <- c("Month", input$stat)
data
})$table2 <- renderPrint(print.data.frame(table2()))
output$table3 <- renderDataTable(table2())
output
### REPORTER
<- Reporter$new()
reporter <- function(card = ReportCard$new(), comment) {
card_fun if (input$tabs == "Plot") {
$set_name("Plot Module")
card$append_text("My plot", "header2")
card$append_plot(plot())
card$append_rcode(
cardpaste(
c(
"x <- mtcars$mpg",
"ggplot2::ggplot(data = mtcars, ggplot2::aes(x = mpg)) +",
paste0("ggplot2::geom_histogram(binwidth = ", input$binwidth, ")")
),collapse = "\n"
),echo = TRUE,
eval = FALSE
)else if (input$tabs == "Table") {
} $set_name("Table Module rtables")
card$append_text("My rtables", "header2")
card$append_table(table())
card$append_rcode(
cardpaste(
c(
"lyt <- rtables::basic_table() %>%",
'rtables::split_rows_by("Month", label_pos = "visible") %>%',
paste0('rtables::analyze("Ozone", afun = ', input$stat, ")"),
"rtables::build_table(lyt, airquality)"
),collapse = "\n"
),echo = TRUE,
eval = FALSE
)else if (input$tabs %in% c("Table DataFrame", "Table DataTable")) {
} $set_name("Table Module DF")
card$append_text("My Table DF", "header2")
card$append_table(table2())
card# Here r code added as a regular verbatim text
$append_text(
cardpaste0(
c(
'data <- aggregate(airquality[, c("Ozone"), drop = FALSE], list(Month = airquality$Month), ',
$stat,
input", na.rm = TRUE)\n",
'colnames(data) <- c("Month", ', paste0('"', input$stat, '"'), ")\n",
"data"
),collapse = ""
"verbatim"
),
)
}if (!comment == "") {
$append_text("Comment", "header3")
card$append_text(comment)
card
}
card
}simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)
reporter_previewer_srv("prev", reporter)
###
}
if (interactive()) shinyApp(ui = ui, server = server)