Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluating R code in YAML header

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)
```

enter image description here

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.

like image 203
baptiste Avatar asked Aug 12 '17 03:08

baptiste


People also ask

What is a YAML header in r?

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.

How do I add a header in R Markdown?

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.

What is r inline code?

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.

How do I use YAML Markdown?

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.


2 Answers

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.

like image 82
user2554330 Avatar answered Sep 27 '22 00:09

user2554330


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:

enter image description here

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)
```

enter image description here

Whether that works in your more complex situation is another question, however.

like image 35
Stibu Avatar answered Sep 26 '22 00:09

Stibu