【问题标题】:Is it possible to include custom css in htmlwidgets for R and/or LeafletR?是否可以在 R 和/或 LeafletR 的 htmlwidgets 中包含自定义 css?
【发布时间】:2016-06-13 17:46:08
【问题描述】:

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

是否可以包含

  • 样式元素或
  • css 文件的自定义路径

是通过 R 的 htmlwidgets 还是 LeafletR?

最好的

【问题讨论】:

  • 您是否有一些可重现的代码或自定义传单样式的示例?我想我有一个答案,但我不知道。 htmltools 在这里肯定会成为您的朋友,但我们可以使用依赖项探索其他一些选项。
  • 你是否也在使用rmarkdown
  • 不,我不使用 rmarkdown。我想在独立网站上使用它。

标签: r leaflet htmlwidgets


【解决方案1】:

您的问题中没有任何代码,因此很难回答。我会尝试回答。将自定义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

【讨论】:

  • 谢谢。正是我需要的。
猜你喜欢
  • 2015-07-04
  • 1970-01-01
  • 2021-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-19
  • 1970-01-01
相关资源
最近更新 更多