Reputation: 985
The below application is updating the url based on row selected in the DT table and the selected tab. But what happens is, when the user copy paste the url in another tab, it is not opening the specific tab, instead it is opening the default page (url : http://127.0.0.1:XXXX/?tab1)
library(shiny)
library(readxl)
library(dplyr)
library(xtable)
library(shinyWidgets) ## for picker input
library(shinydashboard)
library(DT)
library(tidyverse)
library(shinycssloaders)
library(plotly)
library(htmlwidgets)
library(sparkline)
library(data.table)
require(reshape2)
library(glue)
ui <- shinyUI(navbarPage(
"title",id = "inTabset",selected = "Summary",
tabsetPanel(id = "tabs",
# tabPanel(
# "Read me",tags$head(tags$link(rel = "stylesheet", type="text/css", href="style.css"))
# ),
tabPanel(
"Summary",
DT::dataTableOutput("tab")))
)
)
server <- function(input, output, session) {
output$tab <- DT::renderDataTable({
datatable(iris,selection = 'single')
})
observeEvent(input$tab_rows_selected, {
insertTab(inputId = "tabs",
tabPanel(paste0("tab",input$tab_rows_selected),
selectInput(paste0("tab",input$tab_rows_selected),input$tab_rows_selected,choices = c(1,input$tab_rows_selected))
),select = TRUE
)
})
observe({
query <- getQueryString()
print(names(query))
updateQueryString(paste0("?",input$tabs), mode = "push")
})
}
shinyApp(ui, server)
Upvotes: 2
Views: 67
Reputation: 5254
One can save the state of a Shiny app in the URL with the bookmark feature of shiny. I set up a reprex that shows it.
Notes:
url
argument in the onBookmarked
and write your own code. It should be something that can be parsed by getQueryString
.enableBookmarking("url")
.library(shiny)
ui <- function(request) {
shinyUI(navbarPage(
"PaperCut", id = "inTabset", selected = "Summary",
tabsetPanel(id = "tabs",
tabPanel(
"Readme",tags$head(tags$link(rel = "stylesheet", type="text/css", href="style.css"))
),
tabPanel(
"Summary",
tableOutput("tab")))
)
)
}
server <- function(input, output, session) {
# Make sure you only bookmark the tabsetPanel
setBookmarkExclude(isolate(names(input)[names(input) != "tabs"]))
# Every time the tab changes, store the app state as URL bookmark
observeEvent(input$tabs, {
session$doBookmark()
})
# Set callback that stores the app state in the URL
onBookmarked(function(url) {
updateQueryString(paste0("?tabs=", input$tabs), mode = "replace")
})
# Set callback to restore the app state from the URL
onRestore(function(state) {
updateTabsetPanel(inputId = "tabs", selected = getQueryString()[["tabs"]])
})
output$tab <- renderTable({
iris
})
}
enableBookmarking("url")
shinyApp(ui, server)
Upvotes: 3