Yorgos
Yorgos

Reputation: 30445

How to use R variables in css code chunks?

The bookdown reference for SQL chunks states that:

If you need to bind the values of R variables into SQL queries, you can do so by prefacing R variable references with a ?.

But, how do you use R variables in css code chunks?

Upvotes: 0

Views: 123

Answers (1)

user2554330
user2554330

Reputation: 44867

If you want to output CSS code that affects the document, I'd use the glue package to do the substitutions, in an R code chunk that has echo=FALSE, results='asis'. For example,

```{r css, echo=FALSE, results='asis'}
margin <- 40

library(glue)
cat(glue(.open = "<<", .close = ">>", "
<style>
h1 {
  margin-left: <<margin>>px;
}
</style>
"))
```

would put the parts between <style> ... </style> into your document, substituting for R variables whereever it sees a variable name in double angle brackets, e.g. <<margin>> in my example.

Upvotes: 2

Related Questions