我认为您要问的是:“如何访问用户单击的最新项目(在下拉菜单中)?”
如果这是问题所在,那么我必须告诉您,不幸的是,shinydashboard 不支持此功能(至少目前是这样)。 Shinydashboard 的 Github 存储库中有 an issue currently open 可以实现这一点,但我不能告诉你我们是否/何时实现它......我建议使用不同的条件机制(理想情况下由 Shiny 跟踪并可通过input$ 访问)。这对于由menuItem()s 组成的sidebarMenu() 是可能的(只要您向sidebarMenu() 和tabName 提供一个ID 给所有没有孩子的menuItem()s ——阅读更多关于@ 987654322@)。你说你没有使用dashboardBody() 和tabItems() “这样我可以在创建leafletOutput() 时避免边距。”我认为在这些情况下处理 CSS 可能会容易得多,但坚持使用 shinydashboard 提供的开箱即用功能,而不是相反。
如果你真的想要这个,可以使用messageItem() 的href 参数(也出现在taskItem() 和notificationItem() 中)的解决方法。这有点笨拙,因为您必须跟踪所有href,并且每次单击项目时都会更新URL(在下面的示例中,哈希)。如果你这样做,现在你就有了一个可以收听的事件。但是,您不能使用条件面板,因为必须在服务器上调用getUrlHash()(它需要访问session 对象),而不是在用户界面上。
同样,这是一种解决方法。如果可以的话,您应该尝试使用 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)