Correctly Specifying Vectors in R

I have this map in leaflet/r:

library(leaflet)
library(leaflet.extras)
library(dplyr)

# using the same reproducible data from the question/example
cities <- na.omit(read.csv(
    textConnection("City,Lat,Long,Pop, term1, term2
                    Boston,42.3601,-71.0589,645966, AAA, BBB
                    Hartford,41.7627,-72.6743,125017, CCC, DDD
                    New York City,40.7127,-74.0059,8406000, EEE, FFF
                    Philadelphia,39.9500,-75.1667,1553000, GGG, HHH
                    Pittsburgh,40.4397,-79.9764,305841, III, JJJ
                    Providence,41.8236,-71.4222,177994, JJJ, LLL
                    ")))

# leaf-green.png
#https://leafletjs.com/examples/custom-icons/leaf-green.png


leaflet(cities) %>%
    addProviderTiles(providers$OpenStreetMap) %>%
    addMarkers( clusterOptions = markerClusterOptions()) %>%
                    addResetMapButton() %>%
                    # these markers will be "invisible" on the map:
                    addMarkers(
                        data = cities, lng = ~Long, lat = ~Lat, label = cities$City,
                        group = 'cities', # this is the group to use in addSearchFeatures()
                        # make custom icon that is so small you can't see it:
                        icon = makeIcon(
                            iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-green.png",
                            iconWidth = 1, iconHeight = 1
                        )
                    ) %>%
                    addSearchFeatures(
                        targetGroups = 'cities', # group should match addMarkers() group
                        options = searchFeaturesOptions(
                            zoom=12, openPopup = TRUE, firstTipSubmit = TRUE,
                            autoCollapse = TRUE, hideMarkerOnCollapse = TRUE
                        )
                    )

Using this map, I am able to "search" for a city using the search bar:

enter image description here

I would like to modify this code so that I can search based on "city", "term1" or "term2".

I tried this code over here:

leaflet(cities) %>%
    addProviderTiles(providers$OpenStreetMap) %>%
    addMarkers( clusterOptions = markerClusterOptions()) %>%
                    addResetMapButton() %>%
                    # these markers will be "invisible" on the map:
                    addMarkers(
                        data = cities, lng = ~Long, lat = ~Lat, label = cities$City,
                        group = 'cities', # this is the group to use in addSearchFeatures()
                        # make custom icon that is so small you can't see it:
                        icon = makeIcon(
                            iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-green.png",
                            iconWidth = 1, iconHeight = 1
                        )
                    ) %>%
                    addSearchFeatures(
                        targetGroups = c('cities', 'term1', 'term2'), # group should match addMarkers() group
                        options = searchFeaturesOptions(
                            zoom=12, openPopup = TRUE, firstTipSubmit = TRUE,
                            autoCollapse = TRUE, hideMarkerOnCollapse = TRUE
                        )
                    )

This code runs without error, but I can not search using "term1" or "term2":

enter image description here

According to the documentation (addSearchFeatures function - RDocumentation), "addSearchFeatures" should accept a "vector of group names of groups whose features need to be searched". I was under the impression that vectors in R are specified using c('arg1', 'arg2', 'arg3') - but apparently in this function, this is not the case?

  • Could someone please show me how to fix this?

Thank you!

Hello,
Could you please let us know what is the error you get when you use group = c('cities', 'term1', 'term2') instead of group = 'cities' in your code?

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.