闪亮的条件主面板

时间:2018-06-20 03:48:26

标签: r shiny

我正在构建一个Shiny App,我希望Main Panel是动态的,这样,当选择一个下拉菜单时,会创建一个新图。我知道在情节彼此重叠的地方怎么做(这很烂,因为我在下面有表格,而用户必须向下滚动)。如果“主面板图”仅是“开关”,那就更好了。我不确定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")


        )
)
)

1 个答案:

答案 0 :(得分:1)

是的,您当然可以在mainPanel绘图区域中有条件面板。您的代码几乎可以使用(只有一个或两个错误的括号)。下面是带有伪图的修订代码,以显示其工作方式。显然,您将不得不更新实际需要的绘图。基本结构应该很清楚。在用户界面中,只需将conditionalPanels放在mainPanel项中,然后在服务器中分别指定绘图即可。

UI:

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)
相关问题