URL parsing the desired the state of the application

Hi all,

I need your help in capturing the url.

In the below example, I am updating the url based in tabs selected. Suppose when the user clicks on row2 of the DT table, tab2 is opened(url is also updated accordingly) . But when we copy the url (say : http://127.0.0.1:XXXX/?tabs=tabs2), I do not get tha specfic state of the app, instead I get the default page.
Can you help me here. I need to have specific state of app when we paste the url

library(shiny)
library(DT)

ui <- function(request) {
  shinyUI(navbarPage(
    "Title", id = "inTabset", selected = "Summary",
    tabsetPanel(id = "tabs",
                # tabPanel(
                #   "Readme",tags$head(tags$link(rel = "stylesheet", type="text/css", href="style.css"))
                # ),
                tabPanel(
                  "Summary",
                  dataTableOutput("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 <- renderDataTable({
    datatable(iris,selection = 'single')
  })
  
  observeEvent(input$tab_rows_selected, {
    insertTab(inputId = "tabs",
              tabPanel(paste0("tabs",input$tab_rows_selected), "This a dynamically-added tab"),
              target = "Summary",select = TRUE
              
    )
  })
  
  
}


enableBookmarking("url")
shinyApp(ui, server)

I was just working on a bookmarking problem. insertUI and the like require special handling, based on this github issue answered by Winston.

library(shiny)
library(DT)

ui <- function(request) {
  shinyUI(navbarPage(
    "Title", id = "inTabset", selected = "Summary",
    tabsetPanel(id = "tabs",
                # tabPanel(
                #   "Readme",tags$head(tags$link(rel = "stylesheet", type="text/css", href="style.css"))
                # ),
                tabPanel(
                  "Summary",
                  dataTableOutput("tab")))
  )
  )
}

server <- function(input, output, session) {
  
# Create container to house tabs selected in a session
  values <- reactiveValues(n = character())
  
  insertTabCustom = function(id) {
    insertTab(inputId = "tabs",
              tabPanel(paste0("tabs",id), "This a dynamically-added tab"),
              target = "Summary",select = TRUE
              
    )
  }
  
  # 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()
  })
  
  onBookmark(function(state) {
# store the selected tabs in the arbitrary values container in the bookmark link
    state$values$n <- values$n
  })
  
  # Set callback that stores the app state in the URL
  onBookmarked(function(url) {
    #updateQueryString(paste0("?tabs=", input$tabs), mode = "replace")
    updateQueryString(url)
  })

  onRestored(function(state) {
    for (i in state$values$n) insertTabCustom(i)
    updateTabsetPanel(session, inputId = "tabs", selected = paste0("tabs",state$values$n[-1]))
  })
  
  output$tab <- renderDataTable({
    datatable(iris,selection = 'single')
  })
  
  observeEvent(input$tab_rows_selected, {
    values$n <- c(values$n, input$tab_rows_selected)
    insertTabCustom(input$tab_rows_selected)
  })
  
  
}


enableBookmarking("url")
shinyApp(ui, server)

Thanks.
But I think we when we make the tabs reactive, the url does not work. Example, click on 2 row on the DT table, it goes to the last tab with respective row (say 2) getting printed.

Now, when we copy that url and paste it in other tab, it does not show 2 there?

library(shiny)
library(DT)

ui <- function(req) {
  navbarPage(
    title = "TITLE",
    position = "fixed-top",
    inverse = TRUE,
    id = "inTabset",
    collapsible = TRUE,
    # tabsetPanel(id = "tabs",
    # tabPanel(
    #   "Read me",tags$head(tags$link(rel = "stylesheet", type="text/css", href="style.css"))
    # ),
    tabPanel("Summary",tags$head(tags$link(rel = "stylesheet", type="text/css", href="style.css")),DTOutput("tab")%>% withSpinner(color="#1560BD")
             # ,tags$head(tags$script(src="myscript.js"))
    ),
    tabPanel("tab3",br(),br(),br(),br(),
             br(),
             htmlOutput("iris"))
  )
}
server <- function(input, output, session) {
  observe({
    # Trigger this observer every time an input changes
    reactiveValuesToList(input)
    session$doBookmark()
  })

  
  output$tab <- renderDataTable({
    datatable(iris,selection = 'single')
  })
  
  observeEvent(input$tab_rows_selected,{
    updateTabsetPanel(session,inputId = "inTabset",selected = "tab3")
    output$iris <- renderUI({
      as.character(input$tab_rows_selected)
    })
  })
  
  onBookmarked(function(url) {
    updateQueryString(url)
  })
}

shinyApp(ui, server, enableBookmarking = "url")

Did you even try the solution?

Yes I tried.

Basically,
What I am looking for is,

  1. There are 2 tabs (Tab1, Tab2)
  2. There is a DT table in Tab1
  3. Once the user clicks on a any row(say 2 row)
  4. It should redirect to Tab2 with 2 displayed as shown in code (Say this is State1)
  5. Now when we copy the url and paste in another tab, the State1 should be opened

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.