3

I'm using the leaflet library in R, which is a wrapper to the leaflet.js library. I'm wondering if it is possible to add a query or search button using the R interface (or some hack to the underlying code)? There are search plug-ins to the javascript library here http://leafletjs.com/plugins.html#search--popups, but I can't figure out how to get them to work with the javascript that comes out from the R library.

As a minimal example, I want to add to the following the ability to search for "location 1" in the following map, and have it display the popup:

library(leaflet)
df = read.csv(textConnection(
  'Name, Lat, Long
  <b>location 1</b>,42.3401, -71.0589
  <b>location 2</b>,42.3501, -71.0689'))

leaflet(df) %>% 
  addTiles() %>%
  setView(lng=-71.0589,lat=42.3301, zoom=12) %>%
  addMarkers(~Long, ~Lat, popup = ~Name
  )
Devon
  • 650
  • 8
  • 19

3 Answers3

4

A complete working example of adding a search bar using the leafletplugins package is here:

devtools::install_github('byzheng/leaflet')
library(leaflet)
library(leafletplugins)

df = read.csv(textConnection(
  'Name, Lat, Long, Name2
  <b>location 1</b>,42.3401, -71.0589, Loc 1
  <b>location 2</b>,42.3501, -71.0689, Loc 2'))

leaflet(df) %>% 
  addTiles() %>%
  setView(lng=-71.0589,lat=42.3301, zoom=12) %>%
  addMarkers(~Long, ~Lat, popup = ~Name, group = 'marker', label = ~Name2) %>%
  addSearchMarker('marker', position='topleft', propertyName = 'label')
Devon
  • 650
  • 8
  • 19
  • 5
    The functionality is now in the leaflet.extras package. – needRhelp Jan 25 '17 at 10:50
  • 2
    @needRhelp, seems that this project is not really maintained anymore, or at least the landing page doesn't point to a currently maintained fork or another package. Do you happen to know what works nowadays? – Valentin_Ștefan Jan 29 '22 at 14:47
3

It appears that there is a search plugin for the R leaflet package: https://github.com/byzheng/leafletplugins

Xiongbing Jin
  • 11,779
  • 3
  • 47
  • 41
  • This solution worked. Though, note that to get the leafletplugins code to work, you must fork the most recent version of leaflet (not the one through CRAN) using `devtools::install_github('byzheng/leaflet')` – Devon Jun 14 '16 at 10:47
1

Check out the inlmisc package and AddSearchButton

df = read.csv(textConnection(
'Name, Lat, Long
<b>location 1</b>,42.3401, -71.0589
<b>location 2</b>,42.3501, -71.0689'))

map=leaflet(df) %>% 
addTiles() %>%
setView(lng=-71.0589,lat=42.3301, zoom=12) %>%
addMarkers(~Long, ~Lat, popup = ~Name, group="marker")    

map=inlmisc::AddSearchButton(map, group = "marker", zoom = 15,
                            textPlaceholder = "Search here")
W148SMH
  • 152
  • 1
  • 11