我想知道如何使用传单包在R中打印addcirclespopup = ...的多列

问题描述

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,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)

所以,我想知道当我单击传单地图上的圆圈标记时如何获取数据访问的多个信息。

我尝试过: addCircles(lng =〜经度,lat =〜纬度,color =〜acci_colour(accidenttype),popup =〜accidentplace,〜....,〜.....)

addCircles(lng =〜经度,lat =〜纬度,color =〜acci_colour(accidenttype),popup = paste(acci $ accidentplace,acci $ ...,acci $ ...)

addCircles(lng =〜经度,lat =〜纬度,color =〜acci_colour(accidenttype),popup = colnames(acci)[5:9])

...谢谢

解决方法

您只需要使用 ~ 一次,然后使用 html 将列数据粘贴在一起进行格式化。

例如:

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)
)

示例 1

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

enter image description here

示例 2(更多控制)

library(htmltools)

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

enter image description here

使用 htmltools::htmlEscape() 可确保不会将列文本解释为 html。对于这个例子来说,这不是绝对必要的。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...