I am trying to dynamically generate plots in Rmarkdown. Each plot is generated in a new tab. The below code works fine:
---
title: several tabs
output: html_document
---
```{r setup, include=FALSE}
library(plotly)
```
## Graphs {.tabset .tabset-pills}
```{r, include = FALSE}
template <- c(
"### {{tab}}\n",
"```{r, echo = FALSE, message=FALSE}\n",
"plot_ly(x = rnorm(10))\n",
"```\n",
"\n"
)
p <- lapply(1:3, function(i) {
knitr::knit_expand(
tab = i,
text = template)
})
```
r knitr::knit_child(text = unlist(p))
How can I dynamically paste in my data in the template? The below code results in an error because the data is evaluated as text rather then data:
```{r, include = FALSE}
template <- c(
"### {{tab}}\n",
"```{r, echo = FALSE, message=FALSE}\n",
"plot_ly(x = {{ data }})\n",
"```\n",
"\n"
)
p <- lapply(1:3, function(i) {
data <- data.frame(
value1 = c(1,4,6,7,8,19,20),
value2=c(1,3,6,7,6,19,21)
)
knitr::knit_expand(
tab = i,
data = data,
text = template)
})
Error in parse(text = x, srcfile = src) :
<text>:4:0: Unexpected End of Entry
2: plot_ly(x = c(1, 4, 6, 7, 8, 19, 20)
3: