在Shiny中的条件主面板

5

我正在构建一个Shiny App,在其中我希望主面板是动态的,这样当选择一个下拉菜单时会创建一个新的图形。我知道如何在图形堆叠的情况下实现它(这很糟糕,因为我下面有一个表格,用户必须向下滚动)。如果主面板图形只是“切换”那就太棒了。我不确定ConditinalPanel是否适用于此?或者使用Switch语句?这是我的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")


        )
)
)
1个回答

5

是的,在mainPanel绘图区域中可以有条件面板。你的代码已经非常接近可用(只有一个或两个错误的括号)。下面是修改后的代码和虚拟图以展示它的工作原理。你显然需要使用实际的数据更新代码。基本结构应该很清楚。在UI中,只需将你的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)

哦,伙计,我希望有人能回答。谢谢!非常准确。 :) - astronomerforfun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接