【问题标题】:Use a loop to add markers on leaflet map from different data frames in r shiny使用循环在 r shiny 中的不同数据帧的传单地图上添加标记
【发布时间】:2023-03-09 13:27:01
【问题描述】:

我在 r shiny 中使用一个复选框来选择从不同数据集中绘制的位置(例如:游乐场、图书馆、公园)。由于 lat 和 long 存在于不同的数据集中,我想我可以使用 for 循环根据选中的复选框更新基本传单地图。但是,我在这方面并没有取得太大的成功,因为地图没有出现。我也没有错误。我也附上了代码 sn-ps。如果有人可以帮助我,那将非常有帮助。如果有人能建议另一种方法来完成工作,我将不胜感激。

###################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")
                            )
                          ))
                        )
                        ) 
###########服务器
 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))


      }

    }

【问题讨论】:

  • 考虑更详细地解释您的问题陈述
  • 我有 4 个不同的数据集,其中包含图书馆、公园、游乐场和博物馆的位置。我想要一个复选框,使我能够在传单地图上绘制这些资源的一个或任意组合的位置。复选框中的输入变量是数据集的名称。我创建了一个循环,我希望根据复选框中选择的输入数据集(代码的最后几行)更新地图。但是,我没有在应用程序中看到地图。我也没有收到错误。是否有其他方法可以循环更新传单?
  • 或者有没有其他方法可以解决这个问题?
  • 您应该提供一个可重现的示例,以便人们可以测试您的代码。

标签: r shiny r-leaflet


【解决方案1】:

这对我有用:

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

    })

【讨论】:

    猜你喜欢
    • 2021-11-30
    • 2019-11-12
    • 1970-01-01
    • 2017-10-27
    • 2016-10-14
    • 2017-07-29
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    相关资源
    最近更新 更多