在 RMD 文件中使用连字符在 YAML 中添加额外依赖项列表?

Adding a list of extra dependencies in YAML using hyphens in RMD file?

我有以下 YAML 作为 RMD 模板提供给我,供我在课程中使用:

---
title: "Chapter 1"
author: 
output:
  pdf_document:
    extra_dependencies:
    - geometry
    - multicol
    - multirow
  html_document:
    df_print: paged
---

然而,根据https://rmarkdown.rstudio.com/docs/reference/pdf_document.html,键"extra_dependencies"应该使用字符向量,那么为什么允许使用连字符列表?

我没有看到任何关于此的文档或解释,欢迎提供对此进行解释的资源!

extra_dependencies 成为字符向量的必要性是 R 的事情。您上面的不是 R,而是 YAML,这是不同的。如果你看到 R 在解析这个 header 时得到了什么,你会看到

yaml::yaml.load(string='
title: "Chapter 1"
author: 
output:
  pdf_document:
    extra_dependencies:
    - geometry
    - multicol
    - multirow
  html_document:
    df_print: paged')

# $title
# [1] "Chapter 1"
# $author
# NULL
# $output
# $output$pdf_document
# $output$pdf_document$extra_dependencies
# [1] "geometry" "multicol" "multirow"
# $output$html_document
# $output$html_document$df_print
# [1] "paged"

你可以看到嵌入的 $output$pdf_document$extra_dependencies 确实是一个 character 向量。


从当前YAML spec, section 3.1.1定义

... the sequence corresponds to a Perl array and a Python list ...

(对我来说,它类似于 R 的向量),在 section 2.1 中,示例 2.1 ("Sequence of Scalars") 演示了一个序列

- Mark McGwire
- Sammy Sosa
- Ken Griffey

这意味着 yaml 中的一个序列(在 python 中是一个列表,在 R 中是一个向量)被编码为 bullet-list (optionally-indented),每个元素都使用连字符.

回想一下,R 需要 character 向量来实现 pdf_document 函数。 R-markdown文档中的header不是R,是YAML,所以问题是"how to encode the options in the yaml header so that R will eventually see it as a vector of strings"。这就是作为缩进列表使用连字符标识每个元素的方式。