Nicolabo
Nicolabo

Reputation: 1377

Add r chunk within output of html_document - css files

I would like to add css files into my Rmd using r chunk. For example

---
title: "Untitled"
output: 
    html_document:
        css: '`r system.file("shinydashboard.css" , package = "shinydashboard")`'
---

but I got error:

pandoc: Could not fetch `r system.file(
`r system.file(: openBinaryFile: does not exist (No such file or directory)
Error: pandoc document conversion failed with error 67
Execution halted

I read both related questions (this and this), but they did not cover situation when r chunk occurs in html_document output (Ramnathv mentioned brew package but I did not test it, besides it was posted in '14.).

It would be great if it would be possible to do it, and still using Knit button in RStudio.

Thanks,

Update

Another thing is when I want to include all .css files from one directory using r chunk within html_document. Here I want to include all files from www directory:

---
title: "Untitled"
output: 
    html_document:
        css: "`r x <- list.files(path = 'www', pattern = '*.css', full.names = T); x <- paste(x, collapse = '\', \'');z <- paste0('[ \'', paste0(x, collapse = ', '), '\' ]'); z`"
---

Upvotes: 1

Views: 649

Answers (1)

Kevin Arseneau
Kevin Arseneau

Reputation: 6264

The expression in the YAML header must be formatted using !expr (see note at the bottom).

---
title: "Untitled"
output:
  html_document:
    css: !expr system.file("shinydashboard.css" , package = "shinydashboard")
---


## Header

```{r meta}
rmarkdown::metadata
```

This produces the following output where you can clearly see the CSS loaded correctly.

enter image description here

To understand what is happening during the render, it is valuable to look at the pandoc command being run. For your multi-file update, we can run static and dynamic for a comparison.

---
title: "Untitled"
output:
  html_document:
    css: [ 'www/file1.css', 'www/file2.css' ]
---

# /usr/lib/rstudio-server/bin/pandoc/pandoc +RTS -K512m -RTS so.utf8.md --to html4 --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output so.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template /usr/share/R/library/rmarkdown/rmd/h/default.html --no-highlight --variable highlightjs=1 --css www/file1.css --css www/file2.css --variable 'theme:bootstrap' --include-in-header /tmp/RtmpRdKgYW/rmarkdown-str3eef4edabf99.html --mathjax --variable 'mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'     

Importantly, you will see --css www/file1.css --css www/file2.css, and the file renders correctly with both CSS files included.

---
title: "Untitled"
output:
  html_document:
    css: !expr x <- list.files(path = "www", pattern = "*.css", full.names = T); x <- paste(x, collapse = "\', \'"); z <- paste0("[ \'", paste0(x, collapse = ", "), "\' ]"); z
---

# /usr/lib/rstudio-server/bin/pandoc/pandoc +RTS -K512m -RTS so.utf8.md --to html4 --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output so.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template /usr/share/R/library/rmarkdown/rmd/h/default.html --no-highlight --variable highlightjs=1 --css "[ 'www/file1.css', 'www/file2.css' ]" --variable "theme:bootstrap" --include-in-header /tmp/RtmpMStG00/rmarkdown-str423c7a02dab7.html --mathjax --variable "mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"

Where we unfortunately find --css "[ 'www/file1.css', 'www/file2.css' ]". I have tried a few tricks including: cat, capture.output, as.name, and noquote without luck. An enhancement may be required to rmarkdown to present this in the required form to pandoc.

N.B. It is worth noting this is indeed a duplicate of this question where the answer by Eli Holmes demonstrates the alternate syntax. He also notes a change in the development version, which may include the css field.

Upvotes: 3

Related Questions