R/tableaucomparatif.R

Defines functions tabcompph

Documented in tabcompph

#' Comparative table
#'
#' Write a table comparing some groups with one column per group (mean & sd or nb & percent) and one column for p-value
#' 
#' @param dfx data.frame  to explore
#' @param tri explicative variable
#' @param titre tittle of the table ("Tableau comparatif" by default)
#' @param lab label ("tabcomp" by default)
#' @param longt FALSE : short table, TRUE : longtable (0 by default)
#' @param export TRUE : export csv (FALSE by default)
#' @param correct TRUE : correct for to big number of tests (FALSE by default)
#' @param ka si TRUE : print via kable sinon xtable (TRUE  par defaut)
#'
#' @import stats
#'
#' @import dplyr
#' @import tidyverse
#' @import rlang
#' @import xtable
#' @import knitr
#' @import kableExtra
<<<<<<< HEAD
#' @return comparative table in laTeX +/- csv
=======
#' @return comparative table in laTeX or html +/- csv
>>>>>>> 42b20b896837efe579944162827a68963584f331
#'
#' @example tabcompph(dfx = iris, tri = Species, ka = TRUE, correct = FALSE)
#'
#' @export
tabcompph <- function(dfx, tri, 
                      titre = "Tableau comparatif", 
                      lab = "tabcomp",
                      longt = FALSE, 
                      export = FALSE, 
                      correct = FALSE, 
                      ka = TRUE){
  #On supprime les données manquantes dans la variable de tri
  #dfx <- dfx %>% 
    #filter(!is.na({{tri}}))
  #
  tabx <- NULL
  trix <- enquo(tri)
  vv <- quo_name(trix)
  triz <- dfx[vv]
  triz <- triz[[1]]
  for (ll in 1:length(dfx)){
    varx <- dfx[,ll]
    # varx <- varx[[1]]
    nom <- names(dfx)[ll]
    if (nom != vv){
      if (is.numeric(varx)){ # Variables numériques
        print(length(varx))
        lig <- lignum(nom,varx,triz, kk = ka)
        tabx <- rbind(tabx,lig)
      } else { # Variables factorielles
        lig <- ligff(nom,varx,triz, kk = ka)
        tabx <- rbind(tabx,lig)
      }
    }
  }
  # Export
  if (export) {
    nomcsv <- paste0(titre,"_export_comparatif.csv")
    write.csv(tabx,nomcsv)
  }
  # Création tableaux
  ltit <- c(" ",levels(triz),"p")
  if (ka){
    kable(tabx, 
          row.names = FALSE, 
          col.names = ltit, 
          caption = titre, 
          label = lab, 
          escape = FALSE)
  } %>% 
    kable_styling(bootstrap_options = "striped", full_width = FALSE,
                  position = "center")
<<<<<<< HEAD
  else{
  if (longt == FALSE) {
    xx <- xtable(as.matrix(tabx), caption = titre, label = lab)
    colnames(xx) <- ltit
    print(xx,
          include.colnames = TRUE,
          floating = TRUE,
          booktabs = TRUE,
          include.rownames = FALSE,
          sanitize.text.function = function(x){x}
    )
  }
=======
>>>>>>> 42b20b896837efe579944162827a68963584f331
  else{
    if (longt == FALSE) {
      xx <- xtable(as.matrix(tabx), caption = titre, label = lab)
      colnames(xx) <- ltit
      print(xx,
            include.colnames = TRUE,
            floating = TRUE,
            booktabs = TRUE,
            include.rownames = FALSE,
            sanitize.text.function = function(x){x}
      )
    }
    else{
      xx <- xtable(as.matrix(tabx))
      ttit <- paste0("}&\\text{",levels(triz),collapse = "")
      ttit <- paste0("{ ",ttit,"}&p\\\\",collapse = "")
      print(xx,
            align = "lccc",
            tabular.environment = 'longtable',
            include.colnames = FALSE,
            floating = FALSE,
            booktabs = TRUE,
            hline.after = -1,
            include.rownames = FALSE,
            sanitize.text.function = function(x){x},
            add.to.row = list(pos = list(0),
                              command = paste0(ttit,"
                                             \\midrule
                                             \\endfirsthead
                                              "
                                               ,ttit,
                                               "
                                             \\endhead
                                             \\bottomrule
                                             \\endfoot
                                             \\bottomrule
                                             \\caption{",titre,"}
                                             \\label{",lab,"}
                                             \\endlastfoot
                                             ")))
    }
  }
}
philippemichel/thesisph documentation built on Dec. 22, 2020, 11:08 a.m.