可以相對容易地在平面Shiny應用中使用錨鏈接 - https://stackoverflow.com/a/28605517/1659890。外部鏈接到Shiny App中的特定tabPanel
但是,外部鏈接是否可以針對Shiny應用中navbarPage
的特定tabPanel
?
請看下面的測試程序: 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!!!")
服務器:
shinyServer(function(input, output) {
output$irisPlotUI <- renderUI(plotOutput("irisPlot"))
output$irisPlot <- renderPlot(plot(iris$Sepal.Length))
})
從鏈接答案使用方法不起作用的irisPlotUI
的ID是正確的,但一個是tabPanel
小孩居住的情節。
data-value
的tabPanel
通過使用參數value
給出"panel2"
的值,但是Shiny應用繼續給tabPanel
一個我不知道如何定位的唯一ID。
我已經研究部署閃亮應用的來源,例如https://martinjhnhadley.shinyapps.io/AnchorLinks,發現實際的鏈接tabPanel
:https://internal.shinyapps.io/martinjhnhadley/AnchorLinks/?initialWidth=1074&childId=shinyapp#tab-8850-2
然而,直接導航到該鏈接不給我去標籤要麼。
錨鏈接是我的唯一選項,在應用程序的部分目標中,還是有一個閃亮的特定解決方案?
如果沒有,我怎麼可能會插入一個腳本,如下所示https://stackoverflow.com/a/15637801/1659890允許降落在閃亮的應用程序時,以類似的方式來選擇tabPanel
這個https://groups.google.com/d/msg/shiny-discuss/sJlasQf71fY/RW7Xc8F02IoJ
解決方案感謝daattali
要執行的JavaScript使用daattali的答案,我能找到下面還從他身上 - https://github.com/rstudio/shiny/issues/772#issuecomment-112919149
下達到正是我需要的,請注意,我選擇保持與他用/?url=
約定:
UI.R
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"))
))
SERVER.R
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)
})
})
我忘記了我寫的代碼:)我認爲這段代碼對於您的需求過於複雜,除非您有許多選項卡,並且每個選項卡可以具有嵌套選項卡,並且您不提前知道選項卡的名稱。我寫的解決方案專門針對這樣的一般情況。在你的情況下,如果你只有一個標籤級別(vs嵌套標籤),那麼大多數代碼是不必要的 –
@daattali ahh,這對於這個最小工作示例來說肯定是正確的,但真正有光澤的應用程序有很多級別。雖然好暴力。我個人認爲這是別人會從中受益的東西,很難找到,博客值得嗎? –
我正計劃在不久的將來會有許多關於高級閃亮技術的博客,其中包括許多我經常看到的常見問題以及代碼片段,以及解釋它們的解釋。我已經在這裏有這個:) –