TysonStanley / furniture

The furniture R package contains table1 for publication-ready simple and stratified descriptive statistics, tableC for publication-ready correlation matrixes, and other tables #rstats
49 stars 7 forks source link

Labeling with Hmisc::label() #26

Open dylanrussellmd opened 4 years ago

dylanrussellmd commented 4 years ago

Is there anyway to replace the variable names in the output table with labels assigned to data frame columns by Hmisc::label()? The table1 package does this nicely.

TysonStanley commented 4 years ago

Great question. It should be possible using the ` in thetable1()` function. For example:

table1(
  data, 
  var1, var2, var3,
  var_names = Hmisc::label(
    select(data, var1, var2, var3)
  ))

This is a feature I could add to the package but won't have time for a bit.

dylanrussellmd commented 4 years ago

This works very well! I think it would be a great addition. The only label it does not change is the splitby variable.

dylanrussellmd commented 4 years ago

I have made a wrapper for your table1 function called tableone(). It converts logicals to factors with yes and no levels and allows the use of Hmisc::labeling. The only thing I can not get it to do yet is update the label of the splitby variable (in the example below, this would be cut).

tableone <- function(df, ..., group = NULL, test = TRUE) {
 args <- rlang::ensyms(...)
 group <- rlang::enexpr(group)

 change_to_factor <- function(col) {
   label <- Hmisc::label(col)
   col <- factor(col, levels = c(FALSE, TRUE), labels = c("No","Yes"))
   Hmisc::label(col) <- label
   return(col)
 }

 var_names <- function(df, ...) {
   Hmisc::label(dplyr::select(df, ...))
 }

  if(!is.null(group)) {
    df <- df %>%
      dplyr::select(!!!args, !!group) %>%
      dplyr::mutate(dplyr::across(where(is.logical), ~change_to_factor(.x)))
    rlang::eval_tidy(rlang::quo(furniture::table1(df, !!!args, splitby = ~!!group, test = TRUE, var_names = var_names(df, !!!args))))
  } else {
    df <- df %>% dplyr::select(!!!args) %>% dplyr::mutate(dplyr::across(where(is.logical), ~change_to_factor(.x)))
    rlang::eval_tidy(rlang::quo(furniture::table1(df, !!!args, var_names = var_names(df, !!!args))))
  }
}
diamonds <- ggplot2::diamonds
diamonds$expensive <- diamonds$price > 500
Hmisc::label(diamonds$depth) <- "Depth"
Hmisc::label(diamonds$table) <- "Table"
Hmisc::label(diamonds$price) <- "Price"
Hmisc::label(diamonds$clarity) <- "Clarity"
Hmisc::label(diamonds$cut) <- "Cut"
Hmisc::label(diamonds$expensive) <- "Expensive"
diamonds %>% tableone(depth, table, price, clarity, expensive, group = cut)
TysonStanley commented 4 years ago

Nice! I like this. Thanks for sharing! I may come back to this soon and think about how to implement this in the package, if you don't mind.

dylanrussellmd commented 4 years ago

Nice! I like this. Thanks for sharing! I may come back to this soon and think about how to implement this in the package, if you don't mind.

Of course! Hopefully you can figure out how to work in printing the label for the 'splitby' variable too.