从 json 文件中过滤掉区域
Filter out areas from json file
我是 R Shiny 的新手,我想在我的应用程序中添加一个功能,我可以在其中选择城市内社区的下拉菜单。默认 selection 是整个城市本身 SURREY
。预计当我从 SURREY
以外的下拉菜单中 select 一个社区时,我希望它过滤掉其他社区,因此只显示 selected 社区的轮廓.但是,当我尝试这样做时,我的地图完全停止渲染。 JSON 文件是 here,我提取它并将其放在我的相对 data
路径中。我的完整代码如下:
library(shiny)
library(shinydashboard)
library(shinyjs)
library(leaflet)
library(leaflet.extras)
library(sf)
library(ggplot2)
library(dplyr)
library(lubridate)
library(rgdal)
##constants
SURREY_LAT <- 49.15
SURREY_LNG <- -122.8
ZOOM_MIN = 10
ZOOM_MAX = 18
##
neighbourhood <- st_read("data/surrey_city_boundary.json", quiet = TRUE) %>%
st_transform(crs = 4326)%>%
select(NAME,geometry)
neighbourhood_names <- neighbourhood$NAME %>%
as.character(.) %>%
sort()
##basemap
basemap<-leaflet() %>%
addProviderTiles(providers$CartoDB.Positron)%>%
setView(lng = SURREY_LNG,lat =SURREY_LAT, zoom= 10)
ui <- bootstrapPage(
tags$style(type = "text/css", "html,
body {width:100%;height:100%}"),
leafletOutput("basemap", width = "100%", height = "100%"),
absolutePanel(id = "controls", class = "panel panel-default",
top = 60, left = 55, width = 250, fixed = TRUE,
draggable = TRUE, height = "auto",
selectInput(
"neighbourhood_names",
label = "Select a Neighbourhood:",
choices=(neighbourhood_names),
selected= "SURREY")
)
)
#server
server <- function(input, output) {
output$mymap <- renderLeaflet(basemap)
observeEvent(input$neighbourhood_names,{
if(input$neighbourhood_names =="SURREY"){
data<- neighbourhood %>%
filter(NAME %in% c("CITY CENTRE", "CLOVERDALE", "FLEETWOOD", "GUILDFORD",
"NEWTON", "SOUTH SURREY", "WHALLEY"))}
else{
data <- neighbourhood %>%
filter(NAME == input$neighbourhood_names)}
leafletProxy("mymap", data= neighbourhood) %>%
setView(lng = ifelse(input$neighbourhood_names == "Surrey", -122.7953, 49.15),
lat = ifelse(input$neighbourhood_names == "Surrey", 49.10714, 49.15),
zoom = ifelse(input$neighbourhood_names == "Surrey", 11, 12)) %>%
clearShapes() %>%
addPolygons(color = "#141722", weight = 3, smoothFactor = 0.5,
fillOpacity = 0.1, opacity = 1)
})
}
shinyApp(ui = ui, server = server)
你有一些错别字。此外,您需要将 lng
和 lat
放入数据框中,以便每个选择都有自己的坐标。目前,您在 setView
中将其定义为 49.15。修复数据框后,这应该可以工作。
SURREY_LAT <- 49.15
SURREY_LNG <- -122.8
ZOOM_MIN = 10
ZOOM_MAX = 18
##
neighbourhood <- st_read("./surrey_city_boundary.json", quiet = TRUE) %>%
st_transform(crs = 4326) %>%
select(NAME,geometry)
neighbourhood_names <- neighbourhood$NAME %>%
as.character(.) %>%
sort()
##basemap
basemap<-leaflet() %>%
addProviderTiles(providers$CartoDB.Positron)%>%
setView(lng = SURREY_LNG,lat =SURREY_LAT, zoom= 10)
ui <- bootstrapPage(
tags$style(type = "text/css", "html,
body {width:100%;height:100%}"),
leafletOutput("mymap", width = "100%", height = "100%"),
absolutePanel(id = "controls", class = "panel panel-default",
top = 60, left = 55, width = 250, fixed = TRUE,
draggable = FALSE, height = "auto",
shiny::selectInput(
"neighbourhood_names",
label = "Select a Neighbourhood:",
choices=(neighbourhood_names),
selected= "SURREY"
)
)
)
#server
server <- function(input, output) {
output$mymap <- renderLeaflet(basemap)
observeEvent(input$neighbourhood_names,{
req(input$neighbourhood_names)
if(input$neighbourhood_names =="SURREY"){
data<- neighbourhood %>%
dplyr::filter(NAME %in% c("CITY CENTRE", "CLOVERDALE", "FLEETWOOD", "GUILDFORD",
"NEWTON", "SOUTH SURREY", "WHALLEY"))
} else{
data <- neighbourhood %>%
dplyr::filter(NAME == input$neighbourhood_names)}
leafletProxy("mymap", data= data) %>%
setView(lng = ifelse(input$neighbourhood_names == "SURREY", -122.7953, 49.15),
lat = ifelse(input$neighbourhood_names == "SURREY", 49.10714, 49.15),
zoom = ifelse(input$neighbourhood_names == "SURREY", 11, 12)) %>%
clearShapes() %>%
addPolygons(color = "#141722", weight = 3, smoothFactor = 0.5,
fillOpacity = 0.1, opacity = 1)
})
}
shinyApp(ui = ui, server = server)
我是 R Shiny 的新手,我想在我的应用程序中添加一个功能,我可以在其中选择城市内社区的下拉菜单。默认 selection 是整个城市本身 SURREY
。预计当我从 SURREY
以外的下拉菜单中 select 一个社区时,我希望它过滤掉其他社区,因此只显示 selected 社区的轮廓.但是,当我尝试这样做时,我的地图完全停止渲染。 JSON 文件是 here,我提取它并将其放在我的相对 data
路径中。我的完整代码如下:
library(shiny)
library(shinydashboard)
library(shinyjs)
library(leaflet)
library(leaflet.extras)
library(sf)
library(ggplot2)
library(dplyr)
library(lubridate)
library(rgdal)
##constants
SURREY_LAT <- 49.15
SURREY_LNG <- -122.8
ZOOM_MIN = 10
ZOOM_MAX = 18
##
neighbourhood <- st_read("data/surrey_city_boundary.json", quiet = TRUE) %>%
st_transform(crs = 4326)%>%
select(NAME,geometry)
neighbourhood_names <- neighbourhood$NAME %>%
as.character(.) %>%
sort()
##basemap
basemap<-leaflet() %>%
addProviderTiles(providers$CartoDB.Positron)%>%
setView(lng = SURREY_LNG,lat =SURREY_LAT, zoom= 10)
ui <- bootstrapPage(
tags$style(type = "text/css", "html,
body {width:100%;height:100%}"),
leafletOutput("basemap", width = "100%", height = "100%"),
absolutePanel(id = "controls", class = "panel panel-default",
top = 60, left = 55, width = 250, fixed = TRUE,
draggable = TRUE, height = "auto",
selectInput(
"neighbourhood_names",
label = "Select a Neighbourhood:",
choices=(neighbourhood_names),
selected= "SURREY")
)
)
#server
server <- function(input, output) {
output$mymap <- renderLeaflet(basemap)
observeEvent(input$neighbourhood_names,{
if(input$neighbourhood_names =="SURREY"){
data<- neighbourhood %>%
filter(NAME %in% c("CITY CENTRE", "CLOVERDALE", "FLEETWOOD", "GUILDFORD",
"NEWTON", "SOUTH SURREY", "WHALLEY"))}
else{
data <- neighbourhood %>%
filter(NAME == input$neighbourhood_names)}
leafletProxy("mymap", data= neighbourhood) %>%
setView(lng = ifelse(input$neighbourhood_names == "Surrey", -122.7953, 49.15),
lat = ifelse(input$neighbourhood_names == "Surrey", 49.10714, 49.15),
zoom = ifelse(input$neighbourhood_names == "Surrey", 11, 12)) %>%
clearShapes() %>%
addPolygons(color = "#141722", weight = 3, smoothFactor = 0.5,
fillOpacity = 0.1, opacity = 1)
})
}
shinyApp(ui = ui, server = server)
你有一些错别字。此外,您需要将 lng
和 lat
放入数据框中,以便每个选择都有自己的坐标。目前,您在 setView
中将其定义为 49.15。修复数据框后,这应该可以工作。
SURREY_LAT <- 49.15
SURREY_LNG <- -122.8
ZOOM_MIN = 10
ZOOM_MAX = 18
##
neighbourhood <- st_read("./surrey_city_boundary.json", quiet = TRUE) %>%
st_transform(crs = 4326) %>%
select(NAME,geometry)
neighbourhood_names <- neighbourhood$NAME %>%
as.character(.) %>%
sort()
##basemap
basemap<-leaflet() %>%
addProviderTiles(providers$CartoDB.Positron)%>%
setView(lng = SURREY_LNG,lat =SURREY_LAT, zoom= 10)
ui <- bootstrapPage(
tags$style(type = "text/css", "html,
body {width:100%;height:100%}"),
leafletOutput("mymap", width = "100%", height = "100%"),
absolutePanel(id = "controls", class = "panel panel-default",
top = 60, left = 55, width = 250, fixed = TRUE,
draggable = FALSE, height = "auto",
shiny::selectInput(
"neighbourhood_names",
label = "Select a Neighbourhood:",
choices=(neighbourhood_names),
selected= "SURREY"
)
)
)
#server
server <- function(input, output) {
output$mymap <- renderLeaflet(basemap)
observeEvent(input$neighbourhood_names,{
req(input$neighbourhood_names)
if(input$neighbourhood_names =="SURREY"){
data<- neighbourhood %>%
dplyr::filter(NAME %in% c("CITY CENTRE", "CLOVERDALE", "FLEETWOOD", "GUILDFORD",
"NEWTON", "SOUTH SURREY", "WHALLEY"))
} else{
data <- neighbourhood %>%
dplyr::filter(NAME == input$neighbourhood_names)}
leafletProxy("mymap", data= data) %>%
setView(lng = ifelse(input$neighbourhood_names == "SURREY", -122.7953, 49.15),
lat = ifelse(input$neighbourhood_names == "SURREY", 49.10714, 49.15),
zoom = ifelse(input$neighbourhood_names == "SURREY", 11, 12)) %>%
clearShapes() %>%
addPolygons(color = "#141722", weight = 3, smoothFactor = 0.5,
fillOpacity = 0.1, opacity = 1)
})
}
shinyApp(ui = ui, server = server)