我正在使用 argonDash 包构建一个闪亮的仪表板,我需要以反应方式从 argonDashSidebar 捕获所选项目的 tabName,以便我可以在应用程序的其他地方使用它(例如,显示它或触发某些事件) )。在shinydashboard中,我们可以通过input$sidebar_menu轻松检索所选侧边栏项目的tabName,但这似乎不适用于argonDash。
我尝试使用shinyjs来监听点击事件,但我仍然无法让tabName像在shinydashboard中那样做出反应。 input$sidebar_menu 似乎没有使用所选 argonSidebarItem 的 tabName 进行更新。
我需要什么: 我想从 argonDashSidebar 中反应性地捕获所选项目的 tabName (类似于闪亮仪表板中的 input$sidebar_menu )。 将捕获的 tabName 显示在 UI 中的某处作为示例。 可重现的示例:
这是我的 Shiny 应用程序的简化版本,用于演示我想要实现的目标:
library(shiny)
library(argonR)
library(argonDash)
library(shinyjs)
# UI
argonSidebar <- argonDashSidebar(
vertical = TRUE,
skin = "light",
background = "white",
size = "md",
side = "left",
id = "my_sidebar",
icon = tags$img(src = "logo.png", alt = "custom icon"),
br(),
argonSidebarMenu(
id = "sidebar_menu",
argonSidebarItem(
tabName = "cards",
icon = argonIcon(name = "tv-2", color = "info"),
"Overview",
id = "item_cards"
),
argonSidebarItem(
tabName = "tables",
icon = argonIcon(name = "bullet-list-67", color = "red"),
"Charts",
id = "item_tables"
),
argonSidebarItem(
tabName = "bulletin",
icon = argonIcon(name = "book-bookmark", color = "green"),
"Bulletin",
id = "item_bulletin"
)
)
)
server <- function(input, output, session) {
# I'd like to reactively capture the tabName of the selected sidebar item.
# Example: I want to display the currently selected tabName
output$selected_tab_display <- renderText({
# Placeholder: This should display the current tabName
paste("Selected tab is:", input$sidebar_menu)
})
}
# Main UI
ui <- argonDashPage(
useShinyjs(),
sidebar = argonSidebar,
body = argonDashBody(
h2(textOutput("selected_tab_display")) # Display the selected tabName here
)
)
shinyApp(ui, server)
在这个例子中,我希望 input$sidebar_menu 反映当前选定的侧边栏项目(tabName),但它似乎不像在shinydashboard中那样工作。如何捕获 argonDashSidebar 中所选项目的 tabName?
目前原生不支持此功能,但 RinteRface/argonDash#7 中给出了解决方案:您可以使用一个小型
JS
处理程序来定义活动选项卡的名称并将其设置为输入:
library(shiny)
library(argonR)
library(argonDash)
# UI
argonSidebar <- argonDashSidebar(
vertical = TRUE,
skin = "light",
background = "white",
size = "md",
side = "left",
id = "my_sidebar",
icon = tags$img(src = "logo.png", alt = "custom icon"),
br(),
argonSidebarMenu(
id = "sidebar_menu",
argonSidebarItem(
tabName = "cards",
icon = argonIcon(name = "tv-2", color = "info"),
"Overview",
id = "item_cards"
),
argonSidebarItem(
tabName = "tables",
icon = argonIcon(name = "bullet-list-67", color = "red"),
"Charts",
id = "item_tables"
),
argonSidebarItem(
tabName = "bulletin",
icon = argonIcon(name = "book-bookmark", color = "green"),
"Bulletin",
id = "item_bulletin"
)
)
)
server <- function(input, output, session) {
# I'd like to reactively capture the tabName of the selected sidebar item.
# Example: I want to display the currently selected tabName
output$selected_tab_display <- renderText({
# Placeholder: This should display the current tabName
paste("Selected tab is:", input$activeTab)
})
}
# Main UI
ui <- argonDashPage(
sidebar = argonSidebar,
body = argonDashBody(
tags$script("$(document).on('click', function(event) {
Shiny.onInputChange('activeTab',
$('.active').data().value);
});"),
h2(textOutput("selected_tab_display")) # Display the selected tabName here
)
)
shinyApp(ui, server)