rlatexsweavextable

Make a beautiful report on latex from R


I would like to produce a latex report with R code. I know that the subject has already been discussed many times, and I get lost between xtable, Hmisc, and packages like stargazer or reporttools.

I have a database containing about thirty variables (quantitative and qualitative), as well as a variable X (on the example TMA yes / no). I would like to produce a table containing the summary of each variable, the summary of the variable according to the binary variable X, as well as the appropriate test according to the variable X (Student's test for the quantitative variables and Fisher's for the qualitative variables).

At the end, I'll get this:

enter image description here

Below a code for the example:

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) 

It's been a week since I struggled to find a solution!

I will be grateful for the slightest help.


Solution

  • Try the tableone package to create a TableOne object and kable from the knitr package to create latex.

    library(tableone)
    library(knitr)
    
    example(CreateTableOne)
    p <- print(tableOne, printToggle = FALSE, noSpaces = TRUE)
    kable(p, format = "latex")
    

    giving:

    \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}