lar*_*ara 4 r knitr r-markdown kableextra
我需要在 RMarkdown 中创建多个表并使用 kableExtra 包对其进行样式设置。例如,我有鸢尾花数据集。我的第一个表显示前 20 行,第二个表显示接下来的 20 行,第三个表显示接下来的 20 行...下面是代码:
---
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))
```
Run Code Online (Sandbox Code Playgroud)
它运行良好,它返回三个表,每个表位于不同的工作表中。实际上,我有不止三个表,所以我认为使用 for 循环会更明智,并且我利用了此链接R: Why kable does not print inside a for 循环中给出的答案?。很简单,我按照建议在每次打印调用后放置一个换行符。
---
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")
}
```
Run Code Online (Sandbox Code Playgroud)
但这不起作用。我想这是因为我用 kableExtra 包中的命令封装了 kable 命令。
有谁可以让它发挥作用吗?我的意思是有什么方法可以让我免于打字吗?
您现有的代码已经差不多了。我认为唯一需要的更改是添加results='asis'到块选项(我认为不需要额外的换行符)。这是对我有用的完整 RMarkdown 内容
---
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)))
}
```
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5553 次 |
| 最近记录: |