My question is similar to this SO question but instead of creating a plot, I want to evaluate code chunk. I wanted to know if it's possible to programmatically generate headers and evaluate code chunks following their respective headers? My attempt at this is below.
I have a list
of data frames that I want to create a header and evaluate code chunk for each list
element. The example below is just to calculate the number of rows of each data frame using nrow()
.
```{r data}
data("iris","airquality","mtcars")
my_list <- list(iris,airquality,mtcars)
names(my_list) <- c("iris","airquality","mtcars")
```
```{r headers, results = 'asis'}
for (i in seq_along(my_list)) {
cat('#', names(my_list)[i], '\n')
cat('```{r}', '\n')
cat('nrow(mylist[[i]])') #evaluate any other code here
cat('\n')
cat('```')
cat('\n')
}
```
Any insight would be much appreciated!