whence_opened() lets the app react when the viewer opens the panel of an
output: for example, to filter other outputs to the row of a table that
the viewer is looking at. Call it in a server function or a module server.
It gives a reactive expression.
Usage
whence_opened(outputId, session = shiny::getDefaultReactiveDomain())Details
The reactive gives NULL while no panel of that output is open. While one
is open, it gives a list:
row: the number of the row of the table, forwhenceTableOutput()andwhencePlotOutput().NULLforwhenceOutput(), and for the panel of a whole table.data: that row of the table, as a data frame with one row. ForwhenceOutput(), the value.NULLfor the panel of a whole table.
Only one panel is open at a time.
Examples
if (interactive() && requireNamespace("dplyr", quietly = TRUE)) {
library(shiny)
library(dplyr, warn.conflicts = FALSE)
ui <- fluidPage(
whenceTableOutput("by_cyl", label = "Cars by cylinders"),
textOutput("looking_at")
)
server <- function(input, output, session) {
output$by_cyl <- renderWhenceTable(
mtcars |> group_by(cyl) |> summarise(cars = n())
)
opened <- whence_opened("by_cyl")
output$looking_at <- renderText({
req(opened())
paste("The viewer is looking at", opened()$data$cyl, "cylinders.")
})
}
shinyApp(ui, server)
}
