4

我正在尝试menuItem动态生成多个 s,可能很简单,但我没有得到正确的想法。

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

paste函数的 menuItems 无法解析(我在侧边栏上得到粘贴函数的结果)。我试过了evaleval(parse(paste(...)))两个都没有用——我错过了什么?

4

2 回答 2

5

我不太清楚您要的是什么,但这里有一个带有动态菜单的示例。

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)
于 2015-12-01T16:02:08.077 回答
1

我更改了代码如下,它工作:

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))
 })
)

因此,关键是将 的全部内容sidebarMenu放在文本字段中并对其进行评估

于 2015-09-25T15:51:05.083 回答