Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Is it possible to include

  • style elements OR
  • a custom path to a css file

either via htmlwidgets for R or LeafletR?

Best

like image 908
kabr Avatar asked Mar 01 '16 10:03

kabr


People also ask

How do you add a shiny CSS in R?

There are currently three ways you can add CSS to your Shiny apps: Add stylings directly to HTML tags. Add CSS to HTML header. Add stylesheets to the www directory.

What are htmlwidgets R?

The htmlwidgets package provides a framework for creating R bindings to JavaScript libraries. HTML Widgets can be: Used at the R console for data analysis just like conventional R plots. Embedded within R Markdown documents. Incorporated into Shiny web applications.


1 Answers

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.

Easy but Less Robust

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
  ))
)

More Robust with 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
like image 68
timelyportfolio Avatar answered Sep 20 '22 14:09

timelyportfolio