问题
I would like to be able to create RMarkdown chuncks in a loop. I have tried doing this through a for
loop, without much success. I imagine this could probably be possible through lapply
, as one would do for creating UIs
in a shiny app. However, I haven't had any success so far.
Reprex:
---
title: "Untitled"
output:
html_document:
theme: united
highlight: tango
toc: true
toc_float:
collapsed: false
smooth_scroll: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
```
```{r}
library(dplyr)
library(ggplot2)
df <- datasets::iris %>%
dplyr::as_tibble()
```
## setosa
```{r}
df %>%
dplyr::filter(Species == "setosa") %>%
ggplot2::ggplot(ggplot2::aes(Sepal.Length, Petal.Length)) +
ggplot2::geom_point()
```
## versicolor
```{r}
df %>%
dplyr::filter(Species == "versicolor") %>%
ggplot2::ggplot(ggplot2::aes(Sepal.Length, Petal.Length)) +
ggplot2::geom_point()
```
## virginica
```{r}
df %>%
dplyr::filter(Species == "virginica") %>%
ggplot2::ggplot(ggplot2::aes(Sepal.Length, Petal.Length)) +
ggplot2::geom_point()
```
My goal is to create the headings (setosa, versicolor, and virginica) and the chuncks with a loop.
For example:
for(i in c("setosa", "versicolor", "virginica")) {
## i
df %>%
dplyr::filter(Species == i) %>%
ggplot2::ggplot(ggplot2::aes(Sepal.Length, Petal.Length)) +
ggplot2::geom_point()
}
Any ideas on how accomplish this?
回答1:
If you want to create headings + outputs within a loop, you can do:
```{r species_loop, results='asis'}
for(i in c("setosa", "versicolor", "virginica")) {
cat(paste0("\n\n## ", i, "\n"))
p <- df %>%
dplyr::filter(Species == i) %>%
ggplot2::ggplot(ggplot2::aes(Sepal.Length, Petal.Length)) +
ggplot2::geom_point()
print(p)
}
```
So:
- Using
results='asis'
to allow output that youcat()
to be interpreted as Markdown syntax cat()
ing the required markdown syntax to produce the headers (surrounded by some newlines to make sure it's interpreted properly)- Explicitly
print()
ing the plot within the loop.
回答2:
A function based on cat
would replicate your chunks for every iris species. For the one-time chunks use single cat
s.
FUN <- function(x) cat("\n##", x, "
```{r}
df %>%
dplyr::filter(Species == ",x, ") %>%
ggplot2::ggplot(ggplot2::aes(Sepal.Length, Petal.Length)) +
ggplot2::geom_point()
```\n")
To produce the shown .Rmd
file, you could use sink
. (For sake of brevity I'll omit the header here.)
sink(file="iris.Rmd") ## start `sink`
cat("```{r}
library(dplyr)
library(ggplot2)
df <- datasets::iris %>%
dplyr::as_tibble()
```")
invisible(sapply(c("'setosa'", "'versicolor'", "'virginica'"), FUN))
sink() ## end `sink`
You'll find your .Rmd
file in your working directory (getwd()
).
来源:https://stackoverflow.com/questions/57034529/create-rmarkdown-chuncks-in-a-loop