Simple reporter is a shiny
module for capturing app
views during the session, and eventually downloading a report document.
The Simple reporter module consists of two separate modules one for each
of the two buttons, Add Card and Download Report buttons modules.
module designed to capture and visualize application views throughout a session, facilitating the generation and download of a comprehensive report document. This module consists of two separate modules one for each of the two buttons.
- Add card button.
- Download report button.
The implementation should consist of 4 steps:
- Add modules UI component to the app’s UI.
- Initialize
Reporter
instance. - Create a reactive that must return a
teal_card
object. Theteal_card
object should be built step by step, assuming that it is empty at the beginning. Please note that the document page’s design is up to the developer’s imagination. - Invoke the servers with the
Reporter
instance and the function to create theReportCard
instance.
The code added to introduce the reporter is wrapped in the
### REPORTER
code blocks.
First, load the required packages:
Simple reporter shiny
app with separate modules for each
button:
ui <- bslib::page_fluid(
titlePanel(""),
sidebarLayout(
sidebarPanel(
uiOutput("encoding")
),
mainPanel(
### REPORTER
tags$div(
add_card_button_ui("addReportCard"),
download_report_button_ui("downloadButton"),
reset_report_button_ui("resetButton")
),
###
tags$br(),
tabsetPanel(
id = "tabs",
tabPanel("Plot", plotOutput("dist_plot")),
tabPanel("Table", verbatimTextOutput("table"))
)
)
)
)
server <- function(input, output, session) {
output$encoding <- renderUI({
if (input$tabs == "Plot") {
sliderInput(
"binwidth",
"binwidth",
min = 2,
max = 10,
value = 8
)
} else {
selectInput(
"stat",
label = "Statistic",
choices = c("mean", "median", "sd"),
"mean"
)
}
})
plot <- reactive({
req(input$binwidth)
x <- mtcars$mpg
ggplot(data = mtcars, aes(x = mpg)) +
geom_histogram(binwidth = input$binwidth)
})
output$dist_plot <- renderPlot({
plot()
})
table <- reactive({
req(input$stat)
lyt <- basic_table() %>%
split_rows_by("Month", label_pos = "visible") %>%
analyze("Ozone", afun = eval(str2expression(input$stat)))
build_table(lyt, airquality)
})
output$table <- renderPrint({
table()
})
### REPORTER
reporter <- Reporter$new()
card_r <- reactive({
card <- teal_card()
if (input$tabs == "Plot") {
card <- c(card, "## My plot", plot())
} else if (input$tabs == "Table") {
card <- c(card, "## My Table", table())
}
card
})
add_card_button_srv("addReportCard", reporter = reporter, card_fun = card_r)
download_report_button_srv("downloadButton", reporter = reporter)
reset_report_button_srv("resetButton", reporter)
###
}
shinyApp(ui = ui, server = server)
Simple reporter shiny
app with combined buttons
modules:
ui <- bslib::page_fluid(
titlePanel(""),
sidebarLayout(
sidebarPanel(
uiOutput("encoding")
),
mainPanel(
### REPORTER
simple_reporter_ui("simpleReporter"),
###
tabsetPanel(
id = "tabs",
tabPanel("Plot", plotOutput("dist_plot")),
tabPanel("Table", verbatimTextOutput("table"))
)
)
)
)
server <- function(input, output, session) {
output$encoding <- renderUI({
if (input$tabs == "Plot") {
sliderInput(
"binwidth",
"binwidth",
min = 2,
max = 10,
value = 8
)
} else {
selectInput(
"stat",
label = "Statistic",
choices = c("mean", "median", "sd"),
"mean"
)
}
})
plot <- reactive({
req(input$binwidth)
x <- mtcars$mpg
ggplot(data = mtcars, aes(x = mpg)) +
geom_histogram(binwidth = input$binwidth)
})
output$dist_plot <- renderPlot({
plot()
})
table <- reactive({
req(input$stat)
lyt <- basic_table() %>%
split_rows_by("Month", label_pos = "visible") %>%
analyze("Ozone", afun = eval(str2expression(input$stat)))
build_table(lyt, airquality)
})
output$table <- renderPrint({
table()
})
### REPORTER
reporter <- Reporter$new()
# Optionally set reporter id to e.g. secure report reload only for the same app
# The id is added to the downloaded file name.
reporter$set_id("myappid")
reporter <- Reporter$new()
card_r <- reactive({
card <- teal_card()
if (input$tabs == "Plot") {
card <- c(card, "## My plot", plot())
} else if (input$tabs == "Table") {
card <- c(card, "## My Table", table())
}
card
})
simple_reporter_srv("simpleReporter", reporter = reporter, card_fun = card_r)
###
}
shinyApp(ui = ui, server = server)