单击另一个菜单项时,使用 renderMenu 的 RShiny 中的动态侧边栏菜单消失

Dynamic Sidebar Menu in RShiny using renderMenu disappears when another menu item is clicked

我正在尝试在 R Shiny Dashboard 中创建动态侧边栏面板。我已经成功添加了根据是否满足预定义条件添加侧边栏菜单项的功能。但是,一旦我单击应用程序中已存在的任何子菜单项,动态内容就会消失。以下是该问题的一个工作示例:

library(shinycssloaders)
library(shinyalert)
library(shinydashboard)
library(shinyWidgets)


ui <- dashboardPage(dashboardHeader(title = "Title"),
                     dashboardSidebar(
                       sidebarMenu(
                         id = "tabss"
                         ,menuItem("General Information", tabName = "General_Information", icon = icon("android"))
                         ,sidebarMenuOutput("dynamic_content")
                         ,menuItem("Settings", tabName = "settings", icon = icon("cogs")
                                   ,menuSubItem("Setting1", tabName = "Setting1", icon = icon("cogs"))
                                   ,menuSubItem("Setting2", tabName = "Setting2", icon = icon("cogs")))
                         )),
                       dashboardBody(
                         tabItems(
                           tabItem("General_Information",
                                   actionButton("add_menuItem",label = "Add Menu Item"))
                         )
                       )
                       )

server <- function (input, output, session){

  observeEvent(input$add_menuItem,{
    output$dynamic_content <- renderMenu(menuItem("Dynamic Menu","dynamicMenu"))
  })
}

shinyApp(ui, server, options = c(launch.browser = T))

在上面的示例中,使用通用信息页面上的操作按钮在通用信息和设置之间的侧栏面板上添加了一个菜单项。但是,打开设置子菜单会导致动态添加的菜单项从侧边栏中消失。

我正在寻找一种解决方法,以便动态创建的菜单项不会消失。我欢迎并感谢对此的任何帮助。如果有更多信息有助于解决问题,请告诉我。谢谢!

我不确定这是否是您要查找的内容,但您可以添加 reactiveVal 以包含额外的动态菜单项。然后,将您的静态菜单项从 ui 移动到 server 方法,以便您可以将动态项目包含在一起。希望这可能会有所帮助。

library(shinycssloaders)
library(shinyalert)
library(shinydashboard)
library(shinyWidgets)

ui <- dashboardPage(dashboardHeader(title = "Title"),
                    dashboardSidebar(
                      sidebarMenu(
                        id = "tabss"
                        ,sidebarMenuOutput("dynamic_content")
                      )),
                    dashboardBody(
                      tabItems(
                        tabItem("General_Information",
                                actionButton("add_menuItem",label = "Add Menu Item"))
                      )
                    )
)

server <- function (input, output, session){

  menu_val <- reactiveVal(NULL)

  observeEvent(input$add_menuItem,{
    menu_val(menuItem("Dynamic Menu", "dynamicMenu"))
  })

  output$dynamic_content <- renderMenu({
    menu_list <- list(
      menuItem("General Information", tabName = "General_Information", icon = icon("android")),
      menu_val(),
      menuItem("Settings", tabName = "settings", icon = icon("cogs")
                ,menuSubItem("Setting1", tabName = "Setting1", icon = icon("cogs"))
                ,menuSubItem("Setting2", tabName = "Setting2", icon = icon("cogs")))
    )
    sidebarMenu(.list = menu_list) 
  })
}

shinyApp(ui, server)