Hello,
I'd like to build a reactive kableExtra
table within a Shiny app. I'm working with two datasets: raw dataset and an "Instructions" Google spreadsheet that contains table information.
Because this raw dataset contains responses from an ongoing survey, it will be continuously filled with new rows. Using pack_rows()
I created grouped rows (this is the main reason I'm using kableExtra
) to label a few rows into one group. As I get more survey responses, I'd like to add pack_rows()
so that I can create a reactive table inside a Shiny app.
Here is a minimal example:
library(shiny)
library(kableExtra)
# Define UI for dataset viewer app ----
ui <- fluidPage(
# App title ----
titlePanel("Reactivity"),
# Output: HTML table with requested number of observations ----
tableOutput("mtcars_table")
)
# Define server logic to summarize and view selected dataset ----
server <- function(input, output, session) {
# Link to instructions file updating every 1 second
updated_instructions <- reactiveFileReader(
intervalMillis = 1000,
session = session,
filePath = "https://docs.google.com/spreadsheets/d/1J1JEUPveBzkR_AHmBCtNcE5mqNc4jwUdxI9Yr0ecoZo/edit?usp=sharing",
readFunc = googlesheets4::read_sheet
)
## Answer History Table-----------------
output$mtcars_table <- function() {
kbl(mtcars[1:10, 1:6], caption = "Group Rows") %>%
kable_paper("striped", full_width = F) %>%
pack_rows(updated_instructions() %>%
pull(header_text) %>%
nth(1),
4, 7,
label_row_css = paste0("background-color: ",
updated_instructions() %>%
pull(header_background_color) %>%
nth(1),
"; color: ",
updated_instructions() %>%
pull(header_text_color) %>%
nth(1))) %>%
pack_rows(updated_instructions() %>%
pull(header_text) %>%
nth(2),
8, 10,
label_row_css = paste0("background-color: ",
updated_instructions() %>%
pull(header_background_color) %>%
nth(2),
"; color: ",
updated_instructions() %>%
pull(header_text_color) %>%
nth(2)))
}
}
shinyApp(ui, server)
I'm not sure what function I'd use to reactively generate more pack_rows
inside a Shiny app as I get more survey responses and more instructions in the Google spreadsheet.
For example, if the dataset expanded to the first 15 rows of mtcars
and I wanted to add Group 3
and the accompanying colors using pack_rows()
, how'd I make that work?
...