简体   繁体   中英

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. 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? Or even a Switch statement? 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. 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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