用于使用RMarkdown中的knitr和kableExtra包创建多个表的For Loop [英] For Loop for Creating Multiple Tables using knitr and kableExtra packages in RMarkdown

查看:446
本文介绍了用于使用RMarkdown中的knitr和kableExtra包创建多个表的For Loop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在RMarkdown中创建多个表并使用kableExtra包设置其样式.例如,我有虹膜数据集.我的第一张表显示前20行,第二张表显示后20行,第三张表显示20行...下面是代码:

I need to create multiple tables in RMarkdown and style it with the kableExtra package. As an example, I have the iris dataset. My first table displays the first 20 rows, my second table the next 20 rows, and my third table next 20 rows... Below is the code:

---
title: ""
output: pdf_document
---

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

landscape(kable_styling(kable(iris[1:20, ], format = "latex", align = "c", 
     row.names = FALSE), latex_options = c("striped"), full_width = T))

landscape(kable_styling(kable(iris[21:40, ], format = "latex", align = "c", 
     row.names = FALSE), latex_options = c("striped"), full_width = T))

landscape(kable_styling(kable(iris[41:60, ], format = "latex", align = "c", 
     row.names = FALSE), latex_options = c("striped"), full_width = T))
```

它运行良好,它返回三个表,每个表在不同的工作表中.实际上,我不仅有三个表,所以我认为使用for循环会更明智,我利用了此链接中给出的答案

It works well and it returns three tables, each one in a different sheet. In reality I have more than just three tables so I thought it would be a lot wiser to use a for loop and I made use of the answer given in this link R: why kable doesn't print inside a for loop?. Simple I put a line break after each print call as advised.

---
title: "untitled"
output: pdf_document
---

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

library(knitr)
library(kableExtra)

for (i in 1:3) {

  print(landscape(kable_styling(
    kable(iris[20*(i-1)+1:20*i, ], format = "latex", align = "c", 
          row.names = FALSE), latex_options = c("striped"), full_width = T)))
  cat("\n")
}
```

但是它不起作用.我猜这是因为我用kableExtra包中的命令封装了kable命令.

But it does not work. I guess that is because I encapsulated the kable command with the commands from the kableExtra package.

有谁可以使它起作用?我的意思是,有一种方法可以使我免于打字吗?

Is there who can make it work? I mean is there a way that can save me from typing?

推荐答案

您现有的代码就在附近.我认为唯一需要做的更改是在块选项中添加results='asis'(我认为不需要多余的换行符).这是适用于我的完整RMarkdown内容

Your existing code was nearly there. I think the only change needed is to add results='asis' to the chunk option (I don't think the extra newline is necessary). Here's the full RMarkdown content which works for me

---
output: pdf_document
---

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

```{r results='asis'}  
library(knitr)
library(kableExtra)
for (i in 1:3) {
  print(landscape(kable_styling(
    kable(iris[20*(i-1)+1:20*i, ], format = "latex", align = "c", 
          row.names = FALSE), latex_options = c("striped"), full_width = T)))
}
```

这篇关于用于使用RMarkdown中的knitr和kableExtra包创建多个表的For Loop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆