Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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 如何使用selectInput映射数据帧的子集_R_Shiny_Leaflet_Gis - Fatal编程技术网

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

R 如何使用selectInput映射数据帧的子集,r,shiny,leaflet,gis,R,Shiny,Leaflet,Gis,我有一个包含以下列的df:物种|纬度|经度。我想创建一个应用程序,允许用户使用selectInput选择一个物种,并绘制该物种的long/lat信息 library(shiny) library(leaflet) fixInvase <- read.csv("fixed2011_buff_invasives.csv") ### Subsetted data that I would like to map bTrefoil <- subset(fixInvase, S

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

    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子集,并让传单仅映射这些点

在运行应用程序之前,不知道为什么要创建子集

我只是下一次构建一个示例df,请提供一个带有dput的示例


还添加了添加标记以显示物种的发现位置

感谢您的反馈。使用unique当然会使这更简单。我对代码的一个问题是,当我尝试运行它时,我收到一条错误消息,警告:过滤器中出错:找不到对象“物种”。我不明白为什么会发生这种情况。上面的代码运行时没有任何警告?您是否使用我的示例dataframe fixInvase进行测试?是的,我已尝试使用您的示例dataframe以及我的原始示例dataframe。应用程序将显示正确的侧边栏面板,但不会显示地图,只会显示警告。谢谢,过滤器是否需要dplyr包才能工作?谢谢你的帮助,一切都好了!是的,它是用来过滤的
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)