Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Rmd中的YAML元数据中创建三级对象?_R_R Markdown_Pandoc - Fatal编程技术网

如何在Rmd中的YAML元数据中创建三级对象?

如何在Rmd中的YAML元数据中创建三级对象?,r,r-markdown,pandoc,R,R Markdown,Pandoc,我发现了一个在YAML元数据中创建两级对象的好例子。例如,为了包含多个作者 --- title: 'This is the title: it contains a colon' author: - name: Author One affiliation: University of Somewhere - name: Author Two affiliation: University of Nowhere --- 然后在htmltemplate $for(author)$ $i

我发现了一个在YAML元数据中创建两级对象的好例子。例如,为了包含多个作者

---
title:  'This is the title: it contains a colon'
author:
- name: Author One
  affiliation: University of Somewhere
- name: Author Two
  affiliation: University of Nowhere
---
然后在html
template

$for(author)$
$if(author.name)$
$author.name$$if(author.affiliation)$ ($author.affiliation$)$endif$
$else$
$author$
$endif$
$endfor$
是否可以在YAML中创建三级对象?例如,我希望同一机构有多个作者。因此,我的逻辑是

---
title:  'This is the title: it contains a colon'
author:
  - affiliation: 
     - name: Author One
       email: fake@fake.com
     - name: Author Two
       email: fake2@fake.com
  - affiliation: 
     - name: Author Three
       email: fake3@fake.com
---
但它不起作用。有可能吗?我知道它看起来没有任何意义,但它是我的html
模板
结构所必需的

谢谢你的帮助

更新

我还添加了一个在html
模板中创建的块

$if(author)$
$for(author)$
<div class = "author-section">
<div>$author.affiliation$</div>
<div class = "authors">
$for(author.affiliation)$
<div class = "author">
<div class = "mid-col">
$author.affiliation.name$
</div>
<div class = "right-col">
<a>$author.affiliation.email$</a>
</div>
</div>
$endfor$
</div>
</div>
$endfor$
$endif$
我在渲染过程中出错

Error in yaml::yaml.load(string, ...) : 
  Scanner error: mapping values are not allowed in this context at line 9, column 19

就我个人而言,我会让它保持在你的第一个例子中。您可以为两个不同的作者编写两次相同的从属关系名称

但是,如果您真的想通过大学将YAML编写成group/nest作者,它会是这样的(如果您熟悉JSON,请尝试一些在线YAML到JSON转换器):

然后,pandoc模板循环将如下所示:

$for(university)$
<div>
  $university.label$
  $for(university.author)$
    $university.author.name$
    <span class="">$university.author.email$</span>
  $endfor$
</div>
$endfor$
$for(大学)$
$university.label$
$for(大学作者)$
$university.author.name$
$university.author.email$
$endfor$
$endfor$

您可以将YAML列表和对象嵌套到任意深度(google YAML)。但是,您需要相应地修改四个HTML模板(为
循环添加更多的
$)。我不知道你期望得到什么结果,也不知道你筑巢的目的是什么…@mb21谢谢,我编辑了我的问题。请再看一遍。
title:  'This is the title: it contains a colon'
university:
  - label: University of Somehwere
    author:
     - name: Author One
       email: fake@fake.com
     - name: Author Two
       email: fake2@fake.com
  - label: University of Nohwere
    author:
     - name: Author Three
       email: fake3@fake.com
$for(university)$
<div>
  $university.label$
  $for(university.author)$
    $university.author.name$
    <span class="">$university.author.email$</span>
  $endfor$
</div>
$endfor$