对R中的乳胶做出漂亮的报告

时间:2018-07-25 08:45:24

标签: r latex sweave xtable

我想用latex代码生成一个R报告。 我知道这个主题已经讨论了很多次了,我在xtableHmiscstargazerreporttools之类的软件包之间迷路了。

我有一个包含大约三十个变量(定量和定性)以及变量X(在示例TMA中为是/否)的数据库。 我想生成一个表,其中包含每个变量的摘要,根据二进制变量X进行的变量摘要,以及根据变量X进行的适当检验(对于定量变量的学生检验,对于定性变量的Fisher检验)。

最后,我会得到这个:

enter image description here

该示例的代码下方:

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) 

距离我努力寻找解决方案已经过去了一个星期!

我将非常感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

尝试使用tableOne包创建TableOne对象,并从knitr包中kable创建乳胶。

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}

答案 1 :(得分:0)

这是对 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}