12

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

1 Answer 1

38

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))
3
  • 1
    hey i have the similar problem again. but this time my data set is: Country, location, CountOfHit. Some countries have multiple count of hit but when i try to plot it only plots that one location.my code is- m <- leaflet() %>% addTiles() %>% # Add default OpenStreetMap map tiles addCircles(lng=area$longitude, lat=area$latitude, popup=paste("Country:", area$Country, "<br>", "Location:", Location, "-", area$CountOfVis, "<br>" , "Location:", area$Location, "-", area$CountOfVis)) Sep 11, 2015 at 11:41
  • @FaizSaiyed, please make it as a new question.
    – djhurio
    Sep 11, 2015 at 14:21
  • i have here is the link stackoverflow.com/questions/32523314/… Sep 11, 2015 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.