I'm implementing a drill down in my app so that when user clicks on a data table new tab is opened showing details for the selected cell. I wanted to add auto bookmarking (url updates itself) to my app so I can send a link to a specific, newly generated tab but the tab is not restored correctly. I think the observer is not triggered during the restoration process. Any ideas on how to make it work?
Minimal example:
library(shiny)
library(DT)
library(shinydashboard)
ui <- function(request) {
dashboardPage(
dashboardHeader(title = "Drilldown not restoring"),
dashboardSidebar(disable = T),
dashboardBody(
tabsetPanel(id = "tabs",
tabPanel("Cars overview",
h1("Cars overview"),
div("Click any cell"),
br(),
DT::dataTableOutput("mtcars")
)
)
)
)
}
server <- function(input, output, session) {
# Generate data table
output$mtcars <- DT::renderDataTable({mtcars})
# Observe when row is selected and open new tab
observeEvent(input$mtcars_rows_selected, {
print("Trigger")
appendTab(inputId = "tabs",
tabPanel(
"Car details",
"Car detail info"
)
)
# Focus on newly created tab
updateTabsetPanel(session, "tabs", selected = "Car details")
})
# Enable bookmarking
observe({
reactiveValuesToList(input)
session$doBookmark()
})
onBookmarked(function(url) {
updateQueryString(url)
})
}
shinyApp(ui, server, enableBookmarking = "url")