创建反应式renderUI

时间:2016-05-07 18:08:14

标签: r shiny

我正在关注this tutorial以学习构建闪亮的应用程序。在最终版本中,renderUI()用于"国家"只需占用数据集中的所有国家/地区。有没有办法根据使用滑块选择的价格范围使此列表被动/过滤?

以下是相关代码:

output$countryOutput <- renderUI({
    selectInput("countryInput", "Country",
                sort(unique(bcl$Country)),
                selected="CANADA")
  })

这是整个。非常简单的应用程序:

library(shiny)
library(ggplot2)
library(dplyr)

bcl <- read.csv("bcl-data.csv", stringsAsFactors = FALSE)

ui <- fluidPage(
         titlePanel("BC Liquor Prices", windowTitle = "Mmmmm yeh"),
         sidebarLayout(
           sidebarPanel(
             sliderInput("priceInput", "Price",
                         min = 0, max = 100,
                         value = c(25, 40), pre = "$"
                         ),
             radioButtons("typeInput", "Product type",
                          choices = c("BEER", "REFRESHMENT", "SPIRITS", "WINE"),
                          selected = "WINE"
                          ),
             uiOutput("countryOutput")
           ),
           mainPanel(plotOutput("coolplot"),
                     br(),
                     br(),
                     tableOutput("results")
                     )
           )
      )
server <- function(input, output, session) {

  filtered <- reactive({
    if (is.null(input$countryInput)) {return(NULL)} 

    bcl %>%
      filter(
        Price >= input$priceInput[1],
        Price <= input$priceInput[2],
        Type == input$typeInput,
        Country == input$countryInput
      )
  })

  output$countryOutput <- renderUI({
    selectInput("countryInput", "Country",
                sort(unique(bcl$Country)),
                selected="CANADA")
  })

  output$coolplot <- renderPlot({
    if (is.null(filtered())) {return()}
                      ggplot(filtered(), aes(Alcohol_Content)) + geom_histogram()
                    })

  output$results <- renderTable({
                      filtered()
                    })
}

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:2)

尝试此代码...您需要两个不同的被动值:1)一个用于根据前两个输入生成国家/地区列表,以及2)两个根据所选国家/地区生成绘图和表格结果。

另外,我更改了名称以反映我在读取该文件时获得的实际列名。如果您在代码的其他部分更改了它们,则可能需要更改。

library(shiny)
library(ggplot2)
library(dplyr)

bcl <- read.csv("bcl-data.csv", stringsAsFactors = FALSE)

ui <- fluidPage(
  titlePanel("BC Liquor Prices", windowTitle = "Mmmmm yeh"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("priceInput", "Price",
                  min = 0, max = 100,
                  value = c(25, 40), pre = "$"
      ),
      radioButtons("typeInput", "Product type",
                   choices = c("BEER", "REFRESHMENT", "SPIRITS", "WINE"),
                   selected = "BEER"
      ),
      uiOutput("countryOutput")
    ),
    mainPanel(plotOutput("coolplot"),
              br(),
              br(),
              tableOutput("results")
    )
  )
)

server <- function(input, output, session) {
  filteredForCountry <- reactive({
    bcl %>%
      filter(
        CURRENT_DISPLAY_PRICE >= input$priceInput[1],
        CURRENT_DISPLAY_PRICE <= input$priceInput[2],
        PRODUCT_SUB_CLASS_NAME == input$typeInput
      )
  })
  output$countryOutput <- renderUI({
    df <- filteredForCountry()
    if (!is.null(df)) {
      selectInput("countryInput", "Country",
                  sort(unique(df$PRODUCT_COUNTRY_ORIGIN_NAME)),
                  selected="CANADA")
    }
  })

  filteredFull <- reactive({
    if (is.null(input$countryInput)) {
      return (filteredForCountry())
    }
    bcl %>%
      filter(
        CURRENT_DISPLAY_PRICE >= input$priceInput[1],
        CURRENT_DISPLAY_PRICE <= input$priceInput[2],
        PRODUCT_SUB_CLASS_NAME == input$typeInput,
        PRODUCT_COUNTRY_ORIGIN_NAME == input$countryInput
      )
  })
  output$coolplot <- renderPlot({
    if (is.null(filteredFull())) {return()}
    ggplot(filteredFull(), aes(PRODUCT_ALCOHOL_PERCENT)) +
      geom_histogram(binwidth = 0.05)
  })

  output$results <- renderTable({
    filteredFull()
  })
}

shinyApp(ui = ui, server = server)