R Markdown Parameters - can I combine vectors to use as parameters?

Viewed 1490

If you type summary(c(mtcars, cars)) into your console you will receive output. But the following R Markdown (shown below) throws the error, "object 'test' not found".

How do I combine vectors to use as parameters in R Markdown? Maybe this is not allowed and parameters must be either single numbers, or character strings?? I want the test parameter to be c(mtcars, cars). Or perhaps in the future I'll have a parameter VariableX that I'd want to be c("apple", "pear", "orange").

---
title: "Untitled"
output: html_document
params:
  n: 100
  test: c(mtcars, cars)
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r test}
summary(test)
```
1 Answers

As explained in the R Markdown book:

We can also use R objects by including !r before R expressions

To access the parameter within the report using params$test. Putting this together:

---
title: "Untitled"
output: html_document
params:
  n: 100
  test: !r c(mtcars, cars)
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r test}
summary(params$test)
```

enter image description here

Related