tabcx <- function(dfx,
                   tri,
                   nomv,
                   titre = "Tableau comparatif",
                   lab = "tabcomp",
                   export = FALSE) {
  # On supprime les données manquantes dans la variable de tri
  dfx <- as_tibble(dfx)
  dfx <- dfx %>%
    dplyr::filter(!is.na({{tri}}))
  var2 <- "+ age+profession, data = tt, family = binomial)"
  #
  tabx <- NULL
  nlig <- 0
  ligd <- NULL
  trix <- enquo(tri)
  vv <- quo_name(trix)
  triz <- dfx[vv]
  triz <- triz[[1]]
  for (ll in seq_len(length(dfx))) {
    varx <- dfx[, ll]
    varx <- varx[[1]]
    if (names(dfx)[ll] != vv) {
      nom <- paste0("<b>", nomv[ll], "</b>")
      var1 <-  paste0("glm(",vv,"~")
      aa <- paste0(var1,names(dfx)[ll],var2)
      ll <- eval(parse(text = aa))
      dd <- logistic.display(ll, simplified = TRUE)
      dd <- dd$table
      if (is.numeric(varx)) {
        # Variables numériques
        lig <- lignumc(nom, varx, triz)
        lig <- c(lig,beaup(dd[1,4]))
        tabx <- rbind(tabx, lig)
        nlig <- nlig + 1
      } else {
        # Variables factorielles
        if(is.factor(varx)==FALSE){
          varx <- as.factor(varx)
        }
        lig <- ligfcx(nom, varx, triz,beaup(dd[1,4]))
        #print(lig)
        #lig <- c(lig,"*")
        tabx <- rbind(tabx, lig)
        llx <- length(levels(varx))
        nlig <- nlig + 1 + llx
        ligd <- c(ligd, seq(nlig - llx + 1, nlig))
      }
    }
  }
  # Export
  if (export) {
    nomcsv <- paste0(titre, "_export_comparatif.csv")
    write.csv(tabx, nomcsv)
  }
  # Création tableaux
  ltit <- c(" ", levels(triz), "p brut", "p ajusté")
  tabf <-   kable(
    tabx,
    row.names = FALSE,
    col.names = ltit,
    caption = titre,
    label = lab,
    escape = FALSE
  ) %>%
    kable_styling(
      bootstrap_options = "striped",
      full_width = FALSE,
      position = "center"
    )
  
  if (!is.null(ligd)) {add_indent(tabf, ligd)}
  tabf %>%
    scroll_box(width = "100%", height = "850px")

}    





ligfcx <- function(nom, varp, trip ,bdd) {
  tabp <- table(varp, trip)
  chip <- chisq.test(tabp, correct = TRUE)[[3]]
  chip <- chisq.test(tabp, correct = FALSE)
  if (min(chip$expected)<5){chip <- chisq.test(tabp, correct = TRUE)}
  chip <- chip[[3]]
  if (is.na(chip)){
    chip <- fisher.test(tabp)[[1]]
  }
  chip <- beaup(chip)
  ltri <- length(levels(trip))
  lvar <- length(levels(varp))
  tabg <- c(nom, rep(" ", ltri), chip,bdd)
  #
  ss <- colSums(tabp)
  for (lig in 1:lvar) {
    ligt <- levels(varp)[lig] 
    for (cas in 1:ltri) {
      casx <- tabp[lig, cas]
      casp <- round(100*casx/ss[cas],1)
      cast <- paste0(casx,"/",ss[cas]," (",casp," %)" )
      ligt <- c(ligt,cast)
    }
    ligt <- c(ligt, " ", " ")
    tabg <- rbind(tabg, ligt)
    print(tabg)
  }
  return(tabg)
}