我可以在Xaringan YAML中参数设置CSS / JS路径吗?

时间:2019-01-04 18:33:56

标签: r r-markdown xaringan

这类似于Xaringan issue #29。我正在设置要在工作中使用的Xaringan模板,并且希望将所有CSS和Javascript文件保留在同一位置。我不希望任何人都必须将相同的文件复制到每个演示文稿目录中。

但是,我也不想引用公共根目录。我可以(我认为)指向我们内部的Bitbucket服务器,但我希望分析师和数据科学家能够从其本地存储库中获取文件(以提高速度,以及“不在网络上或正在进行自己的更改)。

我也不特别喜欢使用Infinite Moon Reader。 (这是问题29的一部分。)我很容易记得每次我打Control+Shift+K时都会打Control+S

我想到的只是添加一个xaringan_dir参数,并将其用于我的CSS / JS资源列表。

目前,我的文件结构是我的xaringan目录,其中有一个CSS的css目录,一个Javascript的js目录和company-xaringan-template.Rmd YAML起作用:

---
title: 'This is a Title'
subtitle: 'This is a subtitle'
author: 'Benjamin Wolfe'
date: 'January 1, 2019 (revised `r Sys.Date()`)'
output:
  xaringan::moon_reader:
    css: [default, ./css/company-theme.css, ./css/company-theme-fonts.css, ./css/custom-classes.css]
    lib_dir: libs
    nature:
      beforeInit: './js/macros.js'
      highlightStyle: github
      highlightLines: true
      countIncrementalSlides: false
      slideNumberFormat: '%current%'
      ratio: '16:9'
---

要参数化我的YAML,我尝试了以下方法:

---
title: 'This is a Title'
subtitle: 'This is a subtitle'
author: 'Benjamin Wolfe'
date: 'January 1, 2019 (revised `r Sys.Date()`)'
params:
  xaringan_dir: '.'
output:
  xaringan::moon_reader:
    css: [`r paste(c('default', file.path(params$xaringan_dir, 'css', c('company-theme.css', 'company-theme-fonts.css', 'custom-classes.css'))), collapse = ', ')`]
    lib_dir: libs
    nature:
      beforeInit: '`r file.path(params$xaringan_dir, "js", "macros.js")`'
      highlightStyle: github
      highlightLines: true
      countIncrementalSlides: false
      slideNumberFormat: '%current%'
      ratio: '16:9'
---

我知道我可以在YAML中使用参数。例如,这可行:

---
title: 'This is a Title'
author: 'Benjamin Wolfe'
date: 'January 1, 2019 (revised `r Sys.Date()`)'
params:
  xaringan_dir: '.'
  foo_bar: 'This is a subtitle'
subtitle: '`r params$foo_bar`'
output: ...
---

我的r代码段是自己工作的。这些行按预期工作:

params <- list()
params$xaringan_dir <- '.'

paste(c('default', file.path(params$xaringan_dir, 'css', c('esurance.css', 'esurance-fonts.css', 'custom-classes.css'))), collapse = ', ')
file.path(params$xaringan_dir, "js", "macros.js")

但是当我运行参数化的YAML时,会出现此错误:

Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: while scanning for the next token at line 9, column 11 found character that cannot start any token at line 9, column 11
Calls: <Anonymous> ... parse_yaml_front_matter -> yaml_load -> <Anonymous>
Execution halted

其他说明:

  • 如果我将原来的非参数化的beforeInit行与新的css行一起使用,则会遇到相同的错误。
  • 如果我将原始的非参数化的css行与新的beforeInit行一起使用,它将编织文档,但该文档将不会在浏览器中呈现。

有什么想法吗?能做到吗?和/或我应该考虑另一种方法?

0 个答案:

没有答案