I have a shiny app which stores a names of data frames. This names work as a SQL queries – when user clicks on the name, it should run the query and display result. I thought that I could use for that {reactable}
to store the names and details
parameter to call a function which runs a query ({DBI}
). But what I want is to compute the query only when user clicks the name / row (I don’t even want to store the result – if user closes the details of row, results should be garbage collected).
I know that {shiny}
is based on lazy loading paradigm so I thought it will be easy. Here is an example code:
library(shiny)
ui <- fluidPage(
reactable::reactableOutput("tbl")
)
server <- function(input, output, session) {
output$tbl <- reactable::renderReactable({
data_df <- data.frame(row = c("iris", "mtcars"))
reactable::reactable(data_df,
details = function(val) {
reactable::reactable(get(data_df$row[[val]],
envir = rlang::pkg_env("datasets")))
})
})
}
shinyApp(ui, server)
However, my attempts show that everything is computed when the table is created – so taking above example – code reactable::reactable(get(data_df$row[[val]], envir = rlang::pkg_env("datasets")))
from both rows were computed on app start, not only when users click on the name to display details of row (I know that, because I have used some heavy loading SQL query). Is there a way to (preferably easy) turn this behavior to be lazy loading? My real app has dynamic data – number of rows / names / queries changes depending on what user is doing.