1

the given R shiny script creates a dropdown menu in the sidebar with main and sub menu items. When you click on the first sub-item 1, you get two selectInputs in the dashboardBody. I want a functionality where I just declare these inputs once and use it multiple times in the other sub-items, just like how reactive function does.I want to do this to make the script fast and efficient. I have less knowledge of reactive functionality, please help and thanks.

library(shiny)
library(shinydashboard)
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", column(2,offset = 0, style='padding:1px;', 
  selectInput("select1","select1",c("A1","A2","A3"), selected = "A1")),
          column(2,offset = 0, style='padding:1px;', 
  selectInput("select2","select2",c("A3","A4","A5"), selected = "A3"))),
  tabItem("subitem2", "Widgets tab content"),
  tabItem("subitem3", "Sub-item 1 tab content"),
  tabItem("subitem4", "Sub-item 2 tab content"))))
  server <- function(input, output, session) {
  }
  shinyApp(ui, server)

Select Input Capture

Adam Shaw
  • 519
  • 9
  • 24

1 Answers1

1

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)
amrrs
  • 6,215
  • 2
  • 18
  • 27
  • Thanks for the solution, my small query is, I need to replicate this for 16 subitems, will it give a fast solution? – Adam Shaw Jan 19 '18 at 10:01
  • @AdamShaw Ideally it should, I guess. Why don't you test it? – amrrs Jan 19 '18 at 10:05
  • kindly check this post, I am struggling to integrate observe Event with shiny Modules here, kindly check the solution posted too, thanks, https://stackoverflow.com/questions/48394545/usage-of-uioutput-in-multiple-menuitems-in-r-shiny-dashboard/48394758#48394758 – Adam Shaw Jan 23 '18 at 06:13