Can I set the CSS/JS paths parametrically in Xaringan YAML?

淺唱寂寞╮ 提交于 2019-12-22 10:09:17

问题


This is similar to Xaringan issue #29. I'm setting up a Xaringan template to be used at work, and I want to keep all the CSS and Javascript files in the same location. I don't want anyone to have to copy the same files into each presentation directory.

However, I also don't want to refer to a common root directory. I could (I think) point to our in-house Bitbucket server, but I would much rather analysts and data scientists be able to get the files from their local repo (for speed, and also in case they're not on the network or are making their own changes).

I'm also not particularly hung up on using Infinite Moon Reader. (That was part of issue #29.) It's easy enough for me to remember to hit Control+Shift+K every time I hit Control+S.

What I had in mind was just to include a xaringan_dir parameter, and use that in my lists of CSS / JS resources.

My file structure at the moment is my xaringan directory, in which I have a css directory for CSS, a js directory for Javascript, and company-xaringan-template.Rmd. This YAML works:

---
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'
---

To parametrize my YAML, I tried this:

---
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'
---

I know I can use parameters in my YAML. This works, for example:

---
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: ...
---

And my r snippets work by themselves. These lines work as expected:

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")

But when I run my parametrized YAML, I get this error:

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

Additional notes:

  • If I use my original, non-parameterized beforeInit line, with the new css line, I get the same error.
  • If I use my original, non-parameterized css line, with the new beforeInit line, it knits the document but the document won't render in my browser.

Any ideas? Can this be done? And/or should I be considering another approach?

来源:https://stackoverflow.com/questions/54044332/can-i-set-the-css-js-paths-parametrically-in-xaringan-yaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!