1
here is my R code
library(leaflet)

m <- leaflet() %>% 
  addTiles() %>% 
  setView(lng = 126.97806, lat=37.56667, zoom=16)
m

acci <- read.csv("C:/accidents.csv")
acci


leaflet(acci) %>% 
  setView(lng = 126.97806, lat=37.56667, zoom=13) %>%
  addTiles() %>% 
  addCircles(lng=~longitude, lat=~latitude, color=~acci_colour(accidenttype), popup=~accidentplace) %>% 
  addLegend(position = "bottomleft",
            title = "accidenttype",
            pal = acci_colour, values = ~accidenttype, opacity = 1)



acci_colour <- colorFactor("viridis", acci$accidenttype)

SO, I want to know how to get multiple informations of data acci when i click the circle mark on the leaflet map.

I tried : addCircles(lng=~longitude, lat=~latitude, color=~acci_colour(accidenttype), popup=~accidentplace, ~...., ~.....)

addCircles(lng=~longitude, lat=~latitude, color=~acci_colour(accidenttype),popup=paste(acci$accidentplace, acci$..., acci$...)

addCircles(lng=~longitude, lat=~latitude, color=~acci_colour(accidenttype), popup=colnames(acci)[5:9])

... Thank you

1 Answer 1

0

You only need to use ~ once and paste the column data together using html for formatting.

For example:

Data for reprex

library(leaflet)

df <- data.frame(
    lat = runif(10, 35, 40),
    lon = runif(10, 80, 120),
    n = 1:10,
    txt1 = sample(LETTERS, 10),
    txt2 = sample(letters, 10)
)

Example 1

leaflet(df) %>%
    addTiles() %>%
    addCircles(
        lng = ~lon,
        lat = ~lat,
        popup = ~paste(n, txt1, txt2, sep = "<br>")
    )

enter image description here

Example 2 (more control)

library(htmltools)

leaflet(df) %>%
    addTiles() %>%
    addCircles(
        lng = ~lon,
        lat = ~lat,
        popup = ~paste0(
            "<b>n: ", n, "</b><br>",
            "id1: ", txt1, "<br>",
            "id2: ", txt2, "<br>"
        )
    )

enter image description here

Using htmltools::htmlEscape() ensures the column text isn't interpreted as html. It's not strictly necessary for this example.

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