Shiny reactive中的子滤波数据集

时间:2016-11-29 17:30:39

标签: r shiny

我正在浏览this website上的Shiny教程,我遇到了级联过滤器或子过滤器。

本教程使用this dataset。数据"包含有关BC Liquor Store"。

销售的所有产品的信息

我想要发生的是当我选择变量PRODUCT_CLASS_NAME时,我希望PRODUCT_MINOR_CLASS_NAME的选择范围仅限于PRODUCT_CLASS_NAME中的选项。因此,如果在PRODUCT_CLASS_NAME中选择了BEER,我就无法选择在PRODUCT_MINOR_CLASS_NAME下选择AMERICAN WHISKEY。

一些设置代码:

library(shiny)

library(ggplot2)

library(dplyr)

bcl <- read.csv("http://pub.data.gov.bc.ca/datasets/176284/BC_Liquor_Store_Product_Price_List.csv", stringsAsFactors = F)

这是我的ui:

ui <- fluidPage(
                titlePanel("BC Liquor Store prices"),
                sidebarLayout(
                    sidebarPanel(
                uiOutput("countryOutput"),
                        sliderInput("priceInput", "Price", min = 0, max = 100, value=c(0,50), pre="$"),
                        #radioButtons("typeInput", "Product Type", choices = c("BEER", "REFRESHMENT", "SPIRITS", "WINE"), selected="WINE"),
                uiOutput("typeOutput"),
                uiOutput("subtypeOutput")
                #selectInput("countryInput", "Country", choices = c("CANADA", "FRANCE", "ITALY"))               
                    ),               
                    mainPanel(
                        plotOutput("coolplot"),
                        br(),
                        tableOutput("results")
                    )
                )
)

这是我的服务器:

server <- function(input, output) {
                        # create a reactive to filter the dataset

                            df <- reactive({
                                # df() is trying to access teh country input, but the country input hasn't been created yet via uiOutput, so there is an initial error that goes away.  
                                # to prevent this temporary error, just include the following:
                                if (is.null(input$priceInput[1]) | is.null(input$priceInput[2]) | is.null(input$countryInput) | is.null(input$subtypeInput) | is.null(input$typeInput)) {
                                    return(NULL)
                                }

                                bcl <- bcl %>%
                                filter(CURRENT_DISPLAY_PRICE >= input$priceInput[1],
                                      CURRENT_DISPLAY_PRICE <= input$priceInput[2],
                                    PRODUCT_COUNTRY_ORIGIN_NAME %in% input$countryInput,
                                    PRODUCT_CLASS_NAME %in% input$typeInput,
                                    PRODUCT_MINOR_CLASS_NAME %in% input$subtypeInput) 
                                bcl
                        })
                        output$coolplot <- renderPlot({
                                # same error as above
                                if (is.null(df())) {
                                    return(NULL)
                                }
                                ggplot(df(), aes(PRODUCT_ALCOHOL_PERCENT)) + geom_histogram(binwidth = 1)
                        })
                        output$results <- renderTable({
                                df()
                        })
                        output$countryOutput <- renderUI({
                                selectInput("countryInput", "Country",
                                        sort(unique(bcl$PRODUCT_COUNTRY_ORIGIN_NAME))
                                )
                        })
                        output$typeOutput <- renderUI({
                                selectInput("typeInput", "Product type",
                                        sort(unique(bcl$PRODUCT_CLASS_NAME))
                                )
                        })
                        output$subtypeOutput <- renderUI({
                                selectInput("subtypeInput", "Product subtype",
                                        sort(unique(bcl$PRODUCT_MINOR_CLASS_NAME))
                                )
                        })
}


shinyApp(ui = ui, server = server)

我意识到这是因为没有完全理解Shiny或过滤器。有没有更好的方法来获得我想要的结果?

谢谢!

2 个答案:

答案 0 :(得分:0)

我认为你只是混淆了一些过滤。看看我介绍的更新。请注意,数据集中没有CAPS

的列
#rm(list = ls())
library(shiny)
library(ggplot2)
library(dplyr)
bcl <- read.csv("http://deanattali.com/files/bcl-data.csv", stringsAsFactors = F)

app <- shinyApp(
  ui <- fluidPage(
    titlePanel("BC Liquor Store prices"),
    sidebarLayout(
      sidebarPanel(
        selectInput("countryInput", "Country",sort(unique(bcl$Country))),
        sliderInput("priceInput", "Price", min = 0, max = 100, value=c(0,50), pre="$"),
        uiOutput("typeOutput"),
        uiOutput("subtypeOutput")          
      ),               
      mainPanel(
        plotOutput("coolplot"),
        br(),
        tableOutput("results")
      )
    )
  ),
  server <- function(input, output) {

    df0 <- eventReactive(input$countryInput,{
      bcl %>% filter(Country %in% input$countryInput)
    })
    output$typeOutput <- renderUI({
      selectInput("typeInput", "Product type",sort(unique(df0()$Name)))
    })

    df1 <- eventReactive(input$typeInput,{
      df0() %>% filter(Country %in% input$countryInput)
    })

    output$subtypeOutput <- renderUI({
      selectInput("subtypeInput", "Product subtype",sort(unique(df1()$Subtype)))
    })

    df2 <- reactive({
      df1() %>% filter(Price >= input$priceInput[1], Price <= input$priceInput[2],Subtype %in% input$subtypeInput)
    })

    output$coolplot <- renderPlot({
      ggplot(df2(), aes(Alcohol_Content)) + geom_histogram(binwidth = 1)
    })
    output$results <- renderTable({
      df2()
    })
  })
runApp(app)

enter image description here

答案 1 :(得分:0)

感谢Pork Chop帮助我了解我并不知道如何使用过滤器。

这里是基于Pork Chop的最终代码,它适用于实际的数据集:

library(shiny)

library(ggplot2)

library(dplyr)


bcl <- read.csv("http://pub.data.gov.bc.ca/datasets/176284/BC_Liquor_Store_Product_Price_List.csv", stringsAsFactors = F)




ui <- fluidPage(
                titlePanel("BC Liquor Store prices"),
                sidebarLayout(
                    sidebarPanel(
                selectInput("countryInput", "Country",sort(unique(bcl$PRODUCT_COUNTRY_ORIGIN_NAME))),
                        sliderInput("priceInput", "Price", min = 0, max = 100, value=c(0,50), pre="$"),
                        uiOutput("typeOutput"),
                uiOutput("subtypeOutput")           
                    ),               
                    mainPanel(
                        plotOutput("coolplot"),
                        br(),
                        dataTableOutput("results")
                    )
                )
)



server <- function(input, output) {
                        # create a reactive to filter the dataset

                             df0 <- eventReactive(input$countryInput,{
                            bcl %>% filter(PRODUCT_COUNTRY_ORIGIN_NAME %in% input$countryInput)
                         })
                            output$typeOutput <- renderUI({
                            selectInput("typeInput", "Product type",sort(unique(df0()$PRODUCT_CLASS_NAME)))
                            })

                            df1 <- eventReactive(input$typeInput,{
                            df0() %>% filter(PRODUCT_CLASS_NAME %in% input$typeInput)
                            })

                            output$subtypeOutput <- renderUI({
                            selectInput("subtypeInput", "Product subtype",sort(unique(df1()$PRODUCT_MINOR_CLASS_NAME)))
                            })

                            df2 <- reactive({
                            df1() %>% filter(CURRENT_DISPLAY_PRICE >= input$priceInput[1],
                                            CURRENT_DISPLAY_PRICE <= input$priceInput[2],
                                        PRODUCT_MINOR_CLASS_NAME %in% input$subtypeInput)
                            })

                            output$coolplot <- renderPlot({
                            ggplot(df2(), aes(PRODUCT_ALCOHOL_PERCENT)) + geom_histogram(binwidth = 1)
                            })
                            output$results <- renderTable({
                            df2()
                            })
}




shinyApp(ui = ui, server = server)