Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 网络中的子过滤数据集_R_Shiny - Fatal编程技术网

R 网络中的子过滤数据集

R 网络中的子过滤数据集,r,shiny,R,Shiny,我正在阅读关于的闪亮教程,我被层叠过滤器或子过滤器卡住了 本教程使用。数据包含BC酒类商店销售的所有产品的信息 我想做的是,当我选择变量PRODUCT\u CLASS\u NAME时,我希望PRODUCT\u MINOR\u CLASS\u NAME的选择仅限于PRODUCT\u CLASS\u NAME中的选择。所以,如果在产品类别名称中选择了啤酒,我就没有选择权,比如说,在产品类别名称下选择美国威士忌 一些设置代码: library(shiny) library(ggplot2) lib

我正在阅读关于的闪亮教程,我被层叠过滤器或子过滤器卡住了

本教程使用。数据包含BC酒类商店销售的所有产品的信息

我想做的是,当我选择变量PRODUCT\u CLASS\u NAME时,我希望PRODUCT\u MINOR\u CLASS\u NAME的选择仅限于PRODUCT\u CLASS\u NAME中的选择。所以,如果在产品类别名称中选择了啤酒,我就没有选择权,比如说,在产品类别名称下选择美国威士忌

一些设置代码:

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(
                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)
我意识到这是由于没有完全理解光泽或过滤器。有没有更好的方法来获得我想要的结果


谢谢

我想你把一些过滤弄混了。看看我介绍的更新。请注意,数据集中没有带大写的列

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

多亏猪排帮我明白了我不知道如何使用过滤器

以下是基于猪排的最终代码,它在实际数据集上工作:

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)

只是在子类中用df替换bcl。对不起,我原来的帖子拉错了CSV文件。当我的实际代码引用原始CSV时,它正在从教程中提取一个经过编辑的CSV。我在上面编辑了我的代码。啊哈!这一修正奏效了。我对如何使用过滤器的理解很差。我将使用真实的数据集发布我的最终代码。谢谢