简体   繁体   English

闪亮的条件主面板

[英]Conditional Main Panel in Shiny

I'm building a Shiny App where I want the Main Panel to be dynamic, such that when one drop down menu is chosen a new plot is created. 我正在构建一个Shiny App,我希望Main Panel是动态的,以便在选择一个下拉菜单时创建一个新图。 I understand how to do it where the plots are on top of each other (which sucks because I have table underneath that and the User will have to scroll down). 我知道在情节彼此重叠的地方怎么做(这很烂,因为我在下面有表格,而用户必须向下滚动)。 What would be great is if the Main Panel Graph just 'switches'. 如果“主面板图”仅是“开关”,那就更好了。 I'm not sure if ConditinalPanel would work here? 我不确定ConditinalPanel是否可以在这里工作? Or even a Switch statement? 甚至是Switch语句? Here is my UI. 这是我的用户界面。

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


        )
)
)

Yes, you can certainly have conditional panels in the mainPanel plotting area. 是的,您肯定可以在mainPanel绘图区域中使用条件面板。 Your code was quite close to being workable (just one or two errant parentheses). 您的代码几乎可以使用(只有一个或两个错误的括号)。 Below is revised code with and dummy plots to show how it works. 下面是带有伪图的修订代码,以显示其工作方式。 You'll obviously have to update with what you actually want for plots. 显然,您将不得不更新实际需要的绘图。 The basic structure should be quite clear. 基本结构应该很清楚。 In the UI, just include your conditionalPanels in the mainPanel items, and then specify your plots separately in the server. 在用户界面中,只需将您的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: 服务器:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM