【问题标题】:How can I split a table so that it appears side by side in R markdown?如何拆分表格以使其在 R markdown 中并排显示?
【发布时间】:2019-06-04 13:40:38
【问题描述】:

我正在用 R markdown 写一个文档,我想放一张桌子。问题是这个表只有两列,占满一页,不太美观。所以我的问题是:有没有办法将此表一分为二,并将两个“子表”并排放置,只有一个标题?

我使用 kable 命令并尝试了此解决方案 (How to split kable over multiple columns?),但无法执行 cbind() 命令。

这是我创建表格的代码:

---
title: 
author: 
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: pdf_document
indent: true
header-includes:
  - \usepackage{indentfirst}
---

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

```{r, echo = FALSE}
kable(aerop2, format = "markdown")
```

其中 aerop2 是我的数据框,第 1 列中包含国家名称列表,第 2 列中包含每个国家/地区的机场数量。

我有一个很长的两列表格,很浪费空间。我想将此表拆分为两个子表,并将这些子表并排放置,并带有包含它们的标题。

【问题讨论】:

  • 这个问题stackoverflow.com/q/38036680/2554330 询问如何将两张桌子并排放置。拆分表应该是可能的,只需选择行,例如rows <- 1:(nrow(aerop2) %/% 2); part1 <- aerop2[rows,]; part2 <- aerop2[-rows,]
  • 问题是它创建了两个单独的表格和两个标题,有没有办法只有一个标题包含这两个表格?
  • 您需要查找“子表”。这是一个乳胶问题。例如,请参阅tex.stackexchange.com/questions/294589/…

标签: r-markdown


【解决方案1】:

这并没有在间距方面提供很大的灵活性,但这是一种方法。我以mtcars 数据集为例,因为我没有aerop2

---
output: pdf_document
indent: true
header-includes:
  - \usepackage{indentfirst}
  - \usepackage{booktabs}
---

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

The data are in Table \ref{tab:tables}, which will float to the top of the page.

```{r echo = FALSE}
rows <- seq_len(nrow(mtcars) %/% 2)
kable(list(mtcars[rows,1:2],  
           matrix(numeric(), nrow=0, ncol=1),
           mtcars[-rows, 1:2]), 
      caption = "This is the caption.",
      label = "tables", format = "latex", booktabs = TRUE) 
```

这给出了:

请注意,如果没有该零行矩阵,这两个部分会更靠近。为了更多地增加间距,将零行矩阵的额外副本放入 列表。

【讨论】:

  • 这对我来说不适用于一张很长的桌子。
  • @bob:您应该发布一个新问题并包含一个可重现的示例来说明问题所在。请在您的问题中参考此问题,以免您的问题被删除。
【解决方案2】:

“user2554330”提供的解决方案非常有用。
由于我需要拆分更多列并最终拆分更多部分,我进一步发展了这个想法。 我还需要在文本之后放置表格,而不是浮动到顶部。我找到了一种使用kableExtra::kable_styling(latex_options = "hold_position") 的方法。 我写在这里是为了分享发展并提出一些小问题。
1 - 为什么添加 - \usepackage{indentfirst}这一行?
2 - label = "tables" 作为 kable() 输入的效果是什么?
(这些问题与 Latex 有关。我可能对 kable() 文档中的解释知之甚少:“标签 - 表格参考标签”!)

---
title: "Test-split.print"
header-includes:
  - \usepackage{booktabs}
output:
  pdf_document: default
  html_document:
    df_print: paged
---

```{r setup, include=FALSE}
suppressPackageStartupMessages(library(tidyverse))
library(knitr)
library(kableExtra)

split.print <- function(x, cols = 2, sects = 1, spaces = 1, caption = "", label = ""){
  if (cols < 1) stop("cols must be GT 1!")
  if (sects < 1) stop("sects must be GT 1!")
  rims <- nrow(x) %% sects
  nris <- (rep(nrow(x) %/% sects, sects) + c(rep(1, rims), rep(0, sects-rims))) %>%
    cumsum() %>% 
    c(0, .)
  
  for(s in 1:sects){
    xs <- x[(nris[s]+1):nris[s+1], ]
    rimc <- nrow(xs) %% cols
    nric <- (rep(nrow(xs) %/% cols, cols) + c(rep(1, rimc), rep(0, cols-rimc))) %>%
      cumsum() %>% 
      c(0, .)
    lst <- NULL
    spc <- NULL
    for(sp in 1:spaces) spc <- c(spc, list(matrix(numeric(), nrow=0, ncol=1)))
    for(c in 1:cols){
      lst <- c(lst, list(xs[(nric[c]+1):nric[c+1], ]))
      if (cols > 1 & c < cols) lst <- c(lst, spc)
    }
    kable(lst, 
          caption = ifelse(sects == 1, caption, paste0(caption, " (", s, "/", sects, ")")),
          label = "tables", format = "latex", booktabs = TRUE) %>% 
      kable_styling(latex_options = "hold_position") %>% 
      print()
  }
}
```

```{r, results='asis'}
airquality %>% 
  select(1:3) %>% 
  split.print(cols = 3, sects = 2, caption = "multi page table")
```

【讨论】:

  • 这是一个很好的答案,因为我们可以确定列数。回答你的问题: 1)这行在这里没有用,我想我把它放在这里是因为它当时在我的个人代码中,我忘了删除它; 2)这里是为了在文本中引用(如果你删除它,你会在文本中得到“??”而不是表格的编号)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 2022-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多