首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用循环在小叶地图上添加来自r shiny中不同数据框的标记

使用循环在小叶地图上添加来自r shiny中不同数据框的标记
EN

Stack Overflow用户
提问于 2018-07-24 09:22:38
回答 1查看 425关注 0票数 1

我使用r shiny中的一个复选框来选择从不同数据集绘制的位置(例如:游乐场、图书馆、公园)。由于经度和经度存在于不同的数据集中,因此我认为可以使用for循环来根据选中的复选框更新基础小叶地图。然而,我在这方面并没有取得太大的成功,因为地图就是不出现。我也没有得到错误。我也附上了代码片段。如果有人能帮我解决这个问题,那将是非常有帮助的。如果有人能提出另一种方法来完成任务,我将不胜感激。

###################ui

代码语言:javascript
运行
复制
 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

代码语言:javascript
运行
复制
 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))


      }

    }
EN

回答 1

Stack Overflow用户

发布于 2021-08-27 18:20:19

这对我很有效:

代码语言:javascript
运行
复制
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

    })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51489169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档