修改R中的Leaflet Popups

时间:2018-01-12 18:47:44

标签: css r leaflet r-leaflet

我想在R。

中修改传单弹出窗口的外观

帮助文件指出...函数中的popupOptions() “传递给底层Javascript对象构造函数的额外选项。”

this example中,style选项设置为修改标记外观的CSS参数列表:

  addMarkers(
    lng = -118.456554, lat = 34.075,
    label = "Label w/ custom CSS style",
    labelOptions = labelOptions(noHide = T, direction = "bottom",
      style = list(
        "color" = "red",
        "font-family" = "serif",
        "font-style" = "italic",
        "box-shadow" = "3px 3px rgba(0,0,0,0.25)",
        "font-size" = "12px",
        "border-color" = "rgba(0,0,0,0.5)"
      )))

然而,相同的方法似乎不适用于Popups,因为这个最小的工作示例演示了:

if (!require("pacman")) install.packages("pacman")
pacman::p_load(leaflet, eurostat, dplyr)

map <- get_eurostat_geospatial() %>% subset(., .$NUTS_ID == "AT11")

leaflet() %>%

  addPolygons(data = map , 
              group = map$NUTS_ID,
              fillColor = "grey",
              weight = 1,
              color = "black") %>%

  addPopups(lng = 16.3, lat = 47, popup = "Paint it black!",
            options = popupOptions(closeButton = FALSE,
                                   opacity = 0.5,
                                   style = list("background" = "black",
                                                "padding" = "2px",
                                                "border-radius" = "0px")))

有些网页使用小册子的javascript版本(例如here)解释了使用CSS自定义标签。关键似乎是编辑.leaflet-popup-tip.leaflet-popup-content-wrapper。但是如何在R(不使用Shiny)中执行此操作?

相关:this问题,但是它只解决弹出窗口中的修改元素而不是弹出窗口本身。

我欢迎任何建议。

1 个答案:

答案 0 :(得分:4)

也许你可以使用包htmltools来获得你想要的东西。

map2 <- leaflet() %>%
    addPolygons(data = map , 
                group = map$NUTS_ID,
                fillColor = "grey",
                weight = 1,
                color = "black") %>%
    addPopups(lng = 16.3, lat = 47, popup = "Paint it black!")

library(htmltools)
browsable(
  tagList(list(
    tags$head(
      tags$style(
        ".leaflet-popup-content-wrapper {
    background: black;
    color: #ffffff;
    padding: 2px;
border-radius: 0px;
    }
        "
      )
    ),
    map2
  ))
)