簡體   English   中英

R閃亮的過濾器問題

[英]R shiny Filter issues

在我的 R 閃亮項目中執行功能過濾器時遇到問題(不確定這是 UI 還是服務器的問題)--> 社區和評論數量不會與房間類型和區域過濾器通信。

服務器代碼

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"
      )
  })
})

用戶界面代碼

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

在您的 UI 中,您將變量稱為RoomTypeRegion ,但在您的服務器代碼中,您將它們稱為input$room_typeinput$region input$是正確的,但是變量名不同。 所以你基本上是對room_type %in% NULL進行過濾。

編輯澄清:

在用戶界面中,您有:

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

在服務器中,您有:

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

room_type不是來自 UI 的有效輸入。 它可能是您數據框中的一個字段,但不是您的 UI 中的一個字段。 所以你需要:

# 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