如何在 css 代码块中使用 R 变量?

How to use R variables in css code chunks?

bookdown reference for SQL chunks 指出:

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

但是,如何在 css 代码块中使用 R 变量?

如果您想输出影响文档的 CSS 代码,我会使用 glue 包在具有 echo=FALSE, results='asis' 的 R 代码块中进行替换。例如,

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

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

会将 <style> ... </style> 之间的部分放入您的文档中,只要在双尖括号中看到变量名称,就替换为 R 变量,例如<<margin>> 在我的示例中。