如何使用循环在rmarkDown中渲染多个图和表

发布于 2025-02-11 05:16:59 字数 1600 浏览 0 评论 0原文

我正在研究rmarkDown文档,以显示一些图和表。该代码正常运行,但我发现有很多图和表格,因此添加它们的过程确实很大。我的代码是下一个(我需要渲染到Word文档):

---
title: "My document"
output: word_document
date: '2022-06-28'
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Analysis

```{r t1}
library(ggplot2)
library(dplyr)
library(knitr)
#Data
data("iris")
iris
#Create list for plots and tables
List <- split(iris,iris$Species)
#Create plots
plotfun <- function(x)
{
  y <- ggplot(x,aes(x=Sepal.Length,y=Sepal.Width))+
    geom_point()
  return(y)
}
#Create tables
tablefun <- function(x)
{
  y <- x %>% group_by(Species) %>%
    summarise_all(sum)
  return(y)
}
#Create list of plots and tables
Lplots <- lapply(List, plotfun)
Ltables <- lapply(List, tablefun)
```

直到没关系。当我需要打印图和表格时,出现了麻烦。我使用此代码:

```{r,echo=FALSE,warning=FALSE,message=FALSE,fig.cap=paste0("Figure 1","My plot"),fig.pos='H',fig.align='center',out.width='100%',fig.height=5,fig.width=9}
Lplots[[1]]
```

```{r,echo=FALSE,message=FALSE,warning=FALSE}
kable(Ltables[[1]],caption = paste("Table 1: Summary"),align = c('l',rep('c',4)))
```

但是,如果我有100个图和表,那将是非常广泛的。

有人可以帮助我如何做这样的事情:

for(i in 1:length(Lplots))
{

```{r,echo=FALSE,warning=FALSE,message=FALSE,fig.cap=paste0("Figure ",i,"My plot"),fig.pos='H',fig.align='center',out.width='100%',fig.height=5,fig.width=9}
Lplots[[i]]
```

```{r,echo=FALSE,message=FALSE,warning=FALSE}
kable(Ltables[[i]],caption = paste("Table",i," : Summary"),align = c('l',rep('c',4)))
```
}

非常感谢。

I am working on a Rmarkdown document to show some plots and tables. The code works fine but I got the issue that there are many plots and tables so the process of adding them is really large. My code is next (I need to render to word document):

---
title: "My document"
output: word_document
date: '2022-06-28'
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Analysis

```{r t1}
library(ggplot2)
library(dplyr)
library(knitr)
#Data
data("iris")
iris
#Create list for plots and tables
List <- split(iris,iris$Species)
#Create plots
plotfun <- function(x)
{
  y <- ggplot(x,aes(x=Sepal.Length,y=Sepal.Width))+
    geom_point()
  return(y)
}
#Create tables
tablefun <- function(x)
{
  y <- x %>% group_by(Species) %>%
    summarise_all(sum)
  return(y)
}
#Create list of plots and tables
Lplots <- lapply(List, plotfun)
Ltables <- lapply(List, tablefun)
```

Until that is fine. The trouble appears when I need to print the plots and tables. I use this code:

```{r,echo=FALSE,warning=FALSE,message=FALSE,fig.cap=paste0("Figure 1","My plot"),fig.pos='H',fig.align='center',out.width='100%',fig.height=5,fig.width=9}
Lplots[[1]]
```

```{r,echo=FALSE,message=FALSE,warning=FALSE}
kable(Ltables[[1]],caption = paste("Table 1: Summary"),align = c('l',rep('c',4)))
```

But if I have 100 plots and tables, it would be very extensive.

Can somebody help me how I can do something like this:

for(i in 1:length(Lplots))
{

```{r,echo=FALSE,warning=FALSE,message=FALSE,fig.cap=paste0("Figure ",i,"My plot"),fig.pos='H',fig.align='center',out.width='100%',fig.height=5,fig.width=9}
Lplots[[i]]
```

```{r,echo=FALSE,message=FALSE,warning=FALSE}
kable(Ltables[[i]],caption = paste("Table",i," : Summary"),align = c('l',rep('c',4)))
```
}

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄与心 2025-02-18 05:16:59

假设您想在rmarkDown中打印图和表,而不是保存,则可以。 )和3。将结果分配给某些虚拟对象(“抑制”)。如果您不这样做,它将打印出丑陋的输出,例如null或组标题,就好像是控制台输出一样。您需要在每个打印呼叫中分配给虚拟对象并分配整个功能调用。我猜您希望您的桌子和绘制组合在一起,因此您可以将它们放在一个循环中。

{r,echo=FALSE,warning=FALSE,message=FALSE,fig.pos='H',fig.align='center',out.width='100%',fig.height=5,fig.width=9, results='asis'}

lapply(1:length(List), function(i) {
  print(plotfun(List[[i]]) +
    ggtitle(paste("My Plot", i)) -> suppress)
  print(kable(tablefun(List[[i]]),caption = sprintf("Table %s: Summary", i),align = c('l',rep('c',4)))) -> suppress
  }) -> suppress

我在代码块和印刷图中删除了您的图caption选项,并动态地删除了表格。这应该编织到任何所需的输出中。

Assuming you want to print the plots and tables in your rmarkdown and not save them, you can 1. change code chunk option to results = 'asis' 2. wrap each function call in print() and 3. assign results to some dummy object ("suppress"). If you don't do this it will print ugly output, like NULL or the group title as if it were console output. You'll want to assign to dummy object within each print call and assign the whole function call. I'd guess you'd want your tables and plots together by group, so you can put them in one loop.

{r,echo=FALSE,warning=FALSE,message=FALSE,fig.pos='H',fig.align='center',out.width='100%',fig.height=5,fig.width=9, results='asis'}

lapply(1:length(List), function(i) {
  print(plotfun(List[[i]]) +
    ggtitle(paste("My Plot", i)) -> suppress)
  print(kable(tablefun(List[[i]]),caption = sprintf("Table %s: Summary", i),align = c('l',rep('c',4)))) -> suppress
  }) -> suppress

I removed your fig.caption option in code chunk and printed plot and table number dynamically. This should knit into whatever desired output.

做个ˇ局外人 2025-02-18 05:16:59

您应该能够通过将循环放入代码块中来保存图。

for(i in length(lPlots)){
    ggsave(fname,lPlots[[i])
}

至少对于图的另一个有用的选择是使用pwalk(),如下所示 https://cmdlinetips.com/2022/06/create-a-liste-a-list-obgplot-ggplot-objects-and-save-and-save-save-save-them-as-as-files/

You should be able to save the plots by putting the loop within the code chunk.

for(i in length(lPlots)){
    ggsave(fname,lPlots[[i])
}

Another useful option for at least for plots is to use pwalk() as shown here https://cmdlinetips.com/2022/06/create-a-list-of-ggplot-objects-and-save-them-as-files/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文