Shiny 如何基于按钮选择在闪亮的仪表板主体中隐藏或显示图表

Shiny 如何基于按钮选择在闪亮的仪表板主体中隐藏或显示图表,shiny,shinydashboard,shiny-server,shinyapps,shiny-reactivity,Shiny,Shinydashboard,Shiny Server,Shinyapps,Shiny Reactivity,我正在尝试创建具有以下结构的仪表板。它的基本结构有3个层次(菜单、子菜单和按钮选择) 侧栏: 菜单1:有2个子菜单(子菜单1和子菜单2) 菜单2有2个子菜单(子菜单3和子菜单4) 主体具有两个选项的RadioGroup按钮 仪表板主体: RadioGroup按钮:选项1和选项2 所以当用户点击 子菜单1并单击选项1,然后我需要显示#A1 子菜单1并单击选项2,然后我需要显示#A2 子菜单2并单击选项1,然后我需要显示#A3 子菜单2并单击选项2,然后我需要显示#A4 子菜单3和4也是如此 我希望

我正在尝试创建具有以下结构的仪表板。它的基本结构有3个层次(菜单、子菜单和按钮选择) 侧栏: 菜单1:有2个子菜单(子菜单1和子菜单2) 菜单2有2个子菜单(子菜单3和子菜单4) 主体具有两个选项的RadioGroup按钮

仪表板主体: RadioGroup按钮:选项1和选项2

所以当用户点击 子菜单1并单击选项1,然后我需要显示#A1 子菜单1并单击选项2,然后我需要显示#A2

子菜单2并单击选项1,然后我需要显示#A3 子菜单2并单击选项2,然后我需要显示#A4 子菜单3和4也是如此

我希望根据选项1或选项2上的选择隐藏或显示#A1…A8。 我不知道如何显示或隐藏fluidRows(#A1-#A8)。请告知

`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})
})
shinyApp( ui=仪表板页面Plus(皮肤=“紫光”,仪表板标题(标题=“测试”), 仪表板侧栏(宽度=200, 侧边栏菜单( menuItem(“Menu1”,tabName=“dashboard”,startExpanded=TRUE, menuSubItem(“子菜单1”,tabName=“sub1”), menuSubItem(“子菜单2”,tabName=“sub2”), menuItem(“Menu2”,tabName=“Widgets”,startExpanded=TRUE, menuSubItem(“子菜单3”,tabName=“sub3”), menuSubItem(“子菜单4”,tabName=“sub4”), 仪表板主体(选定的按钮(“rb1”,标签=NULL,选项=c(“choice1”,“choice2”)) “choice1”,单个=TRUE,状态=info,对齐=TRUE,方向=horizontal”), tabItems( tabItem(“sub1”,title=“Tab1”, fluidRow(绘图输出(“绘图1”),#A1 fluidRow(绘图输出(“绘图2”)),#A2 tabItem(“sub2”,title=“Tab2”, fluidRow(绘图仪输出(“绘图仪3”),#A3 fluidRow(绘图输出(“绘图1”)),#A4 tabItem(“sub3”,title=“Tab3”, fluidRow(绘图仪输出(“绘图仪3”),#A5 fluidRow(绘图输出(“绘图2”)),#A6 tabItem(“sub4”,title=“Tab4”, fluidRow(绘图输出(“绘图1”),#A7 fluidRow(绘图输出(“绘图1”)),#A8 服务器=功能(输入、输出){ 种子集(1234)
pt1为了演示,我实现了一个子菜单。您可以扩展到其他子菜单。您非常接近

以下代码:

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)

欢迎使用SO!请提供一个可复制的示例,包括您使用的所有软件包。您应该查看conditionalPanel-这里是我使用它们的另一个答案。我使用了以下库:库(shiny)库(shinydashboard)库(shinythemes)库(shinyWidgets)库(dplyr)库(lubridate)库(tidyverse)库(data.table)图书馆(shinydashboardPlus)Bertil,我试图查看您共享的链接,但看不到它如何帮助我完成我的请求。请您澄清我的上述问题…有人可以在这里帮助我吗…谢谢您的帮助,我可以完成其余的。我尝试按照您的建议使用更改,它对一个选项卡项有效,但当我添加第二个选项卡项时tabItem,它不起作用。你能告诉我我这里出了什么问题吗。
tabPanel("Plot", value="tab3", 

         plotlyOutput("plot11", height=750),

         DT::dataTableOutput("testtable3")

         uiOutput("saveplotsbtn1"))