Consider the following Rmd file,
---
title: "Untitled"
author: "baptiste"
date: "`r Sys.Date()`"
output: html_document
test: "`r paste('_metadata.yaml')`"
---
```{r}
cat(rmarkdown::metadata$test)
```
The date is processed (knitted) by R before being passed to pandoc for conversion to md and html. The custom field test
, however, is unevaluated.
What's the difference? Can one force knitr/rmarkdown to evaluate an arbitrary field in the yaml header?
Note: the actual purpose is not to just print()
a filename as in this dummy example, but to load an external yaml file containing metadata (author information), process it with R, and output a string that will be injected in the document.
YAML header is a short blob of text, specially formatted with key: value pairs tags, that seats at the top of our Rmarkdown document. The header not only dictates the final file format, but a style and feel for our final document.
Creating Headings and Subheadings We can insert headings and subheadings in R Markdown using the pound sign # . There are six heading/subheading sizes in R Markdown. The number of pound signs before your line of text determines the heading size, 1 being the largest heading and 6 being the smallest.
Inline code enables you to insert R code into your document to dynamically updated portions of your text. To insert inline code you need to encompass your R code within: . For example, you could write: Which would render to: The mean sepal length found in the iris data set is 5.8433333.
YAML can be used at the top of Markdown documents to add more structured data. Surround the YAML with two lines of consecutive dashes. --- name: "Venus" discoverer: "Galileo Galilei" --- *Venus* is the second planet from the Sun, orbiting it every 224.7 Earth days.
It does evaluate the code. If you run foo.Rmd with
rmarkdown::render("foo.Rmd", clean = FALSE)
you'll see an intermediate file (the pandoc input) called foo.knit.md
left behind. It will look like this:
---
title: "Untitled"
author: "baptiste"
date: "2017-08-12"
output: html_document
test: "_metadata.yaml"
---
```r
cat(rmarkdown::metadata$test)
```
```
## `r paste('_metadata.yaml')`
```
I don't know how to see that from within the document (your example shows that metadata$test
doesn't work), but there's probably some trick or other to get at it.
The standard metadata field data
and your custom field test
are not actually treated any differently. This code:
---
title: "Untitled"
author: "baptiste"
date: "`r Sys.Date()`"
output:
html_document:
keep_md: yes
test: "`r paste('_metadata.yaml')`"
---
```{r}
cat(rmarkdown::metadata$date)
cat(rmarkdown::metadata$test)
```
leads to the following output:
As you can see, also date
was not evaluated. I have not found any functionality in the rmarkdown
or knitr
packages. But the following simple function does the trick at least for your simple example:
---
title: "Untitled"
author: "baptiste"
date: "`r Sys.Date()`"
output:
html_document:
keep_md: yes
test: "`r paste('_metadata.yaml')`"
---
```{r}
eval_meta <- function(x) eval(parse(text = gsub("`|r", "", x)))
eval_meta(rmarkdown::metadata$date)
eval_meta(rmarkdown::metadata$test)
```
Whether that works in your more complex situation is another question, however.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With