简体   繁体   English

R Shinydashboard子菜单使用相同的UI

[英]R Shinydashboard submenuitems using same UI

I have a basic app with a MenuItem that when clicked displays a plot of a specific variable. 我有一个带有MenuItem的基本应用程序,点击它时会显示特定变量的图表。 The variable can be chosen from a SelectInput in the UI for this MenuItem . 可以从此MenuItem的UI中的SelectInput中选择变量。

However, what I'd really like to do is to have the possible variables be displayed as subMenuItems of the original MenuItem , so that when one is clicked it grabs the currently selected variable and renders the plot. 但是,我真正想做的是将可能的变量显示为原始MenuItem subMenuItems ,这样当单击一个时,它会抓取当前选定的变量并渲​​染绘图。 This looks visually neater to me than using a SelectInput dropdown. 与使用SelectInput下拉列表相比,这看起来更符合我的SelectInput

I was trying to get this working by having each tabItem link to the same uiOutput that displays the appropriate content, but Shiny doesn't seem to like having multiple tabs with the same output. 我试图通过让每个tabItem链接到显示相应内容的相同uiOutput这一点,但Shiny似乎不喜欢具有相同输出的多个选项卡。 Is there any way to achieve this functionality? 有没有办法实现这个功能?

Code example that doesn't work: 代码示例不起作用:

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("chart")) 
        )
    )
)

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

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


}


shinyApp(ui, server)

You can't have duplicate IDs, they have to be unique. 您不能拥有重复的ID,它们必须是唯一的。 You can investigate these yourself by right clicking on the page and then inspect , click on console and you will see the errors. 您可以通过右键单击页面然后inspect ,单击console调查这些,您将看到错误。 For you its Uncaught Duplicate binding for ID chart 对于你的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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM