R Shinydashboard子菜单使用相同的UI

时间:2018-02-27 10:33:50

标签: r shiny shinydashboard

我有一个GetDemoObject(myForm: NgForm): employee { this.Demoemployee = new employee; this.Demoemployee.Email = myForm.value.Email; this.Demoemployee.RoleId= myForm.value.RoleId; return this.Demoemployee; } 的基本应用,点击后会显示特定变量的图表。可以从用户MenuItem中的SelectInput中选择变量。

但是,我真正想做的是让可能的变量显示为原始MenuItem的{​​{1}},这样当点击一个变量时,它会抓取当前选定的变量并呈现情节。与使用subMenuItems下拉列表相比,这看起来更符合我。

我试图通过将每个MenuItem链接到显示相应内容的相同SelectInput来实现此功能,但Shiny似乎不喜欢具有相同输出的多个标签。有没有办法实现这个功能?

不起作用的代码示例:

tabItem

1 个答案:

答案 0 :(得分:3)

您不能拥有重复的ID,它们必须是唯一的。您可以通过右键单击页面然后inspect自行调查,点击console,您将看到错误。为你Uncaught Duplicate binding for ID chart

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(), 
  dashboardSidebar(
    sidebarMenuOutput("menu"),
    textOutput("res")
  ),
  dashboardBody(
    tabItems(
      tabItem("dashboard", "Dashboard tab content"),
      tabItem("widgets", "Widgets tab content"),
      tabItem("chartsHome", "Main charts content"),
      tabItem("subitem1", uiOutput("chart")),
      tabItem("subitem2", uiOutput("chart2")) 
    )
  )
)

server <- function(input, output, session) {
  output$res <- renderText({
    paste("You've selected:", input$tabs)
  })
  output$menu <- renderMenu({
    sidebarMenu(
      # Setting id makes input$tabs give the tabName of currently-selected tab
      id = "tabs",

      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
      menuItem("Widgets", icon = icon("th"), tabName = "widgets"),
      menuItem("Charts", icon = icon("bar-chart-o"), tabName="chartsHome",
               menuSubItem("Sub-item 1", tabName = "subitem1"),
               menuSubItem("Sub-item 2", tabName = "subitem2")
      )
    )
  })

  toRender <- reactive({
    if (input$tabs == "subitem1") {
      HTML("Chart with first variable as output")
    } 
    else if (input$tabs == "subitem2") {
      HTML("Chart with second variable as output")
    }
    else{
      return()
    }
  })

  output$chart <- renderUI({
    toRender()
  })

  output$chart2 <- renderUI({
    toRender()
  })

}


shinyApp(ui, server)