4

I have a nested reactable table in a shiny app. The table shows summaries for groups, then I want to expand the groups to show individuals.

The data related to the individuals will change with user input, so I want to:

  1. update the data without collapsing nested tables that were expanded before clicking the button (I want the state of the table to stay the same when data change). I have tried re-rendering the table completely, and tried using updateReactable(), but still the table resets completely with each change. The documentation for using the function for data shows the following:

Arguments ...

data
Table data. A data frame or matrix.

data should have the same columns as the original table data. When updating data, the selected rows, expanded rows, and current page will reset unless explicitly specified. All other state will persist, including sorting, filtering, and grouping state.

So I believe it's possible, but I don't know how to "explicitly specify" not to collapse rows.

and

  1. I want to select rows from the nested reactable tables, but I am having trouble calling them since they are inside the nested columns.

reprex:


library(shiny)
library(dplyr)
library(reactable)

ui <- fluidPage(
  actionButton("grow_setosa", "grow setosa petals"),
  reactableOutput("table"),
  verbatimTextOutput("selected")
   )


server <- function(input, output){
  
  # make datasets -------------------
  data <- reactiveValues()
  
  data$df <- iris %>%
    group_by(Species)%>%
    slice(1:20) %>%
    ungroup()
  
  observe(data$df_summarized <- 
    data$df %>%
    group_by(Species) %>%
    summarize(across(where(is.numeric), mean)))
  
  
  # render reactable table of species summaries
  output$table <- renderReactable(
    reactable(data$df_summarized,
              
              # add nested tables of individuals per species
              details = function(index){
                targets <- 
                  filter(data$df, Species == sort(unique(data$df$Species))[index]) 
                tbl <- reactable(targets, 
                                 selection = "single",
                                 outlined = TRUE, 
                                 highlight = TRUE, 
                                 fullWidth = FALSE)
                htmltools::div(tbl)
              }
    ))
  
  # update table when data changes
     observeEvent(input$grow_setosa, {
       # add one to data
       data$df$Petal.Length[data$df$Species=="setosa"] <-    
         data$df$Petal.Length[data$df$Species=="setosa"] +1
       
       # Change current page
       updateReactable("table", data = data$df,
                       )

     })
     
     # show selected on bottom - this doesn't work
     selected <- reactive(getReactableState("table", "selected"))
     output$selected <- renderText(selected())
}

shinyApp(ui,server)

Jake L
  • 987
  • 9
  • 21

0 Answers0