我使用r shiny中的一个复选框来选择从不同数据集绘制的位置(例如:游乐场、图书馆、公园)。由于经度和经度存在于不同的数据集中,因此我认为可以使用for循环来根据选中的复选框更新基础小叶地图。然而,我在这方面并没有取得太大的成功,因为地图就是不出现。我也没有得到错误。我也附上了代码片段。如果有人能帮我解决这个问题,那将是非常有帮助的。如果有人能提出另一种方法来完成任务,我将不胜感激。
###################ui
tabPanel("Other out-of-school resources",
fluidPage(sidebarLayout(
sidebarPanel(
selectInput("neighborhoods_other", "Select the neighborhood from the dataset", choices = c("No neighborhood selected", neighborhoods_other)),
br(),
checkboxGroupInput("program_other", "Select the type of the program", choices = c("Parks", "Playgrounds", "Rec Centers", "Libraries",
"Museums", "Fields"), selected = "Parks"),
br(),
radioButtons("demographics_other", "Select the demographics variable", choices = c("Median household income ($)","High school degree or equivalent(%)",
"Hispanic population (%)", "Non native English speakers (%)"), selected = character(0)),
br()
),
mainPanel(
tabsetPanel(type = "tab",
tabPanel("Map",
leafletOutput("mymap_other", height = 650)),
tabPanel("Data",
DT::dataTableOutput("datatable_other")),
tabPanel("Summary analysis")
)
))
)
) ###########server
colm_other = reactive({
input$program_other
})
parks_data = reactive({
if(input$neighborhoods_other != "No neighborhood selected" ) {
a = parks[which(parks[, "nbhd_name"] == input$neighborhoods_other),]
}
else {
a = parks
}
return(a)
})
libraries_data = reactive({
if(input$neighborhoods_other != "No neighborhood selected" ) {
a = libraries[which(libraries[, "nbhd_name"] == input$neighborhoods_other),]
}
else {
a = libraries
}
return(a)
})
output$mymap_other = renderLeaflet({
parks_data1 = parks_data()
libraries_data1 = libraries_data()
m = leaflet() %>% setView(lng = -104.991531, lat = 39.742043,zoom = 10) %>% addTiles()
for (col in colm_other()){
if(col == "Parks"){
print(head(parks_data1))
map <- m %>% addMarkers(map = m, lng = jitter(parks_data1$long), lat = jitter(parks_data1$lat))
}
if(col == "Libraries"){
print(col)
map %>% addMarkers(map, lng = jitter(libraries_data1$long), lat = jitter(libraries_data1$lat))
}
}发布于 2021-08-27 18:20:19
这对我很有效:
output$mymap <- renderLeaflet({
# create map
themap<- leaflet(data = dataset
,options = leafletOptions(preferCanvas = TRUE) ) %>%
addTiles(options = providerTileOptions(
updateWhenZooming = FALSE,
updateWhenIdle = FALSE)
) %>% setView(lng = median(dataset$lng)
,lat = median(dataset$lat)
,zoom = 9
)
# add a number of layes to the map in a for loop
for (service in unique(dataset$service_type) ){
themap<-addCircleMarkers(map=themap
,data = dataset[which(dataset$service_type %in% service),]
,~lng
,~lat
,clusterOptions = markerClusterOptions(
iconCreateFunction= JSfunction
,spiderfyOnMaxZoom = TRUE
)
, fillColor = ~pal(service_type)
, stroke = FALSE
, fillOpacity = 0.7
) # aCM
} # for
themap
})https://stackoverflow.com/questions/51489169
复制相似问题