Hi, I'm trying to use knitr and rmarkdown. I'd like to generate chunk names via R expressions. For example, I have a variable, call it foo
, that changes values in one or more code chunks. How can I use that variable foo
to name a R code chunk?
I've tried the following in a Rmd file:
foo <- 2
plot(1:4)
foo <- 10
plot(1:4)
When I try to knit an rmarkdown file containing the above code, I get an error about having a duplicate label.
processing file: Untitled.Rmd
Error in parse_block(g[-1], g[1], params.src) :
duplicate label 'paste0("plot-",foo)'
Calls: ... process_file -> split_file -> lapply -> FUN -> parse_block
Execution halted
How can I create chunk labels that use the distinct values of the variable foo
?
Is there a way to have R evaluate the expression that gets passed to label
?
Here is the full text of my Untitled.Rmd file:
---
title: "Untitled"
author: "Frederick Boehm"
date: "8/1/2019"
output: html_document
---
```{r setup}
foo <- 2
```
```{r label = paste0("plot-", foo)}
plot(1:4)
foo <- 10
```
```{r label = paste0("plot-", foo)}
plot(1:4)
```