是否可以在 R and/or LeafletR 的 htmlwidgets 中包含自定义 css?

Is it possible to include custom css in htmlwidgets for R and/or LeafletR?

我想对我的传单地图进行一些样式更改。

是否可以包括

通过 R 的 htmlwidgets 还是 LeafletR?

最佳

由于您的问题中没有任何代码,因此很难回答。我会尝试回答。有两种方法可以将自定义 CSS 添加到 htmlwidget。我会提前提醒您 需要非常具体 或使用 !important 覆盖,因为已经有相当多的 bit of CSS 自动添加到leaflet.

简单但不够健壮

htmlwidgets 可以与 htmltools 包中的 tags 组合。

library(leaflet)
library(htmltools)

# example from ?leaflet
m = leaflet() %>% addTiles()

# there are two approaches to the custom css problem
#  1.  the easy but less robust way
browsable(
  tagList(list(
    tags$head(
      # you'll need to be very specific
      tags$style("p{font-size:200%;}")
      # could also use url
      #tags$link(href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css",rel="stylesheet")
    ),
    m
  ))
)

htmlDependency 更健壮

您还可以使用 htmlDependency 来处理由重复引起的冲突。

#  2.  you can add dependencies to your leaflet map
#  this mechanism will smartly handle duplicates
#  but carries a little more overhead
str(m$dependencies)  # should be null to start
# 
m$dependencies <- list(
  htmlDependency(
    name = "font-awesome"
    ,version = "4.3.0"
    # if local file use file instead of href below
    #  with an absolute path
    ,src = c(href="http://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css")
    ,stylesheet = "font-awesome.min.css"
  )
)

m