manu p
manu p

Reputation: 985

Copy pasting url is not returning desired tab

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

Answers (1)

Jan
Jan

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:

  • The URL does not exactly look you wanted it. I do not know a way to control it. Update: I found a way that is pretty easy. You ignore the url argument in the onBookmarked and write your own code. It should be something that can be parsed by getQueryString.
  • The UI must be encapsulated in a function.
  • The bookmarking feature must be enabled explicitly using 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

Related Questions