How to hide or display charts in shiny dashboardBody based on radioGroupButton selection

安稳与你 提交于 2020-08-08 04:06:13

问题


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

)`


回答1:


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:

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


来源:https://stackoverflow.com/questions/63045963/how-to-hide-or-display-charts-in-shiny-dashboardbody-based-on-radiogroupbutton-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!