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

kbr*_*ner 5 r leaflet htmlwidgets

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

是否可以包括

  • 风格元素或
  • css文件的自定义路径

通过htmlwidgets为R或LeafletR?

最好

tim*_*lio 9

在您的问题中没有任何代码,回答非常困难.我会尝试一个答案.有添加自定义的两个方法CSShtmlwidget.我会提醒前面,虽然你将需要非常具体,或使用!important替代,因为已经有相当多的CSS被自动添加到leaflet.

容易但不那么健壮

htmlwidgets可以结合tagshtmltools包.

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
  ))
)
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)