kaz-yos / tableone

R package to create "Table 1", description of baseline characteristics with or without propensity score weighting
https://cran.r-project.org/web/packages/tableone/index.html
218 stars 41 forks source link

Feature request allow print options in call to CreateTableOne #79

Closed tjebo closed 3 years ago

tjebo commented 3 years ago

Thanks for the great package. I find it very unintuitive to first create the object and then modify the output in print. Mostly in order to use labels for the variables. Why not adding ... and then print instead of return? (see below suggestion)

I am happy to create a pull request if you should like the idea...

library(tableone)
labelled::var_label(iris$Species) <- "That's an awesome label"

CreateTableOne2 <- function (vars, strata, data, factorVars, includeNA = FALSE, 
          test = TRUE, testApprox = chisq.test, argsApprox = list(correct = TRUE), 
          testExact = fisher.test, argsExact = list(workspace = 2 * 
                                                      10^5), testNormal = oneway.test, argsNormal = list(var.equal = TRUE), 
          testNonNormal = kruskal.test, argsNonNormal = list(NULL), 
          smd = TRUE, addOverall = FALSE, ...) 
{
  tableone:::ModuleStopIfNotDataFrame(data)
  if (missing(vars)) {
    vars <- names(data)
  }
  vars <- tableone:::ModuleReturnVarsExist(vars, data)
  tableone:::ModuleStopIfNoVarsLeft(vars)
  varLabels <- labelled::var_label(data[vars])
  if (!missing(factorVars)) {
    factorVars <- tableone:::ModuleReturnVarsExist(factorVars, data)
    data[factorVars] <- lapply(data[factorVars], factor)
  }
  test <- tableone:::ModuleReturnFalseIfNoStrata(strata, test)
  smd <- tableone:::ModuleReturnFalseIfNoStrata(strata, smd)
  percentMissing <- tableone:::ModulePercentMissing(data[vars])
  varClasses <- lapply(data[vars], class)
  varFactors <- sapply(varClasses, function(VEC) {
    any(VEC %in% c("factor", "ordered", "logical", "character"))
  })
  varFactors <- names(varFactors)[varFactors]
  varNumerics <- sapply(varClasses, function(VEC) {
    any(VEC %in% c("numeric", "integer", "double"))
  })
  varNumerics <- names(varNumerics)[varNumerics]
  varDrop <- setdiff(vars, c(varFactors, varNumerics))
  if (length(varDrop) > 0) {
    warning("Dropping variable(s) ", paste0(varDrop, sep = " "), 
            " due to unsupported class.\n")
    vars <- setdiff(vars, varDrop)
  }
  logiFactors <- vars %in% varFactors
  argsCreateContTable <- list(data = data, test = test, testNormal = testNormal, 
                              argsNormal = argsNormal, testNonNormal = testNonNormal, 
                              argsNonNormal = argsNonNormal, smd = smd, addOverall = addOverall)
  argsCreateCatTable <- list(data = data, includeNA = includeNA, 
                             test = test, testApprox = testApprox, argsApprox = argsApprox, 
                             testExact = testExact, argsExact = argsExact, smd = smd, 
                             addOverall = addOverall)
  if (!missing(strata)) {
    dfStrata <- tableone:::ModuleReturnStrata(strata, data)
    strata <- names(dfStrata)
    argsCreateContTable <- c(list(strata = strata), argsCreateContTable)
    argsCreateCatTable <- c(list(strata = strata), argsCreateCatTable)
  }
  if (length(varNumerics) == 0) {
    ContTable <- NULL
    CatTable <- do.call(CreateCatTable, args = c(list(vars = varFactors), 
                                                 argsCreateCatTable))
  }
  else if (length(varFactors) == 0) {
    ContTable <- do.call(CreateContTable, args = c(list(vars = varNumerics), 
                                                   argsCreateContTable))
    CatTable <- NULL
  }
  else if ((length(varFactors) > 0) & (length(varNumerics) > 
                                       0)) {
    ContTable <- do.call(CreateContTable, args = c(list(vars = varNumerics), 
                                                   argsCreateContTable))
    CatTable <- do.call(CreateCatTable, args = c(list(vars = varFactors), 
                                                 argsCreateCatTable))
  }
  else {
    warning("No variables left to analyzed in vars.")
  }
  TableOneObject <- list(ContTable = ContTable, CatTable = CatTable, 
                         MetaData = list(vars = vars, logiFactors = logiFactors, 
                                         varFactors = varFactors, varNumerics = varNumerics, 
                                         percentMissing = percentMissing, varLabels = varLabels))
  class(TableOneObject) <- "TableOne"
  print(TableOneObject, ...)
}
CreateTableOne2(data = iris, vars= "Species", varLabels = TRUE)
#>                              
#>                               Overall    
#>   n                           150        
#>   That's an awesome label (%)            
#>      setosa                    50 (33.3) 
#>      versicolor                50 (33.3) 
#>      virginica                 50 (33.3)

Created on 2021-05-12 by the reprex package (v2.0.0)

kaz-yos commented 3 years ago

We probably have a philosophical difference here. I will not consider this PR.