简体   繁体   English

R闪亮的过滤器问题

[英]R shiny Filter issues

Having trouble with executing functioning filters in my R shiny project (not sure if it is an issue with the UI or the server) --> neighbourhood and number of reviews will not communicate with room type and region filters.在我的 R 闪亮项目中执行功能过滤器时遇到问题(不确定这是 UI 还是服务器的问题)--> 社区和评论数量不会与房间类型和区域过滤器通信。

Server Code服务器代码

options(shiny.maxRequestSize=30*1024^2)
library(shiny)
library(leaflet)
library(dplyr)

# Define server that analyzes the Singapore Airbnb Listings
shinyServer(function(input, output) {

  # Create an output variable for problem description
  output$text <- renderText({

    "This project uses the dataset .................FILL OUT LATER........."

  })


  # Create a descriptive table for NBHD
  output$table1 <- renderPrint({

    # Connect to the sidebar of file input
    inFile <- input$file

    if(is.null(inFile))
      return("Please Upload A File For Analysis")

    # Read input file
    mydata <- read.csv(inFile$datapath)
    attach(mydata)

    # Filter the data for different Room Types and Regions
    target1 <- c(input$room_type)
    neighbourhood_df <- filter(mydata, room_type %in% target1)

    # Create a table for NBHD
    table(mydata$neighbourhood)

  })

  # Create a descriptive table for # of reviews
  output$table2 <- renderPrint({

    # Connect to the sidebar of file input
    inFile1 <- input$file

    if(is.null(inFile1))
      return("Please Upload A File For Analysis")

    # Read input file
    mydata1 <- read.csv(inFile1$datapath)
    attach(mydata1)

    # Filter the data for different Room Type and Region
    target3 <- c(input$room_type)
    target4 <- c(input$region)
    number_of_reviews_df <- filter(mydata1, room_type %in% target3 & region %in% target4)

    # Create a table for Rooms
    table(mydata1$number_of_reviews)

  })


  # Create a map output variable
  output$map <- renderLeaflet({

    # Connect to the sidebar of file input
    inFile2 <- input$file

    if(is.null(inFile2))
      return(NULL)

    # Read input file
    mydata2 <- read.csv(inFile2$datapath)
    attach(mydata2)

    # Filter the data for different Room Type and Region
    target5 <- c(input$room_type)
    target6 <- c(input$region)
    map_df <- filter(mydata2, room_type %in% target5 & region %in% target6)

    # Create colors with a categorical color function
    color <- colorFactor(rainbow(9), mydata2$neighbourhood)

    # Create the leaflet function for data
    leaflet(map_df) %>%

      # Set the default view
      setView(lng = 103.8608, lat = 1.2834, zoom = 12) %>%

      # Provide tiles
      addProviderTiles("CartoDB.Positron", options = providerTileOptions(noWrap = TRUE)) %>%

      # Add circles
      addCircleMarkers(
        radius = 2,
        lng= mydata2$longitude,
        lat= mydata2$latitude,
        stroke= FALSE,
        fillOpacity=0.1,
        color=color(neighbourhood)
      ) %>%

      # Add legends for different nbhds
      addLegend(
        "bottomleft",
        pal=color,
        values=neighbourhood,
        opacity=0.5,
        title="Singapore Neighbourhood"
      )
  })
})

UI Code用户界面代码

    library(shiny)
library(leaflet)
library(shinythemes)

# Define UI for application that analyzes the patterns of crimes in DC
shinyUI(fluidPage(

  # Change the theme to flatly
  theme = shinytheme("flatly"),

  # Application title
  titlePanel("Patterns of Crimes in Washington DC"),

  # Three sidebars for uploading files, selecting Room types and Regions
  sidebarLayout(
    sidebarPanel(

      # Create a file input
      fileInput("file","Choose A CSV File Please",
                multiple = TRUE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),

      # Create a multiple checkbox input for Room Type
      checkboxGroupInput("RoomType",
                         "Room Type:",
                         c("Private Room","Entire home/apt","Shared room")
      ),

      hr(),
      helpText("Please Select The Room type for listing analysis"),
      helpText("You Can Choose More Than One"),

      hr(),
      hr(),

      # Create a multiple checkbox input for Regions
      checkboxGroupInput("Region",
                         "Region:",
                         choices = list("Central Region"= 1,"East Region"= 2,"North Region"= 3,"North-East Region"= 4,
                                        "West Region"= 5)
      ),

      hr(),
      helpText("Please Select The Regions You Would Like To Analyze For Listing Patterns"),
      helpText("You Can Choose More Than One")
    ),

    # Make the sidebar on the right of the webpage
    position = "right",
    fluid = TRUE,



    # Create two tabs
    mainPanel(
      hr(),
      tabsetPanel(type="tabs",

                  #Add a tab for Analysis Overview
                  tabPanel("Analysis Overview", textOutput("text")),

                  #Add a tab for Room type and Region
                  tabPanel("Listing Analysis",

                           #Add two subtabs
                           tabsetPanel(
                             tabPanel("Neighbourhood",verbatimTextOutput("table1")),
                             tabPanel("Number of Reviews",verbatimTextOutput("table2"))
                           )
                  ),


                  #Tab for the Leaflet Map
                  tabPanel("Map", leafletOutput("map", height=630))
      )
    )
  )
))

In your UI you call the variables RoomType and Region , but in your server code you call them input$room_type and input$region .在您的 UI 中,您将变量称为RoomTypeRegion ,但在您的服务器代码中,您将它们称为input$room_typeinput$region The input$ is correct, but the variable names are different. input$是正确的,但是变量名不同。 So you are basically doing a filter on room_type %in% NULL .所以你基本上是对room_type %in% NULL进行过滤。

Edit for Clarification:编辑澄清:

In the UI you have:在用户界面中,您有:

# Create a multiple checkbox input for Room Type
checkboxGroupInput("RoomType",
                   "Room Type:",
                   c("Private Room","Entire home/apt","Shared room")
)

In the Server you have:在服务器中,您有:

# Filter the data for different Room Type and Region
target3 <- c(input$room_type)

room_type is not a valid input from the UI. room_type不是来自 UI 的有效输入。 It may be a field in your dataframe, but not in your UI.它可能是您数据框中的一个字段,但不是您的 UI 中的一个字段。 So you need:所以你需要:

# Filter the data for different Room Type and Region
target3 <- c(input$RoomType)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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