Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using leaflet library to output multiple popup values

Tags:

r

rstudio

i am using some data to produce a map with markers in certain places and using the popup function to output multiple values.

The data i am getting is from sql, following is the code for that:

con <- odbcConnect("data", uid = "user", pwd = "password")
area <- sqlQuery(con, "EXEC sp")

The area variable contains 11 rows and 6 column. The 6 columns are: Region (text), Employed(integer), Retired(integer), employed(integer), Longitude (integer)and Latitude(integer).

I am using the longitude and the latitude to make the points on map using the leaflet library. The others i want to be displayed as popup for when the user clicks on the respected points on the map.

The code for how i produced the map and points on them with the popup for each point.

m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=area$Longitude, lat=area$Latitude, popup=area$Region)

The above function works and shows me the popup output with the correct region name on the map. but if i try to change the popup to multiple columns, the map is shown with the points but when i click it doesnt respond to me.

Can someone please tell me how i can output multiple values in the popup.

something like this:

m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=area$Longitude, lat=area$Latitude, popup=c(area$Region, area$Employed, area$Retired, area$Unemployed))

Note: I want the popup to be shown as the following when i click on the point:

Region Name
Employed: 559
Retired: 400
Unemployed: 300

Please ak question if you dont understand what i mean as i am still grasping the knowledge of R.

I am doing this on rstudio

like image 725
Faiz Saiyed Avatar asked Jul 22 '15 11:07

Faiz Saiyed


1 Answers

You should use paste() instead of c(). You can use HTML line breaks to get multiple lines. Try this untested code:

m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng = area$Longitude, lat = area$Latitude,
             popup = paste("Region", area$Region, "<br>",
                           "Employed:", area$Employed, "<br>",
                           "Retired:", area$Retired, "<br>",
                           "Unemployed:", area$Unemployed))
like image 141
djhurio Avatar answered Nov 14 '22 10:11

djhurio