Using similar UI script in R shiny under multiple subMenuItems

江枫思渺然 提交于 2019-11-29 17:40:06

Your code can be rewritten with shiny modules. The UI module where you want to display the two dropdowns can be written as one (Ui function) and then can be referred in the places you want.

Modified code:

library(shiny)
library(shinydashboard)

submenuUI <- function(id) {
  ns <- NS(id)


  # return a list of tags
  tagList(
     column(2,offset = 0, style='padding:1px;', 
                               selectInput(ns("select1"),"select1",c("A1","A2","A3"), selected = "A1")),
            column(2,offset = 0, style='padding:1px;', 
                   selectInput(ns("select2"),"select2",c("A3","A4","A5"), selected = "A3"))
  )


}


submenu <- function(input,output,session){}


ui <- dashboardPage(
  dashboardHeader(), 
  dashboardSidebar(
    sidebarMenu(

      id = "tabs",
      menuItem("Charts", icon = icon("bar-chart-o"),
               menuSubItem("Sub-item 1", tabName = "subitem1"),
               menuSubItem("Sub-item 2", tabName = "subitem2"),
               menuSubItem("Sub-item 3", tabName = "subitem3"),
               menuSubItem("Sub-item 4", tabName = "subitem4")
      ))),
  dashboardBody(
    tabItems(tabItem("subitem1", submenuUI('submenu1')),
      tabItem("subitem2", submenuUI('submenu2')),
      tabItem("subitem3", submenuUI('submenu3')),
      tabItem("subitem4", "Sub-item 2 tab content"))))
server <- function(input, output, session) {
  callModule(submenu, "submenu")
}
shinyApp(ui, server)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!