如何使用selectInput映射数据帧的子集

时间:2017-11-01 18:42:00

标签: r shiny leaflet gis

enter image description here

我有一个包含以下列的df:种类|纬度|经度。我想创建一个应用程序,允许用户使用selectInput选择一个物种,并绘制该物种的长/纬度信息。

    library(shiny)
library(leaflet)


fixInvase <- read.csv("fixed2011_buff_invasives.csv")

### Subsetted data that I would like to map

bTrefoil <- subset(fixInvase, Species == "Birdsfoot Trefoil", 
                   select = c(Species, latitude, longitude))
cThistle <- subset(fixInvase, Species == "Canada Thistle", 
                   select = c(Species, latitude, longitude))
cheatgrass <- subset(fixInvase, Species == "Cheatgrass", 
                     select = c(Species, latitude, longitude))
cBuckthorn <- subset(fixInvase, Species == "Common Buckthorn", 
                     select = c(Species, latitude, longitude))

...



ui <- fluidPage(
  titlePanel("2011 NWCA Invasive Species"),
  mainPanel(
    leafletOutput("map"),
    br(), br(),
    tableOutput("results")),
  sidebarPanel(
    ### User chooses the species to map
    selectInput("speciesInput", "Species",
                c("Birdsfoot Trefoil" = "bTrefoil", 
                  "Canada Thistle" = "cThistle",
                  "Cheatgrass" = "cheatgrass",
                  "Common Buckthorn" = "cBuckthorn"))
  ))

server <- function(input, output, session){

  ####
  output$map <- renderLeaflet({
    filtered <-
      fixInvase %>%
      filter(Species== input$speciesInput
      )
    leaflet(filtered) %>% addTiles() %>%
      fitBounds(~min(longitude), ~min(latitude), ~max(longitude), ~max(latitude))
  })
  output$results <- renderTable({
    filtered <-
      fixInvase %>%
      filter(Species == input$speciesInput
      )
    filtered
  })

}

shinyApp(ui, server)

在传单中,我可以使用以下方法绘制数据框的子集:

bTrefoil <- subset(fixInvase, Species == "Birdsfoot Trefoil", 
                   select = c(Species, latitude, longitude))
leaflet(data = bTrefoil) %>% addTiles() %>%
  addMarkers(~longitude, ~latitude, popup = ~as.character(Species), label = ~as.character(Species))

但是当我尝试使用selectInput来子集并绘制数据时,我会遇到错误。

有没有办法根据selectInput中的用户选择对df进行子集化,并且只有那些点的传单映射?

1 个答案:

答案 0 :(得分:1)

在运行应用程序之前,不知道为什么要进行子集化。

我刚建立一个示例df(下次请提供dput()示例

library(shiny)
library(leaflet)
library(dplyr)

fixInvase <- structure(list(Species = structure(c(1L, 1L, 2L, 2L, 3L, 4L, 5L), .Label = c("Birdsfood Trefoil", "Canada Thistle", "Cheatgrass", "Common Buckthorn", "Common Reed"), class = "factor"), latitude = c("30.271", "48.288", "46.118", "36.976", "36.976", "42.416", "36.722"), longitude = c("-87.74", "-122.377", "-98.877", "-104.478", "-104.478", "-90.411", "-75.947")), .Names = c("Species", "latitude", "longitude"), row.names = c(NA, -7L), class = "data.frame")



ui <- fluidPage(
  titlePanel("2011 NWCA Invasive Species"),
  mainPanel(
    leafletOutput("map"),
    br(), br(),
    tableOutput("results")),
  sidebarPanel(
    ### User chooses the species to map
    selectInput("speciesInput", "Species",
                unique(fixInvase$Species))
  ))

server <- function(input, output, session){

  ####
  output$map <- renderLeaflet({
    filtered <-
      fixInvase %>%
      filter(Species== input$speciesInput
      )
    leaflet(filtered) %>% addTiles() %>%
      fitBounds(~min(longitude), ~min(latitude), ~max(longitude), ~max(latitude))%>% 
      addMarkers(~as.numeric(longitude), ~as.numeric(latitude), popup = ~as.character(Species), label = ~as.character(Species))
  })
  output$results <- renderTable({
    filtered <-
      fixInvase %>%
      filter(Species == input$speciesInput
      )
    filtered
  })

}

shinyApp(ui, server)

还添加addMarkers以显示物种的位置