问题描述
在 r-markdown 中是将 title:
移出主 yaml、as described in the R Markdown Cookbook 的选项。
但是,在 xaringan 幻灯片集中,新的 ---
似乎与新幻灯片的想法相冲突。
下面的代码有效,但是当将第 2 行,title: "Presentation Ninja"
移到主 yaml 之外,并将其插入为 title: "The name of the dog is
r dog_name!"
,通过删除我所有的<!-- ... -->
第 17-19 行中的代码,它没有按预期工作。我不再得到标题页。我想我需要解决 xaringan 中的 ---
?
任何帮助将不胜感激!
---
title: "Presentation Ninja"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
output:
xaringan::moon_reader:
lib_dir: libs
---
# xaringan set the document title dynamically
```{r,code=xfun::read_utf8('script_with_many_dog_names.R')}
```
The name of the dog is `r dog_name`!
<!-- --- -->
<!-- title: "The name of the dog is `r dog_name`!" -->
<!-- --- -->
Some bullets
- Foo
- Bar
解决方法
您可以将逻辑直接添加到标题中,这对我来说似乎很好:
---
title: "The number is `r round(100 * runif(1),2)`"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
output:
xaringan::moon_reader:
lib_dir: libs
---
,
您可以使用 R Markdown
参数:
- 创建模板文件
Template.Rmd
---
title: "The name of the dog is `r params$dog_name`"
subtitle: "⚔<br/>with xaringan"
author: "John Doe"
output:
xaringan::moon_reader:
lib_dir: libs
params:
dog_name: NA
---
# xaringan set the document title dynamically
Some bullets
- Foo
- Bar
- 设置
dog_name
参数后渲染模板:
source('script_with_many_dog_names.R')
# As an example,but it could be the result of previous script
params <- list(dog_name = 'Charles')
rmarkdown::render('Template.Rmd',output_file = 'presentation.html',params = params,envir = new.env(parent = globalenv())
)