Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Report: add_stat_label() can no longer be forced to not update dichotomous or categorical labels #1937

Closed
dereksonderegger opened this issue Aug 28, 2024 · 2 comments · Fixed by #1939

Comments

@dereksonderegger
Copy link
Contributor

The function add_stat_label() unfortunately affects all statistics and not just the ones that the user wants to update. This was particularly annoying for dichotomous and categorical variables because changing the labels in a continuous2 type would then force a label change to add the n (%) to everything else.

In previous versions of gtsummary, we were able to work around this by setting the new label to NA_character_ and the output would look like nothing had changed for the dichotomous and categorical variables. Unfortunately this trick no longer works in version 2.0.1.

tmp <- data.frame(
  A = c(TRUE, TRUE, TRUE, FALSE),
  B = c(TRUE, FALSE, FALSE, FALSE),
  C = rnorm(4),
  D = c('G1','G2','G1','G2')
)

# Default behavior for logical or categorical is to suppress the n (%)
# in favor of the footer
tmp |>
  gtsummary::tbl_summary(
    type = list(C ~ 'continuous2'),
    statistic = list(
      gtsummary::all_continuous2() ~ c('{N_obs}','{mean} ({sd})')
    )
  )

# When we add the custom statistics label for continuous2 variables
# the n (%) now shows up on in the variable labels
tmp |>
  gtsummary::tbl_summary(
    type = list(C ~ 'continuous2'),
    statistic = list(
      gtsummary::all_continuous2() ~ c('{N_obs}','{mean} ({sd})')
    )
  ) |>
  gtsummary::add_stat_label(
    list(
      gtsummary::all_continuous2() ~ c('Number of Subjects', 'Mean (Std Dev)')
    ),
    location='row'
  )


# In previous versions of gtsummary, the solution was to set
# the label for dichotomous or categorical variables to
#  `NA_character_`, but that no longer works
tmp |>
  gtsummary::tbl_summary(
    type = list(C ~ 'continuous2'),
    statistic = list(
      gtsummary::all_continuous2() ~ c('{N_obs}','{mean} ({sd})')
    )
  ) |>
  gtsummary::add_stat_label(
    list(
      gtsummary::all_continuous2() ~ c('Number of Subjects', 'Mean (Std Dev)'),
      gtsummary::all_dichotomous() ~ NA_character_
    ),
    location = 'row'
  )

# Passing an empty string sort-of works, but leaves an obnoxious
# comma 
tmp |>
  gtsummary::tbl_summary(
    type = list(C ~ 'continuous2'),
    statistic = list(
      gtsummary::all_continuous2() ~ c('{N_obs}','{mean} ({sd})')
    )
  ) |>
  gtsummary::add_stat_label(
    list(
      gtsummary::all_continuous2() ~ c('Number of Subjects', 'Mean (Std Dev)'),
      gtsummary::all_dichotomous() ~ ''
    ),
    location = 'row'
  )

Ideally, the merge would be acting more like gt::cols_merge() where the pattern cols_merge_pattern = "{label}, {stat_label}" could be replaced with cols_merge_pattern = "{label}<<, {stat_label}>>" and updated labels that are NA will work.

@ddsjoberg
Copy link
Owner

Thanks @dereksonderegger !

Issue should be solved in the dev version of the pkg. Can you confirm, please?

@dereksonderegger
Copy link
Contributor Author

Hi @ddsjoberg !

Thank you so much for responding to this. I've checked that the reprex example works and I've been successful using gtsummary version 2.0.1.9008 in my code stack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants