我正在编织一个名为MyFile.rmd
的降价文件。
如何在编织过程中访问字符串MyFile
并将其用于:
在后续的R chunk中使用?
---
title: "`r rmarkdown::metadata$title`"
author: "My Name"
date: "10. Mai 2015"
output: beamer_presentation
---
## Slide 1
```{r}
rmarkdown::metadata$title
```
导致......
...这是不正确的,因为我编织的文件名称不同。
> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
locale:
[1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] digest_0.6.8 htmltools_0.2.6 rmarkdown_0.5.1 tools_3.1.2 yaml_2.1.13
答案 0 :(得分:7)
rmarkdown::metadata
为您提供R Markdown的元数据列表,例如rmarkdown::metadata$title
将成为您文档的标题。一个例子:
---
title: "Beamer Presentation Title"
author: "My Name"
date: "10\. Mai 2015"
output: beamer_presentation
---
## Slide 1
Print the title in a code chunk.
```{r}
rmarkdown::metadata$title
```
## Slide 2
The title of the document is `r rmarkdown::metadata$title`.
要获取输入文档的文件名,请使用knitr::current_input()
。
答案 1 :(得分:1)
总结一下Yihui的回答:
---
title: "`r knitr::current_input()`"
author: "My Name"
date: "10. Mai 2015"
output: beamer_presentation
---
## Slide 1
```{r}
knitr::current_input()
```
针织工作。