我正在嘗試通過 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
```
根據以下內容,輸出不適合 1 頁,并且我沒有得到包含剩余變數的另一頁:

在這個例子中,我如何確保表格被分成 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))
uj5u.com熱心網友回復:
這套西裝適合你嗎?
```{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"))
```
uj5u.com熱心網友回復:
@manro 在這里讓您走上正軌。訣竅是使用longtable=TRUE引數。
在幕后,modelsummary呼叫了kbl從功能kableExtra包產生的表。所有未明確使用的引數都通過省略號datasummary()自動推送到kbl函式...。這意味著您可以longtable直接在datasummary()呼叫中定義引數。例如:
datasummary(mpg hp ~ Mean SD, data = mtcars, longtable = TRUE)
以上是一個最小的例子。我嘗試longtable=TRUE在您更復雜的示例中以相同的方式傳遞引數,并且您的 Rmarkdown 檔案在我的機器上編譯沒有問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/351291.html
上一篇:回傳向量元素乘積函式的函式
下一篇:如何根據r中的條件重新編碼因子
