Is it possible to return 'selected' row from nested table?
Hi, I'm trying to return value from selected row. All works fine as far as I'm working with table without expandable rows. I'm nesting table in each row of main table, when I expand row then table with data shows up. Then I select on a row in this nested table and nothing happens, no value is return from this row. I added selection="multiple" and onClick="select" to nested table but it doesn't work. Following your example (from GitHub), I added those two to the code:
data <- unique(CO2[, c("Plant", "Type")])
reactable(data, details = function(index) {
plant_data <- CO2[CO2$Plant == data$Plant[index], ]
htmltools::div(style = "padding: 16px",
reactable(plant_data,
selection="multiple",
onClick="select",
outlined = TRUE)
)
})
So my question is - can we get values from nested table when given row is clicked? Or it's not possible?
Hi, can you clarify what you mean by returning the value from a selected row? Do you mean getting the selected row indices in a Shiny app? If so, the nested table will have to be in a Shiny output via reactableOutput().
You can render a nested table like reactableOutput("nested") in the row details, then access its selected rows using getReactableOutput("nested", "selected").
Doing this for every row in the table gets tricky because you have to create Shiny outputs for each nested table, but it could be done using something a loop or apply like:
library(shiny)
library(reactable)
data <- unique(CO2[, c("Plant", "Type")])
ui <- fluidPage(
reactable(data, details = function(index) {
htmltools::div(style = "padding: 16px",
reactableOutput(paste0("tbl-", index))
)
})
)
server <- function(input, output) {
lapply(seq_len(nrow(data)), function(index) {
output[[paste0("tbl-", index)]] <- renderReactable({
plant_data <- CO2[CO2$Plant == data$Plant[index], ]
reactable(plant_data,
selection="multiple",
onClick="select",
outlined = TRUE)
})
})
observe({
# Print selected rows for the nested table in row 1
print(getReactableState("tbl-1", "selected"))
})
}
shinyApp(ui, server)
Also, note that collapsing the table will remove the table from the page, so the selected rows reset when you expand the row again.
Ok, thank you, this is waht I wanted to know:)