外部链接到 Shiny App 中的特定 tabPanel

Mar*_*ley 6 r shiny

可以相对轻松地在平面 Shiny 应用程序中使用锚链接 - /sf/answers/2002386221/

然而,有可能是一个外部链接针对特定tabPanelnavbarPage一个闪亮的应用程序?

考虑以下测试应用程序: UI:

shinyUI(navbarPage(
  "Anchor Test",
  tabPanel(
    "Panel 1", fluidPage(HTML(
      paste(
        "<p>I'm the first panel</p>","<p><a href='#irisPlotUI'>Link to irisPlotUI in panel 2</a></p>",
        "<a href='#panel2'>Link to panel 2</a>",sep = ""
      )
    ))
  ),
  tabPanel("Panel 2", fluidPage(
    "I'm the second panel", uiOutput("irisPlotUI")
  ),
  value = "#panel2"),
  tabPanel("Panel 3", "I'm a table!!!")
Run Code Online (Sandbox Code Playgroud)

服务器:

shinyServer(function(input, output) {
  output$irisPlotUI <- renderUI(plotOutput("irisPlot"))
  output$irisPlot <- renderPlot(plot(iris$Sepal.Length))
  })
Run Code Online (Sandbox Code Playgroud)

使用链接答案中的方法不起作用, idirisPlotUI是正确的,但tabPanel它是情节所在的 id 的子代。

data-valuetabPanel给定的值"panel2",通过使用的说法value,然而,闪亮的应用程序将继续给予tabPanel一个唯一的ID,我不知道该如何定位。

我检查了已部署的 Shiny 应用程序的来源,例如https://martinjhnhadley.shinyapps.io/AnchorLinks,并找到了指向以下内容的实际链接tabPanelhttps : //internal.shinyapps.io/martinjhnhadley/AnchorLinks/?initialWidth= 1074&childId=shinyapp#tab-8850-2

但是,直接导航到此链接也不会将我定向到选项卡。

锚链接是我定位应用程序部分的唯一选择还是有闪亮的特定解决方案?

如果没有,我如何插入一个脚本,例如此处显示的/sf/answers/1094646101/以允许在登陆 Shiny 应用程序时执行 javascript 以tabPanel类似于此https:/ /groups.google.com/d/msg/shiny-discuss/sJlasQf71fY/RW7Xc8F02IoJ

感谢 daattali 的解决方案

使用 daattali 的回答,我也能从他那里找到以下内容 - https://github.com/rstudio/shiny/issues/772#issuecomment-112919149

以下内容完全满足我的需求,请注意,我选择保持/?url=他使用的约定:

用户界面

shinyUI(
  navbarPage( "test", id = 'someID',
              tabPanel("tab1", h1("page1")),

              navbarMenu( "menu",
                          tabPanel('tab2a',  value='nested1', h1("page2a")),
                          tabPanel('tab2b',  value='nested2', h1("page2b")),
                          tabPanel('tab_sub',  "foobar")
              ),
              tabPanel("tab3", h1("page3"))
  ))
Run Code Online (Sandbox Code Playgroud)

服务器

url1 <- url2 <- ""

shinyServer(function(input, output, session) {

  values <- reactiveValues(myurl = c(), parent_tab = "")

  observe({
    # make sure this is called on pageload (to look at the query string)
    # and whenever any tab is successfully changed.
    # If you want to stop running this code after the initial load was
    # successful so that further manual tab changes don't run this,
    # maybe just have some boolean flag for that.

    input$someID
    input$tab_sub_tabs
    query <- parseQueryString(session$clientData$url_search)
    url <- query$url
    if (is.null(url)) {
      url <- ""
    }

    # "depth" is how many levels the url in the query string is
    depth <- function(x) length(unlist(strsplit(x,"/")))

    # if we reached the end, done!
    if (length(values$myurl) == depth(url)) {
      return()
    }
    # base case - need to tell it what the first main nav name is
    else if (length(values$myurl) == 0) {
      values$parent_tab <- "someID"
    }
    # if we're waiting for a tab switch but the UI hasn't updated yet
    else if (is.null(input[[values$parent_tab]])) {
      return()
    }
    # same - waiting for a tab switch
    else if (tail(values$myurl, 1) != input[[values$parent_tab]]) {
      return()
    }
    # the UI is on the tab that we last switched to, and there are more
    # tabs to switch inside the current tab
    # make sure the tabs follow the naming scheme
    else {
      values$parent_tab <- paste0(tail(values$myurl, 1), "_tabs")
    }

    # figure out the id/value of the next tab
    new_tab <- unlist(strsplit(url, "/"))[length(values$myurl)+1]

    # easy peasy.
    updateTabsetPanel(session, values$parent_tab, new_tab)
    values$myurl <- c(values$myurl, new_tab)
  })
})
Run Code Online (Sandbox Code Playgroud)

JBl*_*laz 6

我发现另一个答案有点难以理解,所以我创建了下面的例子。要导航到“数据可用性”选项卡,请将“?a=b”放在 url 的末尾。例如,如果在端口 7436 上托管应用程序,以下链接会将您直接带到数据可用性页面。http://127.0.0.1:7436/?a=b

library(shinydashboard)
library(shiny)
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(id = "container",
      menuItem("Channels", tabName = "lt_channels"),
      menuItem("Data Availability", tabName = "data_avail")
    )),
  dashboardBody(
    tabItems(
      tabItem(tabName = "lt_channels",
              h1("Test Fail")),
      tabItem(tabName = "data_avail",
              h1("Test Pass"))
    )
  )
)



server <- function(session, input, output){
  observe({
    query <- parseQueryString(session$clientData$url_search)
    query1 <- paste(names(query), query, sep = "=", collapse=", ")
    print(query1)
    if(query1 == "a=b"){
      updateTabItems(session, inputId = "container", selected = "data_avail")
    }
  })
}
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)


Dea*_*ali 4

您可以向 URL 添加搜索查询参数(例如href='www.myapp.com?tab=tab2),并且在正在链接的应用程序中,您必须添加一些逻辑,如果搜索字符串存在,则在初始化时更改为指定选项卡(例如,查找atsession$clientData$url_search如果它存在并且有一个tab变量,则使用updateTabsetPanel()转到该选项卡)