首页 > 解决方案 > Shiny 中的条件主面板

问题描述

我正在构建一个闪亮的应用程序,我希望主面板动态,以便在选择一个下拉菜单时创建新图。我了解如何在绘图彼此重叠的情况下进行操作(这很糟糕,因为我在其下方有表格并且用户必须向下滚动)。如果主面板图只是“切换”,那就太好了。我不确定 ConditinalPanel 是否可以在这里工作?甚至是 Switch 语句?这是我的用户界面。

source("DATA CLEANING.R")

salespeople <- sort(unique(salesdatav3$SALESPERSON))


# Define UI for application that draws a histogram
ui <- fluidPage(theme = shinytheme("united"),

   # Application title
   titlePanel("Pounds_New"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
        pickerInput("slsp", "SalesPerson", choices = salespeople, selected =NULL, options = list(`actions-box` = TRUE), multiple = T),


      pickerInput("stats", "Summary Stats", choices = as.vector(c("Positive/Negative Count", "Histogram", "Plot Pounds by Time", "Top Ten Positive Trending",
                                                        "Top Ten Negative Trending")), selected = NULL, multiple = F, list(`actions-box` = TRUE))

      ),

      # Show a plot of the generated distribution
        mainPanel(
          plotOutput("sidebarplot"),
          # conditionalPanel(
          #   condition =  "input.stats == 'Histogram'",
          #   plotOutput("histt"),

          # conditionalPanel(
          #   condition = "input.slsp",
            DT::dataTableOutput("data_table"),
          plotOutput("plot_pounds")


        )
)
)

标签: rshiny

解决方案


是的,您当然可以在 mainPanel 绘图区域中使用条件面板。您的代码非常接近可行(只有一两个错误的括号)。下面是修改后的代码和虚拟图,以显示它是如何工作的。你显然必须更新你真正想要的情节。基本结构应该很清楚。在 UI 中,只需将您conditionalPanels的项目包含在mainPanel项目中,然后在服务器中单独指定您的图。

用户界面:

library(shiny)
library(shinythemes)
library(shinyWidgets)
ui <- fluidPage(theme = shinytheme("united"),

            # Application title
            titlePanel("Pounds_New"),

            # Sidebar with a slider input for number of bins 
            sidebarLayout(
              sidebarPanel(
                pickerInput("slsp", "SalesPerson", choices = c("a","b","c","d"), selected =NULL, options = list(`actions-box` = TRUE), multiple = T),


                pickerInput("stats", "Summary Stats", choices = as.vector(c("Positive/Negative Count", "Histogram", "Plot Pounds by Time", "Top Ten Positive Trending",
                                                                            "Top Ten Negative Trending")), selected = NULL, multiple = F, list(`actions-box` = TRUE))

              ),

              # Show a plot of the generated distribution
              mainPanel(
                conditionalPanel(
                  condition = "input.stats == 'Positive/Negative Count'",
                  plotOutput("sidebarplot")
                ),
                conditionalPanel(
                  condition =  "input.stats == 'Histogram'",
                  plotOutput("histt")
                ),
                conditionalPanel(
                  condition = "input.slsp",
                  # DT::dataTableOutput("data_table"),
                  plotOutput("plot_pounds")
                )
              )
            )
)

服务器:

server <- function(input, output) {
  output$sidebarplot <- renderPlot({
    hist(rnorm(50),10)
  })

  output$histt <- renderPlot({
    hist(runif(50),10)
  })

  output$plot_pounds <- renderPlot({
    hist(rbeta(50,1,5),10)
  })

}

shinyApp(ui, server)

推荐阅读