Search code examples
rshinytabpanel

navigating to specific tab from external link shiny


I'm trying to give access to a specific view of my shiny dashboard to an external application. Basically, I want to give them a url link with a filter parameter so that when they click on the link, my shiny dashboard opens up on the specific view with the filters applied. I came across some other posts regarding the same on SO Externally link to specific tabPanel in Shiny App

I tried using the code to figure out the solution, but haven't been able to. This is what I currently have, what I'd like to have is something like http://127.0.0.1:7687/?url=%22Plot%202%22&species=%22setosa%22

This should open up the Plot 2 tab of the dashboard and apply the relevant filters. Any help on this would be great. Thanks!

library(shiny)
library(DT)


# Define UI for application that draws a histogram
ui <- navbarPage(title = "Navigate", id = 'nav',

   # Application title
   tabPanel("Plot",
            plotOutput("distPlot")
            ),
   tabPanel("Plot 2",
            selectInput("species", "Select Species", choices = c("setosa", "virginica"),
                        multiple = T, selected = NULL),
            dataTableOutput("tbl1")
            )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {

  observe({
    query <- parseQueryString(session$clientData$url_search)
    if(!is.null(query$url)) {
      url <- strsplit(query$url,"/")[[1]]
      updateTabsetPanel(session, 'nav', url)
    }
  })

   output$distPlot <- renderPlot({
      hist(rnorm(100), col = 'darkgray', border = 'white')
   })

   output$tbl1 <- renderDataTable({
     tmp <- iris
     if(!is.null(input$species))
        tmp <- iris[which(iris$Species %in% input$species), ]

     datatable(tmp)
   })
}

# Run the application
shinyApp(ui = ui, server = server)

Solution

  • Your observe should be the following:

      observe({
        query <- parseQueryString(session$clientData$url_search)
        if(!is.null(query$url)) {
          url <- strsplit(query$url,"\"")[[1]][2]
          species <- strsplit(query$species, "\"")[[1]][2]
          updateTabsetPanel(session, 'nav', url)
          updateSelectInput(session, 'species',selected = species)
        }
      })