如何在 rmarkdown 中使用 for 循环? [英] how to use a for loop in rmarkdown?
本文介绍了如何在 rmarkdown 中使用 for 循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑这个简单的例子:
---
title: "Untitled"
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Slide with R Output
```{r t, warning=FALSE, message=FALSE}
library(knitr)
library(kableExtra)
library(dplyr)
for(threshold in c(20, 25)) {
cars %>%
filter(dist < threshold) %>%
kable('html') %>%
kable_styling(bootstrap_options = "striped")
}
```
在这里,我只想将 for 循环
的每个输出打印到不同的幻灯片中.在这个例子中,有两个对 kable
的调用应该在两个不同的幻灯片上进行.
Here I simply want to print each output of the for loop
into a different slide. In this example, there are two calls to kable
that should go on two different slides.
上面的代码不起作用.我什至为此使用了正确的软件包吗?有什么想法吗?
The code above does not work. Am I even using the right packages for that? Any ideas?
谢谢!
推荐答案
您可以使用 asis
选项:
---
title: "Untitled"
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)
library(dplyr)
# needed so r will include javascript/css dependencies needed for striped tables:
kable(cars, "html") %>% kable_styling(bootstrap_options = "striped")
```
```{r, results = "asis"}
for (threshold in c(20, 25)) {
cat("\n\n##\n\n")
x <- cars %>%
filter(dist < threshold) %>%
kable('html') %>%
kable_styling(bootstrap_options = "striped")
cat(x)
}
```
这篇关于如何在 rmarkdown 中使用 for 循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文