我想你問的是:「我怎樣才能訪問用戶點擊的最新項目(在下拉菜單中)?」
如果這是問題,那麼我不得不告訴你,不幸的是,shinydashboard不支持(現在至少)。 Shinydashboard的Github回購中有an issue currently open,這將使這成爲可能,但我不能告訴你是否/何時實現該目標...我建議使用不同的條件機制(理想情況下可通過Shiny和可訪問的內容通過input$
)。這是可能的sidebarMenu()
,這是由menuItem()
s組成(只要你提供一個id到sidebarMenu()
和tabName
所有兒童menuItem()
s - 閱讀更多關於該here)。你說你沒有使用dashboardBody()
和tabItems()
「這樣我就可以在創建leafletOutput()時避開邊距。」我認爲在這些情況下處理CSS可能會容易很多,但要堅持shinydashboard提供的功能,而不是相反。
如果你真的想這樣,存在使用href
說法messageItem()
(也存在於taskItem()
和notificationItem()
)一種解決方法。這有點笨重,因爲你必須跟蹤所有的hrefs,並且每次點擊一個項目時你都要更新URL(在下面的例子中,散列)。如果你這樣做,現在你有一個你可以聽的事件。但是,您不能使用條件面板,因爲必須在服務器上調用getUrlHash()
(它需要訪問session
對象),而不是在UI上。
同樣,這是一種解決方法。如果可以的話,您應該嘗試使用shinydashboard完全支持的機制(在未來版本的軟件包中您的應用程序崩潰或更改的可能性較小)。
使用變通方法href
參數messageItem
:
library(shiny)
library(shinydashboard)
# these must be unique and exist for every item
itemIds <- c(team = "#team", team2 = "#team2", user = "#user")
ui <- dashboardPage(
dashboardHeader(
dropdownMenu(type = "messages", badgeStatus = "success",
messageItem("Support Team", "This is the content of a message.",
time = "5 mins", href = itemIds[["team"]]
),
messageItem("Support Team", "This is the content of another message.",
time = "2 hours", href = itemIds[["team2"]]
),
messageItem("New User", "Can I get some help?",
time = "Today", href = itemIds[["user"]]
)
)
),
dashboardSidebar(),
dashboardBody(
sliderInput("sld", "This is here so you know that other inputs are not affected by this workaround", 0, 100, 50),
uiOutput("ui")
)
)
server <- function(input, output, session) {
output$ui <- renderUI({
hash <- getUrlHash()
if (hash %in% itemIds) {
paste("This is shown when", hash, "is clicked")
}
})
}
shinyApp(ui, server)
謝謝你的回答,我這個昨天發現,似乎更容易實現,https://stackoverflow.com/a/34449844/8042782 –