Laura
Laura

Reputation: 483

In RMarkdown how to source css file in a chunk?

I need to know how to source a CSS file to be applied to my .Rmd report, using a chunk in RMarkdown? Is it possible?

Actually, I would like to make .css file become a parameter.

Something like this below:

---
title: "Untitled"
output: 
  html_document
---

```{css, echo=FALSE}
source('style.css')

```

Upvotes: 2

Views: 587

Answers (2)

Marcus
Marcus

Reputation: 3636

You can use a css chunk with the code chunk option (which is intended to allow for programmatically inserted code)

---
title: "Untitled"
output: html_document
params:
  my_css: my_css.css
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```  

```{css, code = readLines(params$my_css)}

```

Hello World!

Upvotes: 4

the-mad-statter
the-mad-statter

Reputation: 8676

You could try using htmltools::includeCSS():

Contents of my_css.css:

p {
  color: red;
}

Contents of Rmd:

---
title: "Untitled"
output: html_document
params:
  my_css: my_css.css
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

```{r}
htmltools::includeCSS(params$my_css)
```

Hello World!

Produces red text:

enter image description here

Upvotes: 4

Related Questions