R Leaflet:添加多边形时传递popupOptions。

时间:2016-06-14 19:05:59

标签: r leaflet

addPolygons()内,popup参数就像addPopups()函数中的参数一样。差异(我认为)是当在addPolygons()内创建弹出窗口时,可以单击多边形内的任何位置来触发弹出窗口,但如果使用addPopups(),则单个lng和必须定义lat点。

我想更改maxWidth中的一个默认选项(popupOptions()),这可以在使用addPopups()时轻松完成,因为它包含参数options = popupOptions()但我不喜欢使用addPolygons()时不知道该怎么做;在该函数中,options参数为options = pathOptions()

以下是传单文档中的一个可重现的示例,其中添加了一个弹出窗口,我想增加maxWidth

library(rgdal)

# From https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html
states <- readOGR("shp/cb_2013_us_state_20m.shp",
  layer = "cb_2013_us_state_20m", verbose = FALSE)

neStates <- subset(states, states$STUSPS %in% c(
  "CT","ME","MA","NH","RI","VT","NY","NJ","PA"
))

leaflet(neStates) %>%
  addPolygons(
    stroke = FALSE, fillOpacity = 0.5, smoothFactor = 0.5,
    color = ~colorQuantile("YlOrRd", states$AWATER)(AWATER),
    popup="<b>Hello World</b>"
  )

enter image description here

1 个答案:

答案 0 :(得分:2)

您只需在popupOptions()块中弹出后添加addPloygons(),就可以这样做:

leaflet(neStates) %>%
    addPolygons(
        stroke = FALSE, fillOpacity = 0.5, smoothFactor = 0.5,
        color = ~colorQuantile("YlOrRd", states$AWATER)(AWATER),
        popup="<b>Hello World</b>"
        popupOptions = popupOptions(maxWidth ="100%", closeOnClick = TRUE)

以下是您可以放在popupOptions()列表中的所有内容的小册子R上的PDF小插图列表:

popupOptions(maxWidth = 300, minWidth = 50, maxHeight = NULL,
            autoPan = TRUE, keepInView = FALSE, closeButton = TRUE,
             zoomAnimation = TRUE, closeOnClick = NULL, className = "", ...)