Skip to content

Commit 418a262

Browse files
254 tblsummary pattern (#328)
* in progress * in progress * updates to tidyselect allowing for more flexible inputs * NSE for add_p and tbl_uvregression * Update tbl_uvregression.R * Update tbl_regression.R * making all inputs accept bare/tidyselect notation * making the selectors totally tidy compatible * updated argument name for internal select function * documentation update using tidyselect/symbol inputs * deprecation note update * Update test-select_helpers.R * in progress * in progress * first draft complete * first draft of inline_text pattern update for tbl_summary * Update .covrignore * removed functions no longer in use * fixing edge cases for summarizing data that resulted in errors * removing references to ..continuous.. and ..categorical.. * in progress * small tweaks to NSE inputs * select for tbl_regression complete * updated unit tests, error messaging, and input checks * dep version updates * deleting commented out code * #300 improved error messaging * bug fix to new include/exclude notation over terms * doc update to inline_text.tbl_summary * documentation update * Update NEWS.md * Update _pkgdown.yml * version number incremented * NEWS update * added return for when an input resolves to NULL Co-authored-by: Margaret Hannum <[email protected]>
1 parent 5be5c57 commit 418a262

23 files changed

+782
-955
lines changed

.covrignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
R/deprecated.R
2+
R/print.R

DESCRIPTION

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: gtsummary
22
Title: Presentation-Ready Data Summary and Analytic Result
33
Tables
4-
Version: 1.2.4.9002
4+
Version: 1.2.4.9003
55
Authors@R:
66
c(person(given = "Daniel D.",
77
family = "Sjoberg",
@@ -48,32 +48,32 @@ BugReports: https://github.com/ddsjoberg/gtsummary/issues
4848
Depends:
4949
R (>= 3.5)
5050
Imports:
51-
broom (>= 0.5.1),
52-
broom.mixed (>= 0.2.3),
51+
broom (>= 0.5.3),
52+
broom.mixed (>= 0.2.4),
5353
crayon (>= 1.3.4),
54-
dplyr (>= 0.7.8),
55-
glue (>= 1.3.0),
56-
knitr (>= 1.21),
54+
dplyr (>= 0.8.3),
55+
glue (>= 1.3.1),
56+
knitr (>= 1.26),
5757
lifecycle (>= 0.1.0),
5858
magrittr (>= 1.5),
59-
purrr (>= 0.3.0),
60-
rlang (>= 0.3.1),
59+
purrr (>= 0.3.3),
60+
rlang (>= 0.4.2),
6161
stringr (>= 1.4.0),
6262
survival,
63-
tibble (>= 2.0.1),
63+
tibble (>= 2.1.3),
6464
tidyr (>= 1.0.0),
6565
tidyselect (>= 0.2.5),
6666
usethis (>= 1.5.1)
6767
Suggests:
68-
car (>= 3.0.2),
69-
covr (>= 3.2.1),
70-
geepack (>= 1.2.1),
71-
ggplot2 (>= 3.1.0),
72-
Hmisc (>= 4.2.0),
73-
lme4 (>= 1.1.18.1),
74-
rmarkdown (>= 1.11),
75-
spelling (>= 2.0),
76-
testthat (>= 2.1.0),
68+
car (>= 3.0.6),
69+
covr (>= 3.4.0),
70+
geepack (>= 1.3.1),
71+
ggplot2 (>= 3.2.1),
72+
Hmisc (>= 4.3.0),
73+
lme4 (>= 1.1.21),
74+
rmarkdown (>= 2.0),
75+
spelling (>= 2.1),
76+
testthat (>= 2.3.1),
7777
forcats (>= 0.4.0)
7878
Enhances:
7979
gt (>= 0.1.0)

NAMESPACE

+3
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ importFrom(knitr,knit_print)
132132
importFrom(lifecycle,deprecate_soft)
133133
importFrom(magrittr,"%>%")
134134
importFrom(purrr,compact)
135+
importFrom(purrr,cross_df)
135136
importFrom(purrr,discard)
136137
importFrom(purrr,every)
137138
importFrom(purrr,flatten)
@@ -156,10 +157,12 @@ importFrom(purrr,pmap_dbl)
156157
importFrom(purrr,pmap_lgl)
157158
importFrom(purrr,some)
158159
importFrom(rlang,"%||%")
160+
importFrom(rlang,":=")
159161
importFrom(rlang,.data)
160162
importFrom(rlang,parse_expr)
161163
importFrom(rlang,set_names)
162164
importFrom(rlang,sym)
165+
importFrom(rlang,syms)
163166
importFrom(stringr,fixed)
164167
importFrom(stringr,str_detect)
165168
importFrom(stringr,str_extract_all)

NEWS.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
# gtsummary (development version)
22

3+
* Package-wide update allowing arguments that accept variable names to accept bare/symbol inputs, character inputs, stored character inputs, and tidyselect helpers. When passing a single variable, the `vars()` function wrapper is no longer required. #250
4+
5+
```r
6+
tbl_summary(trial, label = age ~ "NEW LABEL"))
7+
tbl_summary(trial, label = "age" ~ "NEW LABEL"))
8+
tbl_summary(trial, label = c("age", "trt") ~ "NEW LABEL"))
9+
tbl_summary(trial, label = c(age, trt) ~ "NEW LABEL"))
10+
tbl_summary(trial, label = vars(age, trt) ~ "NEW LABEL"))
11+
tbl_summary(trial, label = vars(everything(), -age, -trt) ~ "NEW LABEL"))
12+
13+
age_column = "age"
14+
tbl_summary(trial, label = age_column ~ "NEW LABEL"))
15+
tbl_summary(trial, label = vars(age_column) ~ "NEW LABEL")
16+
17+
purrr::map(c("trt", "grade"), ~tbl_summary(trial, by = .x))
18+
```
19+
20+
* New `pattern=` argument in `inline_text.tbl_summary()`. Previously, we could only grab the entire cell from a `tbl_summary()` with `inline_text()`, and now we can get any single statistic reported #254
21+
22+
* Cubic spline terms are now accurately matched to a variable name/term #312
23+
24+
* Improved error messaging in `tidyselect_to_list()` #300
25+
26+
* Functions `tbl_summary_()` and `add_p_()` have been deprecated because the `by=` and `group=` arguments now accept strings #250
27+
328
* Bug fix when non-standard evaluation arguments were passed in `method.args=` argument of `tbl_uvregression()` (#322)
429

530
* Functions `all_categorical()`, `all_dichotomous()`, and `all_continuous()` may now be used in `tbl_summary()` argument `type=` (#256)
@@ -10,7 +35,7 @@
1035

1136
* Bug fix in `as_kable()` where column header did not match statistics presented when certain levels of the `by=` variable are entirely missing in `tbl_summary()` (#304)
1237

13-
* Updated the trial example dataset `"trt"` variable to be `"Drug A"` and `"Drug B"` instead of `"Placebo"` and `"Drug"`
38+
* Updated the trial example data set `"trt"` variable to be `"Drug A"` and `"Drug B"` instead of `"Placebo"` and `"Drug"`
1439

1540
* Improved messaging to users when an error or warning occurs while calculating a p-value in `add_p()`. Also, p-values are no longer omitted from output when a warning is encountered during their calculation (#283)
1641

R/add_global_p.R

+9-14
Original file line numberDiff line numberDiff line change
@@ -77,28 +77,23 @@ add_global_p.tbl_regression <- function(x, include = NULL, exclude = NULL,
7777
select_input = !!rlang::enquo(exclude))
7878

7979
# fetching categorical variables from model
80-
model_terms <- x %>%
80+
if (is.null(include))
81+
include <- x %>%
8182
pluck("table_body") %>%
82-
filter(.data$var_type == "categorical") %>%
83+
filter(.data$var_type %in% c("categorical", "interaction")) %>%
8384
pull(.data$variable) %>%
8485
unique()
8586

86-
# if not terms supplied, getting list of all categorical terms in model
87-
if (is.null(terms)) terms <- model_terms
87+
include <- include %>% setdiff(exclude)
88+
89+
8890

8991
# if no terms are provided, stop and return x
90-
if (length(terms) == 0) {
92+
if (length(include) == 0) {
9193
message("No terms were selected, and no global p-values added to table")
9294
return(x)
9395
}
9496

95-
# check that terms selected appear in model.
96-
if (!all(terms %in% model_terms)) {
97-
stop(glue(
98-
"Terms selected are not categorical terms from model: ",
99-
"{paste(terms[!(terms %in% model_terms)], collpase = ', ')}"
100-
))
101-
}
10297

10398
# calculating global pvalues
10499
tryCatch(
@@ -121,7 +116,7 @@ add_global_p.tbl_regression <- function(x, include = NULL, exclude = NULL,
121116
car_Anova %>%
122117
as.data.frame() %>%
123118
tibble::rownames_to_column(var = "variable") %>%
124-
filter(!!parse_expr("variable %in% terms")) %>%
119+
filter(.data$variable %in% !!include) %>%
125120
select(c("variable", starts_with("Pr(>"))) %>% # selecting the pvalue column
126121
set_names(c("variable", "p.value_global")) %>%
127122
mutate(row_type = "label")
@@ -143,7 +138,7 @@ add_global_p.tbl_regression <- function(x, include = NULL, exclude = NULL,
143138
x$table_body <-
144139
x$table_body %>%
145140
mutate(
146-
p.value = if_else(.data$variable %in% terms & .data$row_type == "level",
141+
p.value = if_else(.data$variable %in% !!include & .data$row_type == "level",
147142
NA_real_, .data$p.value
148143
)
149144
)

R/add_p.R

+13-10
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,18 @@ add_p <- function(x, test = NULL, pvalue_fun = NULL,
7979
group = NULL, include = NULL, exclude = NULL) {
8080

8181
# converting bare arguments to string ----------------------------------------
82-
group <- var_input_to_string(data = x$inputs$data, select_input = !!rlang::enquo(group))
83-
include <- var_input_to_string(data = x$inputs$data, select_input = !!rlang::enquo(include))
84-
exclude <- var_input_to_string(data = x$inputs$data, select_input = !!rlang::enquo(exclude))
82+
group <- var_input_to_string(data = x$inputs$data, select_input = !!rlang::enquo(group),
83+
arg_name = "by", select_single = TRUE)
84+
include <- var_input_to_string(data = x$inputs$data, select_input = !!rlang::enquo(include),
85+
arg_name = "by")
86+
exclude <- var_input_to_string(data = x$inputs$data, select_input = !!rlang::enquo(exclude),
87+
arg_name = "by")
8588

8689
# group argument -------------------------------------------------------------
8790
if (!is.null(group)) {
8891
# checking group is in the data frame
8992
if (!group %in% x$meta_data$variable) {
90-
stop(glue("'{group}' is not a column name in the input data frame."))
93+
stop(glue("'{group}' is not a column name in the input data frame."), call. = FALSE)
9194
}
9295
# dropping group variable from table_body and meta_data
9396
x$table_body <- x$table_body %>% filter(.data$variable != group)
@@ -103,24 +106,24 @@ add_p <- function(x, test = NULL, pvalue_fun = NULL,
103106
"'pvalue_fun' is not a valid function. Please pass only a function\n",
104107
"object. For example,\n\n",
105108
"'pvalue_fun = function(x) style_pvalue(x, digits = 2)'"
106-
))
109+
), call. = FALSE)
107110
}
108111

109112
# checking that input is class tbl_summary
110-
if (class(x) != "tbl_summary") stop("x must be class 'tbl_summary'")
113+
if (class(x) != "tbl_summary") stop("x must be class 'tbl_summary'", call. = FALSE)
111114
# checking that input x has a by var
112115
if (is.null(x$inputs[["by"]])) {
113116
stop(paste0(
114117
"Cannot add comparison when no 'by' variable ",
115118
"in original tbl_summary() call"
116-
))
119+
), call. = FALSE)
117120
}
118121

119122
# test -----------------------------------------------------------------------
120123
# parsing into a named list
121124
test <- tidyselect_to_list(
122125
x$inputs$data, test,
123-
.meta_data = x$meta_data, input_type = "test"
126+
.meta_data = x$meta_data, arg_name = "test"
124127
)
125128

126129
if (!is.null(test)) {
@@ -131,13 +134,13 @@ add_p <- function(x, test = NULL, pvalue_fun = NULL,
131134
stop(glue(
132135
"Each element in 'test' must be named. ",
133136
"For example, 'test = list(age = \"t.test\", ptstage = \"fisher.test\")'"
134-
))
137+
), call. = FALSE)
135138
}
136139
}
137140

138141
# checking pvalue_fun are functions
139142
if (!is.function(pvalue_fun)) {
140-
stop("Input 'pvalue_fun' must be a function.")
143+
stop("Input 'pvalue_fun' must be a function.", call. = FALSE)
141144
}
142145

143146
# Getting p-values only for included variables

R/gtsummary-package.R

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
#' rename_at bind_cols mutate_all mutate_at slice desc
44
#' @importFrom purrr map imap map2 pmap map_chr map_dfr map_lgl map_dbl map_if
55
#' imap_dfr imap_lgl map2_chr pmap_lgl pmap_chr pmap_dbl compact keep discard
6-
#' every some pluck flatten negate partial
6+
#' every some pluck flatten negate partial cross_df
77
#' @importFrom tidyr nest unnest complete spread
88
#' @importFrom tibble tibble as_tibble
9-
#' @importFrom rlang .data %||% set_names sym parse_expr
9+
#' @importFrom rlang .data %||% set_names sym syms parse_expr :=
1010
#' @importFrom glue glue as_glue glue_collapse
11+
#' @importFrom stringr fixed word str_extract_all str_remove_all str_starts
12+
#' str_split str_detect str_remove
1113
#' @keywords internal
1214
"_PACKAGE"
1315

0 commit comments

Comments
 (0)