3

I am trying to create this dashboard with following structure. It basic structure has 3 levels (Menu, sub Menu and radioGroupButton selection) SideBar: Menu1: has 2 sub menus (Sub Menu 1 and Sub Menu2) Menu2 has 2 sub menus (Sub Menu 3 and Sub Menu4) The body has radioGroupButton that has 2 choices

dashBoardBody: RadioGroupButton: Choice 1 and Choice 2

So when the user clicks Sub Menu1 and clicks Choice 1 then I need to display #A1 Sub Menu1 and clicks Choice 2 then I need to display #A2

Sub Menu2 and clicks Choice 1 then I need to display #A3 Sub Menu2 and clicks Choice 2 then I need to display #A4 Likewise for sub menu 3 and 4.

I want #A1...A8 to hide or display based on the selection on Choice 1 or Choice 2. I didnt know how to show or hide the fluidRows (#A1- #A8). Please advise.

`shinyApp(
ui= dashboardPagePlus(skin="purple-light",dashboardHeader(title="Testing"),
dashboardSidebar(width=200,
sidebarMenu(
menuItem("Menu1", tabName = "dashboard",startExpanded = TRUE,
menuSubItem("Sub Menu1", tabName = "sub1"),
menuSubItem("Sub Menu2", tabName = "sub2")),
menuItem("Menu2", tabName= "Widgets",startExpanded = TRUE,
menuSubItem("Sub Menu3", tabName = "sub3"),
menuSubItem("Sub Menu4", tabName = "sub4")))),

dashboardBody(radioGroupButtons("rb1",label=NULL, choices = c("choice1","choice2"), selected= 
"choice1",individual= TRUE,status="info", justified= TRUE ,direction= "horizontal"),

tabItems(
tabItem("sub1",title= "Tab1",
fluidRow(plotOutput("plotgraph1")), #A1
fluidRow(plotOutput("plotgraph2"))), #A2

tabItem("sub2", title= "Tab2",
fluidRow(plotOutput("plotgraph3")), #A3
fluidRow(plotOutput("plotgraph1"))), #A4

tabItem("sub3", title= "Tab3",
fluidRow(plotOutput("plotgraph3")), #A5
fluidRow(plotOutput("plotgraph2"))), #A6

tabItem("sub4", title= "Tab4",
fluidRow(plotOutput("plotgraph1")), #A7
fluidRow(plotOutput("plotgraph1")) )))), #A8

server = function(input,output){
set.seed(1234)
pt1 <- qplot(rnorm(500),fill=I("red"),binwidth=0.2,title="plotgraph1")
pt3 <- qplot(rnorm(600),fill=I("blue"),binwidth=0.2,title="plotgraph3")
pt2 <- reactive({input$rb1
if (input$rb1 =="choice1"){
return(qplot(rnorm(500),fill=I("blue"),binwidth=0.2,title="plotgraph2"))
} else {
return(NULL)
}
})
output$plotgraph1 = renderPlot({pt1})
output$plotgraph2 = renderPlot({pt2()})
output$plotgraph3 = renderPlot({pt3})
})

)`

Kris4u
  • 39
  • 4
  • Welcome to SO! Please provide a reproducible example with all packages you use – starja Jul 23 '20 at 08:18
  • you should look into conditionalPanel - here is another answer where I used them https://stackoverflow.com/a/60967925/1951485 – Bertil Baron Jul 23 '20 at 09:06
  • I used the following libraries; library(shiny) library(shinydashboard) library(shinythemes) library(shinyWidgets) library(dplyr) library(lubridate) library(tidyverse) library(data.table) library(shinydashboardPlus) – Kris4u Jul 23 '20 at 19:38
  • Bertil, I tried to look that the link you shared, but could not see how it could help me with my request. Please could you clarify my above mentioned question... – Kris4u Jul 23 '20 at 20:21
  • Can someone help me out here please..... – Kris4u Jul 23 '20 at 21:33

1 Answers1

0

For demonstration purpose, I have implemented for one sub-menu. You can expand to others similarly. You were very close.

The following code:

ui <-  dashboardPage(#skin="purple-light",
                     dashboardHeader(title="Testing"),
                      dashboardSidebar(width=200,
                                       sidebarMenu(
                                         menuItem("Menu1", tabName = "dashboard",startExpanded = TRUE,
                                                  menuSubItem("Sub Menu1", tabName = "sub1"),
                                                  menuSubItem("Sub Menu2", tabName = "sub2")),
                                         menuItem("Menu2", tabName= "Widgets",startExpanded = TRUE,
                                                  menuSubItem("Sub Menu3", tabName = "sub3"),
                                                  menuSubItem("Sub Menu4", tabName = "sub4")))),
                      
                      dashboardBody(radioGroupButtons("rb1",label=NULL, choices = c("choice1","choice2"), selected= 
                                                        "choice1",individual= TRUE,status="info", justified= TRUE ,direction= "horizontal"),
                                    
                                    tabItems(
                                      tabItem("sub1",title= "Tab1",
                                              #fluidRow(plotOutput("plotgraph1")), #A1
                                              fluidRow(plotOutput("plotgraph2"))) #A2
                                      
                                      # tabItem("sub2", title= "Tab2",
                                      #         fluidRow(plotOutput("plotgraph3")), #A3
                                      #         fluidRow(plotOutput("plotgraph1"))), #A4
                                      # 
                                      # tabItem("sub3", title= "Tab3",
                                      #         fluidRow(plotOutput("plotgraph3")), #A5
                                      #         fluidRow(plotOutput("plotgraph2"))), #A6
                                      # 
                                      # tabItem("sub4", title= "Tab4",
                                      #         fluidRow(plotOutput("plotgraph1")), #A7
                                      #         fluidRow(plotOutput("plotgraph1")) ) #A8
                                      ))) #A8

server <- function(input,output){
  set.seed(1234)
  pt1 <- qplot(rnorm(500),fill=I("red"),binwidth=0.2,title="plotgraph1")
  pt3 <- qplot(rnorm(600),fill=I("blue"),binwidth=0.2,title="plotgraph3")
  pt2 <- reactive({
    req(input$rb1)
    if (input$rb1 =="choice1"){
      return(qplot(rnorm(500),fill=I("blue"),binwidth=0.2,title="plotgraph1"))
    }else if (input$rb1 =="choice2") {
      return(qplot(rnorm(500),fill=I("red"),binwidth=0.2,title="plotgraph2"))
    }else {return(NULL)}
  })
  output$plotgraph1 = renderPlot({pt1})
  output$plotgraph2 = renderPlot({pt2()})
  output$plotgraph3 = renderPlot({pt3})
}

shinyApp(ui, server)

gives this output:

output

You can add the skin and expand it. Also, yes, you can add a bunch of items in a given tab. For example, you can output a plot, datatable, uiOutput, etc., as shown below.

tabPanel("Plot", value="tab3", 

         plotlyOutput("plot11", height=750),

         DT::dataTableOutput("testtable3")

         uiOutput("saveplotsbtn1"))
YBS
  • 19,324
  • 2
  • 9
  • 27
  • Thanks for your help, I can complete the rest. – Kris4u Jul 24 '20 at 00:36
  • I tried to use the changes as per your suggestion, it worked for one tabItem, but when I added second tabItem, it was not working. Could you please suggest what am I going wrong here. – Kris4u Jul 26 '20 at 01:09