1

I am trying to use plotOutput in R shiny in multiple sub-menu items. However, I believe it is not possible for plotOutput to be used at multiple places with same id. Please help me if it is possible somehow. attaching the snapshot for reference.

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")
  ))),
dashboardBody(
tabItems(tabItem("subitem1", 3),
         tabItem("subitem2", plotOutput("brand_selector")))
 ))
server <- shinyServer(function(input, output) {
output$brand_selector <-  renderPlot({
plot(iris$Sepal.Length)
}) 
})
shinyApp(ui = ui, server = server)

snapshot

Update

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","A2","A1","A3","A1"))),
column(2,offset = 0, style='padding:1px;', 

selectInput(ns("select2"),"select2",c("B3","B4","B5","B3","B6","B2","B3")))
)
}
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) {
  observeEvent(input$Select1,{
  updateSelectInput(session,'Select2',
                  choices= input$select2[input$Select1 == "A1"] )
   }) 
   callModule(submenu, "submenu")
   }
   shinyApp(ui, server)

Please help me to update the second selectInput based on input from the previous selectInput.

Adam Shaw
  • 519
  • 9
  • 24
  • 1
    Maybe this post can help, https://stackoverflow.com/questions/48337624/using-similar-ui-script-in-r-shiny-under-multiple-submenuitems/48338347#48338347 – Adam Shaw Jan 22 '18 at 11:24

1 Answers1

2

This is the modularized code for multiple plotting: Very similar to the selectInput one.

library(shiny)
library(shinydashboard)

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


  # return a list of tags
  tagList(
   plotOutput(ns('plt'))
  )


}



plotop <- function(input,output,session){
  ns <- session$ns

  output$plt <- renderPlot({
    plot(iris$Sepal.Length)
  })
}


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")
      ))),
  dashboardBody(
    tabItems(tabItem("subitem1", plotopUI('plt1')),
             tabItem("subitem2", plotopUI('plt2')))))

server <- function(input, output, session) {
  callModule(plotop, "plt1")
  callModule(plotop, "plt2")
}
shinyApp(ui, server)
amrrs
  • 6,215
  • 2
  • 18
  • 27
  • thanks a lot for relpying here, I shall definitely accept the solution, however as I am progressing ahead in my work, I came to see that, the help I need here is a little different, I have observeEvent function which I am not able to use with shinyModules , please help me with a fix on the same. I regret for the inconvenience. – Adam Shaw Jan 22 '18 at 12:16
  • Please check the script below the Update, it is not a new requirement but an addon to what I had asked, the observeEvent is not getting fixed along with shiny modules, I would greatly appreciate your help here. – Adam Shaw Jan 22 '18 at 12:25
  • @AdamShaw I've to rush out, I'll reply after a few hours. – amrrs Jan 22 '18 at 13:16
  • thanks, I appreciate it, I have posted an altogether new question and would like you to assist here https://stackoverflow.com/questions/48385417/automating-selectinput-and-menuitems-in-r-shiny – Adam Shaw Jan 22 '18 at 15:53
  • I have put my own effort and done furthur simplification of the requirement, please help me here, I am sure this is easy to crack, https://stackoverflow.com/questions/48394545/usage-of-uioutput-in-multiple-menuitems-in-r-shiny-dashboard – Adam Shaw Jan 23 '18 at 05:13
  • please help me with this script here based on R shiny Modules, https://stackoverflow.com/questions/48496426/display-the-selectinput-value-in-a-r-shiny-widget-box – Adam Shaw Jan 29 '18 at 08:23