如何使用 datasummary 在 R markdown 中将 table 分成 2 页?
how to split table into 2 pages in R markdown using datasummary?
我正在尝试通过 R Markdown 使用 datasummary 创建一个 table。然而,我的 table 很长,有很多变量。请参阅下面的示例代码:
---
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
```
输出不适合下面的 1 页,我没有得到包含剩余变量的另一页:
如何确保在这个例子中 table 被分成 2 个,移动到标题为“我的 table 的标题”的下一页?(续)?
我检查了 tables
package vignette 并通过长时间包含以下信息 table 得到了相同的输出:
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}
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"))
```
@manro 在这里让你走上了正确的轨道。诀窍是使用 longtable=TRUE
参数。
在幕后,modelsummary
调用 the kbl
function from the kableExtra
package 生成 table。 datasummary()
明确使用的 而非 的所有参数都会通过 ...
省略号自动推送到 kbl
函数。这意味着您可以直接在 datasummary()
调用中定义 longtable
参数。例如:
datasummary(mpg + hp ~ Mean + SD, data = mtcars, longtable = TRUE)
以上是一个最小的例子。我尝试在您更复杂的示例中以相同的方式传递 longtable=TRUE
参数,并且您的 Rmarkdown 文档在我的机器上编译没有问题。
我正在尝试通过 R Markdown 使用 datasummary 创建一个 table。然而,我的 table 很长,有很多变量。请参阅下面的示例代码:
---
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
```
输出不适合下面的 1 页,我没有得到包含剩余变量的另一页:
如何确保在这个例子中 table 被分成 2 个,移动到标题为“我的 table 的标题”的下一页?(续)?
我检查了 tables
package vignette 并通过长时间包含以下信息 table 得到了相同的输出:
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}
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"))
```
@manro 在这里让你走上了正确的轨道。诀窍是使用 longtable=TRUE
参数。
在幕后,modelsummary
调用 the kbl
function from the kableExtra
package 生成 table。 datasummary()
明确使用的 而非 的所有参数都会通过 ...
省略号自动推送到 kbl
函数。这意味着您可以直接在 datasummary()
调用中定义 longtable
参数。例如:
datasummary(mpg + hp ~ Mean + SD, data = mtcars, longtable = TRUE)
以上是一个最小的例子。我尝试在您更复杂的示例中以相同的方式传递 longtable=TRUE
参数,并且您的 Rmarkdown 文档在我的机器上编译没有问题。