0

I want to create a function that would automatically generate the tables with summary statistics when i parse different column names. I am trying to create a function for gtsummary I have tried enquo and deparse but both don't seem to help. Can somebody please guide me in what I am doing wrong here.

get_stats <- function (var2) {
var2 <- dplyr::enquo(var2)
grp_val <- deparse(substitute(var2))
 df %>% 
   gtsummary::tbl_summary(.,
      by = trt,
      missing = "no",
      type =
        list(!!var2 ~ "continuous2"),
      statistic = list(
        "{{var2}}" = c(
          "{N_nonmiss}",
          "{mean} ({sd})",
          "{median} ({p25}, {p75})",
          "{min}, {max}"
        )
      )
  ,
      digits = !!var2 ~ c(0, 1, 1, 1)
    )
}

The error I keep getting is Error: Error in type= argument input. Select from ‘age’, ‘trt’.

When I use this with the trial data without parsing anything it works fine.

trial %>% 
  dplyr::select(age, trt) %>% 
    dplyr::mutate_if(is.factor, as.character()) %>%
    gtsummary::tbl_summary(
                           by = trt,
                           missing = "no",
                           type =
                             list(age ~ "continuous2"),
  statistic = list(
    "age" = c(
      "{N_nonmiss}",
      "{mean} ({sd})",
      "{median} ({p25}, {p75})",
      "{min}, {max}"
    ))
    ,
    digits = age ~ c(0, 1, 1, 1)
  )

Expected output from the code

2
  • 1
    Could you please share some reproducible data using dput()? Commented Apr 20, 2022 at 18:37
  • I edited with the trial data, and added an expected output, does that help ? Also, I am not familiar with dput() Commented Apr 20, 2022 at 19:03

1 Answer 1

1

Using rlang::as_name and named lists you could do:

library(gtsummary)

get_stats <- function(df, var2) {
  var2_str <- rlang::as_name(rlang::enquo(var2))
  df %>%
    gtsummary::tbl_summary(.,
      by = trt,
      missing = "no",
      type = setNames(list(c("continuous2")), var2_str),
      statistic = setNames(list(c(
          "{N_nonmiss}",
          "{mean} ({sd})",
          "{median} ({p25}, {p75})",
          "{min}, {max}"
        )), var2_str
      ),
      digits = setNames(list(c(0, 1, 1, 1)), var2_str),
    )
}

trial %>%
  select(age, trt) %>%
  dplyr::mutate_if(is.factor, as.character()) %>%
  get_stats(age)

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

The solution worked! The only issue I am having is with one statement where this fails to work gtsummary::remove_row_type(setNames(list(c("header")), var2_str),)
The API for gtsummary::remove_row_type is different. Here we don't need a named list but could do %>% gtsummary::remove_row_type({{var2}}, type = "header"). Note however that in your example this has no effect. ):
I saw that we can not remove the rows for continuous variables but can remove it for categorical. Not sure if it is a bug or if it was intentional.
Also not sure if there is a way to remove the row, because my final output does not need the row. Perhaps by converting into a gt object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.