用 R 做一个漂亮的 Latex 报告
Make a beautiful report on latex from R
我想用 R
代码生成一份 latex
报告。
我知道这个主题已经被讨论过很多次了,我在 xtable
、Hmisc
和 stargazer
或 reporttools
.
等包之间迷失了方向
我有一个数据库,其中包含大约 30 个变量(定量和定性),以及一个变量 X(在示例 TMA 是/否)。
我想生成一个 table 包含每个变量的摘要,根据二进制变量 X 的变量摘要,以及根据变量 X 的适当测试(学生对定量变量的测试和 Fisher's对于定性变量)。
最后,我会得到这个:
示例代码如下:
AAA = matrix (data = c (rnorm (10), round (runif (10, min = 1, max = 3), digits = 0), rbinom (10,1,0.5)), nrow = 10, byrow = F)
AAA = as.data.frame (AAA); AAA $ V2 = as.factor (AAA $ V2)
我已经一个星期没找到解决方案了!
如有丝毫帮助,我将不胜感激。
尝试使用 tableone 包创建一个 TableOne
对象,并使用 knitr 包 kable
创建 latex。
library(tableone)
library(knitr)
example(CreateTableOne)
p <- print(tableOne, printToggle = FALSE, noSpaces = TRUE)
kable(p, format = "latex")
给予:
\begin{tabular}{l|l|l|l|l}
\hline
& 1 & 2 & p & test\
\hline
n & 158 & 154 & & \
\hline
time (mean (sd)) & 2015.62 (1094.12) & 1996.86 (1155.93) & 0.883 & \
\hline
status (\%) & & & 0.894 & \
\hline
0 & 83 (52.5) & 85 (55.2) & & \
\hline
1 & 10 (6.3) & 9 (5.8) & & \
\hline
2 & 65 (41.1) & 60 (39.0) & & \
\hline
age (mean (sd)) & 51.42 (11.01) & 48.58 (9.96) & 0.018 & \
\hline
sex = f (\%) & 137 (86.7) & 139 (90.3) & 0.421 & \
\hline
ascites = 1 (\%) & 14 (8.9) & 10 (6.5) & 0.567 & \
\hline
hepato = 1 (\%) & 73 (46.2) & 87 (56.5) & 0.088 & \
\hline
spiders = 1 (\%) & 45 (28.5) & 45 (29.2) & 0.985 & \
\hline
edema (\%) & & & 0.877 & \
\hline
0 & 132 (83.5) & 131 (85.1) & & \
\hline
0.5 & 16 (10.1) & 13 (8.4) & & \
\hline
1 & 10 (6.3) & 10 (6.5) & & \
\hline
bili (mean (sd)) & 2.87 (3.63) & 3.65 (5.28) & 0.131 & \
\hline
chol (mean (sd)) & 365.01 (209.54) & 373.88 (252.48) & 0.748 & \
\hline
albumin (mean (sd)) & 3.52 (0.44) & 3.52 (0.40) & 0.874 & \
\hline
copper (mean (sd)) & 97.64 (90.59) & 97.65 (80.49) & 0.999 & \
\hline
alk.phos (mean (sd)) & 2021.30 (2183.44) & 1943.01 (2101.69) & 0.747 & \
\hline
ast (mean (sd)) & 120.21 (54.52) & 124.97 (58.93) & 0.460 & \
\hline
trig (mean (sd)) & 124.14 (71.54) & 125.25 (58.52) & 0.886 & \
\hline
platelet (mean (sd)) & 258.75 (100.32) & 265.20 (90.73) & 0.555 & \
\hline
protime (mean (sd)) & 10.65 (0.85) & 10.80 (1.14) & 0.197 & \
\hline
stage (\%) & & & 0.201 & \
\hline
1 & 12 (7.6) & 4 (2.6) & & \
\hline
2 & 35 (22.2) & 32 (20.8) & & \
\hline
3 & 56 (35.4) & 64 (41.6) & & \
\hline
4 & 55 (34.8) & 54 (35.1) & & \
\hline
\end{tabular}
这是对 G.Grothendieck 的回答的补充 - 现在有 kableExtra
包,它提供了强大的表格布局功能,特别是主题。
水平和垂直规则的纯乳胶输出将不再存在。
see this vignette for all options
library(tableone)
library(kableExtra)
# directly with a the tableone wrapper for the kable function
kableone(tableOne, booktabs = TRUE, format = "latex")
# or, to give more flexibility for the tableOne object, print first
# E.g., if you want to use labels for your rows.
p <- print(tableOne)
kbl(p, booktabs = TRUE, format = "latex")
输出
\begin{tabular}[t]{lllll}
\toprule
& 1 & 2 & p & test\
\midrule
n & 158 & 154 & & \
time (mean (SD)) & 2015.62 (1094.12) & 1996.86 (1155.93) & 0.883 & \
status (\%) & & & 0.894 & \
0 & 83 (52.5) & 85 (55.2) & & \
1 & 10 ( 6.3) & 9 ( 5.8) & & \
\addlinespace
2 & 65 (41.1) & 60 (39.0) & & \
age (mean (SD)) & 51.42 (11.01) & 48.58 (9.96) & 0.018 & \
sex = f (\%) & 137 (86.7) & 139 (90.3) & 0.421 & \
ascites = 1 (\%) & 14 ( 8.9) & 10 ( 6.5) & 0.567 & \
hepato = 1 (\%) & 73 (46.2) & 87 (56.5) & 0.088 & \
\addlinespace
spiders = 1 (\%) & 45 (28.5) & 45 (29.2) & 0.985 & \
edema (\%) & & & 0.877 & \
0 & 132 (83.5) & 131 (85.1) & & \
0.5 & 16 (10.1) & 13 ( 8.4) & & \
1 & 10 ( 6.3) & 10 ( 6.5) & & \
\addlinespace
bili (mean (SD)) & 2.87 (3.63) & 3.65 (5.28) & 0.131 & \
chol (mean (SD)) & 365.01 (209.54) & 373.88 (252.48) & 0.748 & \
albumin (mean (SD)) & 3.52 (0.44) & 3.52 (0.40) & 0.874 & \
copper (mean (SD)) & 97.64 (90.59) & 97.65 (80.49) & 0.999 & \
alk.phos (mean (SD)) & 2021.30 (2183.44) & 1943.01 (2101.69) & 0.747 & \
\addlinespace
ast (mean (SD)) & 120.21 (54.52) & 124.97 (58.93) & 0.460 & \
trig (mean (SD)) & 124.14 (71.54) & 125.25 (58.52) & 0.886 & \
platelet (mean (SD)) & 258.75 (100.32) & 265.20 (90.73) & 0.555 & \
protime (mean (SD)) & 10.65 (0.85) & 10.80 (1.14) & 0.197 & \
stage (\%) & & & 0.201 & \
\addlinespace
1 & 12 ( 7.6) & 4 ( 2.6) & & \
2 & 35 (22.2) & 32 (20.8) & & \
3 & 56 (35.4) & 64 (41.6) & & \
4 & 55 (34.8) & 54 (35.1) & & \
\bottomrule
\end{tabular}
我想用 R
代码生成一份 latex
报告。
我知道这个主题已经被讨论过很多次了,我在 xtable
、Hmisc
和 stargazer
或 reporttools
.
我有一个数据库,其中包含大约 30 个变量(定量和定性),以及一个变量 X(在示例 TMA 是/否)。 我想生成一个 table 包含每个变量的摘要,根据二进制变量 X 的变量摘要,以及根据变量 X 的适当测试(学生对定量变量的测试和 Fisher's对于定性变量)。
最后,我会得到这个:
示例代码如下:
AAA = matrix (data = c (rnorm (10), round (runif (10, min = 1, max = 3), digits = 0), rbinom (10,1,0.5)), nrow = 10, byrow = F)
AAA = as.data.frame (AAA); AAA $ V2 = as.factor (AAA $ V2)
我已经一个星期没找到解决方案了!
如有丝毫帮助,我将不胜感激。
尝试使用 tableone 包创建一个 TableOne
对象,并使用 knitr 包 kable
创建 latex。
library(tableone)
library(knitr)
example(CreateTableOne)
p <- print(tableOne, printToggle = FALSE, noSpaces = TRUE)
kable(p, format = "latex")
给予:
\begin{tabular}{l|l|l|l|l}
\hline
& 1 & 2 & p & test\
\hline
n & 158 & 154 & & \
\hline
time (mean (sd)) & 2015.62 (1094.12) & 1996.86 (1155.93) & 0.883 & \
\hline
status (\%) & & & 0.894 & \
\hline
0 & 83 (52.5) & 85 (55.2) & & \
\hline
1 & 10 (6.3) & 9 (5.8) & & \
\hline
2 & 65 (41.1) & 60 (39.0) & & \
\hline
age (mean (sd)) & 51.42 (11.01) & 48.58 (9.96) & 0.018 & \
\hline
sex = f (\%) & 137 (86.7) & 139 (90.3) & 0.421 & \
\hline
ascites = 1 (\%) & 14 (8.9) & 10 (6.5) & 0.567 & \
\hline
hepato = 1 (\%) & 73 (46.2) & 87 (56.5) & 0.088 & \
\hline
spiders = 1 (\%) & 45 (28.5) & 45 (29.2) & 0.985 & \
\hline
edema (\%) & & & 0.877 & \
\hline
0 & 132 (83.5) & 131 (85.1) & & \
\hline
0.5 & 16 (10.1) & 13 (8.4) & & \
\hline
1 & 10 (6.3) & 10 (6.5) & & \
\hline
bili (mean (sd)) & 2.87 (3.63) & 3.65 (5.28) & 0.131 & \
\hline
chol (mean (sd)) & 365.01 (209.54) & 373.88 (252.48) & 0.748 & \
\hline
albumin (mean (sd)) & 3.52 (0.44) & 3.52 (0.40) & 0.874 & \
\hline
copper (mean (sd)) & 97.64 (90.59) & 97.65 (80.49) & 0.999 & \
\hline
alk.phos (mean (sd)) & 2021.30 (2183.44) & 1943.01 (2101.69) & 0.747 & \
\hline
ast (mean (sd)) & 120.21 (54.52) & 124.97 (58.93) & 0.460 & \
\hline
trig (mean (sd)) & 124.14 (71.54) & 125.25 (58.52) & 0.886 & \
\hline
platelet (mean (sd)) & 258.75 (100.32) & 265.20 (90.73) & 0.555 & \
\hline
protime (mean (sd)) & 10.65 (0.85) & 10.80 (1.14) & 0.197 & \
\hline
stage (\%) & & & 0.201 & \
\hline
1 & 12 (7.6) & 4 (2.6) & & \
\hline
2 & 35 (22.2) & 32 (20.8) & & \
\hline
3 & 56 (35.4) & 64 (41.6) & & \
\hline
4 & 55 (34.8) & 54 (35.1) & & \
\hline
\end{tabular}
这是对 G.Grothendieck 的回答的补充 - 现在有 kableExtra
包,它提供了强大的表格布局功能,特别是主题。
水平和垂直规则的纯乳胶输出将不再存在。
see this vignette for all options
library(tableone)
library(kableExtra)
# directly with a the tableone wrapper for the kable function
kableone(tableOne, booktabs = TRUE, format = "latex")
# or, to give more flexibility for the tableOne object, print first
# E.g., if you want to use labels for your rows.
p <- print(tableOne)
kbl(p, booktabs = TRUE, format = "latex")
输出
\begin{tabular}[t]{lllll}
\toprule
& 1 & 2 & p & test\
\midrule
n & 158 & 154 & & \
time (mean (SD)) & 2015.62 (1094.12) & 1996.86 (1155.93) & 0.883 & \
status (\%) & & & 0.894 & \
0 & 83 (52.5) & 85 (55.2) & & \
1 & 10 ( 6.3) & 9 ( 5.8) & & \
\addlinespace
2 & 65 (41.1) & 60 (39.0) & & \
age (mean (SD)) & 51.42 (11.01) & 48.58 (9.96) & 0.018 & \
sex = f (\%) & 137 (86.7) & 139 (90.3) & 0.421 & \
ascites = 1 (\%) & 14 ( 8.9) & 10 ( 6.5) & 0.567 & \
hepato = 1 (\%) & 73 (46.2) & 87 (56.5) & 0.088 & \
\addlinespace
spiders = 1 (\%) & 45 (28.5) & 45 (29.2) & 0.985 & \
edema (\%) & & & 0.877 & \
0 & 132 (83.5) & 131 (85.1) & & \
0.5 & 16 (10.1) & 13 ( 8.4) & & \
1 & 10 ( 6.3) & 10 ( 6.5) & & \
\addlinespace
bili (mean (SD)) & 2.87 (3.63) & 3.65 (5.28) & 0.131 & \
chol (mean (SD)) & 365.01 (209.54) & 373.88 (252.48) & 0.748 & \
albumin (mean (SD)) & 3.52 (0.44) & 3.52 (0.40) & 0.874 & \
copper (mean (SD)) & 97.64 (90.59) & 97.65 (80.49) & 0.999 & \
alk.phos (mean (SD)) & 2021.30 (2183.44) & 1943.01 (2101.69) & 0.747 & \
\addlinespace
ast (mean (SD)) & 120.21 (54.52) & 124.97 (58.93) & 0.460 & \
trig (mean (SD)) & 124.14 (71.54) & 125.25 (58.52) & 0.886 & \
platelet (mean (SD)) & 258.75 (100.32) & 265.20 (90.73) & 0.555 & \
protime (mean (SD)) & 10.65 (0.85) & 10.80 (1.14) & 0.197 & \
stage (\%) & & & 0.201 & \
\addlinespace
1 & 12 ( 7.6) & 4 ( 2.6) & & \
2 & 35 (22.2) & 32 (20.8) & & \
3 & 56 (35.4) & 64 (41.6) & & \
4 & 55 (34.8) & 54 (35.1) & & \
\bottomrule
\end{tabular}