ddsjoberg / gtsummary

Presentation-Ready Data Summary and Analytic Result Tables
http://www.danieldsjoberg.com/gtsummary
Other
1.05k stars 121 forks source link

gtsummary Functions Not Working with dplyr Pipe Operator After Update #2025

Closed Elhadedy2018 closed 4 weeks ago

Elhadedy2018 commented 4 weeks ago

Hi there,

I've recently encountered an issue after updating the gtsummary package to the latest version. It seems that the gtsummary functions are no longer working with the dplyr pipe operator (%>%). Before the update, my code was functioning without any problems.

I've tried reinstalling both the dplyr and gtsummary packages several times and have restarted my R session multiple times, but the issue persists.

To help address this, I’ve created a reproducible example below. I would greatly appreciate any assistance in resolving this issue.

I really appreciate any help you can provide.


A code snippet

# Create a small example dataset similar to your structure
cleaned_data <- tibble::tribble(
  ~randomization, ~age, ~sex, ~bmi, ~bw, ~okd, ~comorbidities,
  "Placebo",      26,    "Female", 22.8, 62,   "Solitary Kidney",    "No Comorbidities",
  "Placebo",      69,    "Male",   30.4, 88,   "Unknown",            "Cardiac Disease",
  "Drug",         63,    "Male",   29.9, 78,   "Solitary Kidney",    "Cardiac Disease",
  "Drug",         57,    "Male",   32.7, 99,   "Unknown",            "Htn",
  "Drug",         34,    "Male",   40.0, 113,  "Htn Nephropathy",    "Htn",
  "Drug",         64,    "Male",   34.0, 110,  "Stone Kidney Disease","Htn",
  "Drug",         65,    "Male",   32.3, 88,   "Unknown",            "Htn",
  "Drug",         64,    "Male",   31.5, 90.9, "Solitary Kidney",    "Cardiac Disease",
  "Drug",         42,    "Female", 30.9, 87.2, "Unknown",            "Htn",
  "Placebo",      67,    "Male",   32.8, 88.3, "Unknown",            "Htn"
)

# Original code for table summary
table_1 <- cleaned_data %>% 
  select(randomization, age, sex, bmi, bw, okd, comorbidities) %>% 
  mutate(
    okd = case_when(
      okd == "Htn Nephropathy" ~ "Hypertensive Nephropathy",
      okd == "Pck" ~ "Polycystic Kidney Disease",
      TRUE ~ okd
    )
  ) %>% 
  mutate(
    comorbidities = case_when(
      comorbidities == "Htn" ~ "Hypertension",
      TRUE ~ comorbidities
    )
  ) %>% 
  tbl_summary(
    by  = randomization,
    statistic = list(
      all_continuous() ~ "{mean} ({sd})",
      all_categorical() ~ "{n} / {N} ({p}%)"
    ),
    digits = all_continuous() ~ 2,
    missing = "no",
    label = list(
      age ~ "Age",
      sex ~ "Sex",
      bmi ~ "Body mass index",
      bw ~ "Body weight",
      okd ~ "Original kidney disease",
      comorbidities ~ "Comorbidities"
    )
  ) %>% 
  add_p(all_continuous() ~ "t.test",
        pvalue_fun = ~ style_pvalue(.x, digits = 2)) %>% 
  bold_labels() %>% 
  modify_footnote( all_stat_cols() ~ "Mean (sd) or Frequency (%)") %>% 
  modify_caption("**Table 1. Demographic data**")
#> Error in cleaned_data %>% select(randomization, age, sex, bmi, bw, okd, : could not find function "%>%"
ddsjoberg commented 4 weeks ago

Dear @Elhadedy2018 ,

I can't replicate this error. Can you please update your code to use reprex (reprex.tidyrverse.com). This ensures all code runs in a fresh environment as well. if you are updating your packages, please keep this issue in mind as well (https://github.com/ddsjoberg/gtsummary/issues/2011). FYI a new version of gtsummary is going to CRAN today.

Elhadedy2018 commented 4 weeks ago

Sure, Thank you for the fast reply: Here is the full code with the error

library(dplyr)
#> Warning: package 'dplyr' was built under R version 4.4.1
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(gtsummary)
#> Warning: package 'gtsummary' was built under R version 4.4.1
library(tibble)

# Create a small example dataset similar to your structure
cleaned_data <- tibble::tribble(
  ~randomization, ~age, ~sex, ~bmi, ~bw, ~okd, ~comorbidities,
  "Placebo",      26,    "Female", 22.8, 62,   "Solitary Kidney",    "No Comorbidities",
  "Placebo",      69,    "Male",   30.4, 88,   "Unknown",            "Cardiac Disease",
  "Drug",         63,    "Male",   29.9, 78,   "Solitary Kidney",    "Cardiac Disease",
  "Drug",         57,    "Male",   32.7, 99,   "Unknown",            "Htn",
  "Drug",         34,    "Male",   40.0, 113,  "Htn Nephropathy",    "Htn",
  "Drug",         64,    "Male",   34.0, 110,  "Stone Kidney Disease","Htn",
  "Drug",         65,    "Male",   32.3, 88,   "Unknown",            "Htn",
  "Drug",         64,    "Male",   31.5, 90.9, "Solitary Kidney",    "Cardiac Disease",
  "Drug",         42,    "Female", 30.9, 87.2, "Unknown",            "Htn",
  "Placebo",      67,    "Male",   32.8, 88.3, "Unknown",            "Htn"
)

# The code that triggers the error
table_1 <- cleaned_data %>% 
  select(randomization, age, sex, bmi, bw, okd, comorbidities) %>% 
  mutate(
    okd = case_when(
      okd == "Htn Nephropathy" ~ "Hypertensive Nephropathy",
      okd == "Pck" ~ "Polycystic Kidney Disease",
      TRUE ~ okd
    )
  ) %>% 
  mutate(
    comorbidities = case_when(
      comorbidities == "Htn" ~ "Hypertension",
      TRUE ~ comorbidities
    )
  ) %>% 
  tbl_summary(
    by  = randomization,
    statistic = list(
      all_continuous() ~ "{mean} ({sd})",
      all_categorical() ~ "{n} / {N} ({p}%)"
    ),
    digits = all_continuous() ~ 2,
    missing = "no",
    label = list(
      age ~ "Age",
      sex ~ "Sex",
      bmi ~ "Body mass index",
      bw ~ "Body weight",
      okd ~ "Original kidney disease",
      comorbidities ~ "Comorbidities"
    )
  ) %>% 
  add_p(all_continuous() ~ "t.test",
        pvalue_fun = ~ style_pvalue(.x, digits = 2)) %>% 
  bold_labels() %>% 
  modify_footnote( all_stat_cols() ~ "Mean (sd) or Frequency (%)") %>% 
  modify_caption("**Table 1. Demographic data**")
#> Error in `dplyr::mutate()`:
#> ℹ In argument: `stat = as.character(...)`.
#> Caused by error in `glue_data()`:
#> ! is.environment(.envir) is not TRUE
Created on 2024-10-04 with [reprex v2.1.1](https://reprex.tidyverse.org/)
ddsjoberg commented 4 weeks ago

Ahh, I see; the error message changed when run in a separate environment.

This issue is being tracked here: https://github.com/ddsjoberg/gtsummary/issues/2011

Elhadedy2018 commented 3 weeks ago

Thank you so much for your fast reply, I really appreciate it.