【问题标题】:How to catch dynamic tabPanel() id in Shiny app如何在闪亮的应用程序中捕获动态 tabPanel() id
【发布时间】:2017-06-29 14:35:44
【问题描述】:

可以在链接中使用 tabPanel id 吗?我说像 #tab-6584-1#tab-6985-1 这样的 id,当您的光标位于浏览器中运行的 Shiny 应用程序中的选项卡上时显示:

Image of dynamic tabPanel() id example with Firefox

我想用它在一个闪亮的应用程序中放置一个左上角的链接,以重定向到应用程序的“主页”页面。

【问题讨论】:

    标签: shiny


    【解决方案1】:

    根据您的问题,我不确定,但您似乎想在应用中模仿导航到“子页面”。如果是这种情况,在 Shiny 中有一种方法可以做到这一点,即在应用程序 URL (link to gist) 的末尾读取和写入哈希字符串的值:

    library(shiny)
    
    ui <- fluidPage(
      sidebarLayout(
        sidebarPanel(
          tags$a("Go to Panel 1", href = "#panel1"), br(),
          tags$a("Go to Panel 2", href = "#panel2"), br(),
          tags$a("Go to Panel 3", href = "#panel3")
        ),
        mainPanel(
          tabsetPanel(id = "tabs",
            tabPanel("Panel 1", h1("Panel 1"), value = "#panel1"),
            tabPanel("Panel 2", h1("Panel 2"), value = "#panel2"),
            tabPanel("Panel 3", h1("Panel 3"), value = "#panel3")
          )
        )
      )
    )
    server <- function(input, output, session) {
      # When we change from one `tabPanel` to another, update the URL hash
      observeEvent(input$tabs, {
    
        # No work to be done if input$tabs and the hash are already the same
        if (getUrlHash() == input$tabs) return()
    
        # The 'push' argument is necessary so that the hash change event occurs and
        # so that the other observer is triggered.
        updateQueryString(
          paste0(getQueryString(), input$tabs),
          "push"
        )
        # Don't run the first time so as to not generate a circular dependency 
        # between the two observers
      }, ignoreInit = TRUE)
    
      # When the hash changes (due to clicking on the link in the sidebar or switching
      # between the `tabPanel`s), switch tabs and update an input. Note that clicking 
      # another `tabPanel` already switches tabs.
      observeEvent(getUrlHash(), {
        hash <- getUrlHash()
    
        # No work to be done if input$tabs and the hash are already the same
        if (hash == input$tabs) return()
    
        valid <- c("#panel1", "#panel2", "#panel3")
    
        if (hash %in% valid) {
          updateTabsetPanel(session, "tabs", hash)
        }
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2020-01-21
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 2018-02-03
      • 2017-07-27
      • 2015-01-15
      相关资源
      最近更新 更多