如何使用datasummary在R markdown中将表格分成2页?

问题描述 投票:0回答:2

我正在尝试通过 R Markdown 使用 datasummary 创建一个表。然而,我的表很长,有很多变量。请参阅下面的示例代码:

---
title: "R Notebook"
output:
  pdf_document: default
  html_notebook: default
  html_document:
    df_print: paged
---

Table 1 example:

```{r, warning=FALSE, message=FALSE, echo=FALSE, include=FALSE, fig.pos="H"}
library(magrittr)
library(tidyverse)
library(kableExtra)
library(readxl)
library(modelsummary)
library(scales)

tmp <- mtcars

tmp$test2 <- tmp$mpg
tmp$test3 <- tmp$cyl
tmp$test4 <- tmp$disp
tmp$test5 <- tmp$hp
tmp$test6 <- tmp$drat
tmp$test7 <- tmp$wt
tmp$test8 <- tmp$qsec
tmp$test9 <- tmp$vs
tmp$test10 <- tmp$am
tmp$test11 <- tmp$gear
tmp$test12 <- tmp$carb
tmp$test13 <- tmp$mpg
tmp$test14 <- tmp$cyl
tmp$test15 <- tmp$disp
tmp$test16 <- tmp$hp
tmp$test17 <- tmp$drat
tmp$test18 <- tmp$wt
tmp$test19 <- tmp$qsec
tmp$test20 <- tmp$vs
tmp$test21 <- tmp$am
tmp$test22 <- tmp$gear
tmp$test23 <- tmp$mpg
tmp$test24 <- tmp$cyl
tmp$test25 <- tmp$disp
tmp$test26 <- tmp$hp
tmp$test27 <- tmp$drat
tmp$test28 <- tmp$wt
tmp$test29 <- tmp$qsec
tmp$test30 <- tmp$vs
tmp$test31 <- tmp$am
tmp$test32 <- tmp$gear
tmp$test33 <- tmp$mpg
tmp$test34 <- tmp$cyl
tmp$test35 <- tmp$disp
tmp$test36 <- tmp$hp
tmp$test37 <- tmp$drat
tmp$test38 <- tmp$wt
tmp$test39 <- tmp$qsec
tmp$test40 <- tmp$vs
  
# create a list with individual variables
# remove missing and rescale
tmp_list <- lapply(tmp, na.omit)
tmp_list <- lapply(tmp_list, scale)

# create a table with `datasummary`
# add a histogram with column_spec and spec_hist
# add a boxplot with colun_spec and spec_box
emptycol = function(x) " "
final_4_table <- datasummary(mpg + cyl + disp + hp + drat + wt + qsec + vs + am + gear + carb + test2 + test3 + test4 + test5 + test6 + test7 + test8 + test9 + test10 + test11 + test12 + test13 + test14 + test15 + test16 + test17 + test18 + test19 + test20 + test21 + test22 + test23 + test24 + test25 + test26 + test27 + test28 + test29 + test30 + test31 + test32 + test33 + test34 + test35 + test36 + test37 + test38 + test39 + test40 ~ N + Mean + SD + Heading("Boxplot") * emptycol + Heading("Histogram") * emptycol, data = tmp, title = "Title of my table.", notes = list("note2", "note1")) %>%
    column_spec(column = 5, image = spec_boxplot(tmp_list)) %>%
    column_spec(column = 6, image = spec_hist(tmp_list))

```


```{r finaltable, echo=FALSE}
final_4_table
```

输出不适合如下所示的一页,并且我没有得到包含剩余变量的另一页:

enter image description here

如何确保在本例中表格被分成 2 个,移动到标题为“我的表格的标题。(续)”的下一页?

我检查了

tables
包小插图,并通过包含 longtable 的以下信息给了我相同的输出:

final_4_table <- datasummary(mpg + cyl + disp + hp + drat + wt + qsec + vs + am + gear + carb + test2 + test3 + test4 + test5 + test6 + test7 + test8 + test9 + test10 + test11 + test12 + test13 + test14 + test15 + test16 + test17 + test18 + test19 + test20 + test21 + test22 + test23 + test24 + test25 + test26 + test27 + test28 + test29 + test30 + test31 + test32 + test33 + test34 + test35 + test36 + test37 + test38 + test39 + test40 ~ N + Mean + SD + Heading("Boxplot") * emptycol + Heading("Histogram") * emptycol, data = tmp, title = "Title of my table.", notes = list("note2", "note1"), options = list(tabular="longtable",
          toprule="\\caption{This table crosses page boundaries.}\\\\
\\toprule", midrule="\\midrule\\\\[-2\\normalbaselineskip]\\endhead\\hline\\endfoot")) %>%
    column_spec(column = 5, image = spec_boxplot(tmp_list)) %>%
    column_spec(column = 6, image = spec_hist(tmp_list))
r r-markdown modelsummary tables-package
2个回答
1
投票

@manro 让您走上正轨。诀窍是使用

longtable=TRUE
参数。

在幕后,

modelsummary
调用kbl
中的
kableExtra
函数来生成一个表。所有
datasummary() 未明确使用的参数都会通过 kbl
 省略号自动推送到 
...
 函数。这意味着您可以直接在 
longtable
 调用中定义 
datasummary()
 参数。例如:
datasummary(mpg + hp ~ Mean + SD, data = mtcars, longtable = TRUE)

以上是一个最小的例子。我尝试在您更复杂的示例中以相同的方式传递
longtable=TRUE
参数,并且您的 Rmarkdown 文档在我的机器上编译没有问题。


这套衣服适合你吗?

1
投票
library(knitr) library(kableExtra) kable(iris[1:55, ], longtable = TRUE, caption = "Long-long-table") %>% #try to change iris[1:55, ] to final_4_table kable_styling(latex_options = c("repeat_header"))


© www.soinside.com 2019 - 2024. All rights reserved.