R Shinydashboard dynamic menuItem

时间秒杀一切 提交于 2019-12-04 17:22:20

I couldn't quite make out what you're asking for, but here's an example of something with a dynamic menu.

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Dynamic Menu"),
  dashboardSidebar(
    sidebarMenuOutput(outputId = "dy_menu")
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "main",
              textInput(inputId = "new_menu_name", 
                        label = "New Menu Name"),
              actionButton(inputId = "add",
                           label = "Add Menu")
      )
    )
  )
)

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

  output$dy_menu <- renderMenu({
    menu_list <- list(
      menuItem("Add Menu Items", tabName = "main", selected = TRUE),
      menu_vals$menu_list)
    sidebarMenu(.list = menu_list)
  })

  menu_vals = reactiveValues(menu_list = NULL)
  observeEvent(eventExpr = input$add,
               handlerExpr = {
                 menu_vals$menu_list[[length(menu_vals$menu_list) + 1]] <- menuItem(input$new_menu_name,
                                                                                    tabName = input$new_menu_name) 
               })

}

shinyApp(ui, server)

I changed the code as follows and it worked :

library(shiny)
library(shinydashboard)
port_tables<-c("tab1","tab2","tab3","tab4") # These are from a DB connection in the original code
text1<-paste("menuItem(\"",port_tables,"\",tabName=\"",port_tables,"\",icon=icon('th'))")
text2<-paste("sidebarMenu(id = 'tabs',textInput('port', 'Enter port:'),h4('Tables',style='color:yellow;margin-left:20px;'),",paste(text1,collapse=","),paste(")"))
function(input, output) {
output$smenu1 <- renderMenu({
eval(parse(text=text2))
 })
)

So, the key is put the whole content of sidebarMenu in a text field and evaluate it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!