4

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

但是,我也不想引用公共根目录。我可以(我认为)指向我们内部的 Bitbucket 服务器,但我更希望分析师和数据科学家能够从他们的本地 repo 中获取文件(为了速度,以及如果他们不在网络上或正在做出自己的改变)。

我也不是特别热衷于使用 Infinite Moon Reader。(这是问题 #29 的一部分。)我很容易记住Control+Shift+K每次击中Control+S.

我的想法只是包含一个xaringan_dir参数,并在我的 CSS / JS 资源列表中使用它。

我目前的文件结构是我的xaringan目录,其中有一个cssCSSjs目录、一个 Javascript 目录和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行一起使用,它会编织文档,但文档不会在我的浏览器中呈现。

有任何想法吗?这可以做到吗?和/或我应该考虑另一种方法吗?

4

0 回答 0