[英]How to parameterize the inline code, text, together with R code chunk in Rmarkdown
In my Rmarkdown report, most of sections have the same text, inline code and R code chunk.在我的 Rmarkdown 报告中,大多数部分具有相同的文本、内联代码和 R 代码块。 Is it possible to parameterize them?
是否可以参数化它们? For example the below image, is it possible to use something like for loop to produce them instead of repeating similar code 3 times?
例如下图,是否可以使用类似 for 循环的东西来生成它们而不是重复类似的代码 3 次?
In main RMD file,在主 RMD 文件中,
library(tidyverse)
dat <- tibble(
id = 1:3,
fruit = c("apple", "orange", "banana"),
sold = c(10, 20, 30)
)
res <- lapply(dat$id, function(x) {
knitr::knit_child(
'template.Rmd', envir = environment(), quiet = TRUE
)
})
cat(unlist(res), sep = '\n')
In template.RMD,在模板.RMD中,
current_dat <- filter(dat, id == x)
# Section: `r current_dat$fruit`
current_dat %>%
ggplot(aes(x = fruit, y = sold)) + geom_col()
IMHO, the simplest wat to achieve this is to use results = 'asis'
and cat()
below is a minimal RMarkdown file.恕我直言,实现此目的最简单的方法是使用
results = 'asis'
并且下面的cat()
是一个最小的 RMarkdown 文件。
---
title: "Minimal example"
---
```{R results = "asis"}
for (i in 1:3) {
x <- runif(10)
cat("# section", floor(i), "\n")
plot(x)
# line break
cat("\n\n")
}
```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.