是否可以在R和/或LeafletR的htmlwidget中包含自定义css? [英] Is it possible to include custom css in htmlwidgets for R and/or LeafletR?

查看:74
本文介绍了是否可以在R和/或LeafletR的htmlwidget中包含自定义css?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to make some style changes to my leaflet map.

是否可以包含

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

是通过htmlwidgets for R还是LeafletR?

either via htmlwidgets for R or LeafletR?

最佳

推荐答案

问题中没有任何代码,回答非常困难.我会尝试一个答案.有两种将自定义CSS添加到htmlwidget的方法.我会在事前警告您,尽管您将必须非常具体或使用!important替代,因为已经有一个

With no code whatsoever in your question, answering is very difficult. I'll attempt an answer. There are two methods of adding custom CSS to an htmlwidget. I will caution up front though that you will need to be very specific or use the !important override, since there is already quite a bit of CSS that is automatically added to leaflet.

简单但不那么强大

htmlwidgets可以与htmltools软件包中的tags组合.

htmlwidgets can be combined with tags from the htmltools package.

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来处理由重复项引起的冲突.

You can also use the htmlDependency which will handle conflicts caused by duplicates.

#  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

这篇关于是否可以在R和/或LeafletR的htmlwidget中包含自定义css?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆