Skip to content

Instantly share code, notes, and snippets.

@yihui
Created March 31, 2015 02:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yihui/811f68229400a07c3cc7 to your computer and use it in GitHub Desktop.
Save yihui/811f68229400a07c3cc7 to your computer and use it in GitHub Desktop.
A minimal example of HTML dependencies
---
title: HTML Dependencies
output: html_document
---
This example explains how HTML dependencies work in **htmltools**. Sometimes an HTML fragment may have additional dependencies to work correctly, such as JavaScript and/or CSS files. In R Markdown documents, you have to let **rmarkdown** know these dependencies, so they can be added to the HTML header when the document is rendered through Pandoc.
Another thing that you need to pay attention is you have to "protect" your HTML output so that Pandoc does not touch it, e.g. when you have four leading spaces, Pandoc may think this line is supposed to be a `<pre>` block whereas you only meant to indent the line for cosmetic purposes. In this case, the function `htmltools::htmlPreserve()` will be _automatically_ applied to your HTML content in R Markdown if the content is generated from `htmltools::tags` or wrapped in `htmltools::HTML()`.
Now we use a random CSS file in the **knitr** package to illustrate how dependencies work. The goal here is to generate a `<div>` with a class `error`, of which the style has been defined in the CSS file.
```{r}
htmlFun = function(content) {
x = htmltools::tags$div(content, class = 'error')
# you could also do this if you don't care about escaping HTML entities in 'content':
# x = htmltools::HTML(paste('<div class="error">', content, '</div>'))
d = htmltools::htmlDependency(
'knitr-css', '1.2.3', src = system.file('misc', package = 'knitr'),
stylesheet = 'knitr.css'
)
x = htmltools::attachDependencies(x, d)
x
}
```
Now you should see `Hello World` in red:
```{r}
htmlFun('Hello World!')
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment