简体   繁体   中英

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.

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 . The input$ is correct, but the variable names are different. So you are basically doing a filter on 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. It may be a field in your dataframe, but not in your UI. So you need:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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