Skip to content

Commit d7199f9

Browse files
authored
Merge pull request #34 from R4EPI/fix_checks
Fix checks and tests
2 parents 4f23842 + 69f66b6 commit d7199f9

23 files changed

+1207
-2209
lines changed

DESCRIPTION

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ RoxygenNote: 7.3.2
3131
URL: https://R4EPI.github.io/epitabulate/, https://github.com/R4EPI/epitabulate/
3232
BugReports: https://github.com/R4EPI/epitabulate/issues
3333
Imports:
34+
binom,
3435
dplyr (>= 1.0.2),
3536
epikit (>= 0.1.2),
3637
forcats,
@@ -39,11 +40,14 @@ Imports:
3940
MASS,
4041
purrr,
4142
rlang (>= 0.4.0),
43+
scales,
4244
stats,
4345
tibble (>= 3.0.0),
4446
tidyr (>= 1.0.0),
4547
tidyselect (>= 1.0.0)
4648
Suggests:
49+
broom.helpers,
50+
cardx,
4751
testthat,
4852
matchmaker (>= 0.1.0),
4953
epidict,

NAMESPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export(mortality_rate)
1212
export(tbl_cmh)
1313
import(dplyr)
1414
importFrom(MASS,glm.nb)
15+
importFrom(binom,binom.wilson)
1516
importFrom(dplyr,filter)
1617
importFrom(dplyr,mutate)
1718
importFrom(dplyr,relocate)
@@ -27,9 +28,14 @@ importFrom(gtsummary,style_pvalue)
2728
importFrom(gtsummary,style_ratio)
2829
importFrom(gtsummary,tbl_stack)
2930
importFrom(gtsummary,tbl_uvregression)
31+
importFrom(rlang,":=")
3032
importFrom(rlang,as_label)
3133
importFrom(rlang,enquo)
3234
importFrom(rlang,quo_get_expr)
35+
importFrom(scales,number)
36+
importFrom(stats,binomial)
3337
importFrom(stats,glm)
38+
importFrom(stats,poisson)
39+
importFrom(stats,setNames)
3440
importFrom(tidyr,drop_na)
3541
importFrom(tidyselect,vars_select)

R/cfr.R

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,6 @@
3939
#' @export
4040
#'
4141
#' @rdname attack_rate
42-
#'
43-
#' @examples
44-
#' # Attack rates can be calculated with just two numbers
45-
#' print(ar <- attack_rate(10, 50), digits = 4) # 20% attack rate
46-
#'
47-
#' # print them inline using `fmt_ci_df()`
48-
#' epikit::fmt_ci_df(ar)
49-
#'
50-
#' # Alternatively, if you want one column for the CI, use `mergeCI = TRUE`
51-
#' attack_rate(10, 50, mergeCI = TRUE, digits = 2) # 20% attack rate
52-
#'
53-
#' print(cfr <- case_fatality_rate(1, 100), digits = 2) # CFR of 1%
54-
#' epikit::fmt_ci_df(cfr)
55-
#'
56-
#' # using a data frame
57-
#' if (require("outbreaks")) {
58-
#' withAutoprint({
59-
#' e <- outbreaks::ebola_sim$linelist
60-
#' case_fatality_rate_df(e,
61-
#' outcome == "Death",
62-
#' group = gender,
63-
#' add_total = TRUE,
64-
#' mergeCI = TRUE
65-
#' )
66-
#' })
67-
#' }
6842
attack_rate <- function(cases, population, conf_level = 0.95,
6943
multiplier = 100, mergeCI = FALSE, digits = 2) {
7044
res <- proportion(cases, population, multiplier = multiplier, conf_level = conf_level)
@@ -89,6 +63,7 @@ case_fatality_rate <- function(deaths, population, conf_level = 0.95,
8963

9064
#' @rdname attack_rate
9165
#' @importFrom forcats fct_explicit_na
66+
#' @importFrom rlang :=
9267
#' @export
9368
case_fatality_rate_df <- function(x, deaths, group = NULL, conf_level = 0.95,
9469
multiplier = 100, mergeCI = FALSE, digits = 2,
@@ -100,7 +75,7 @@ case_fatality_rate_df <- function(x, deaths, group = NULL, conf_level = 0.95,
10075

10176
# Group the data if needed
10277
if (wants_grouping) {
103-
x <- dplyr::mutate(x, !!qgroup := forcats::fct_explicit_na(!!qgroup, "(Missing)"))
78+
x <- dplyr::mutate(x, !!qgroup := forcats::fct_na_value_to_level(!!qgroup, "(Missing)"))
10479
x <- dplyr::group_by(x, !!qgroup, .drop = FALSE)
10580
}
10681

@@ -160,6 +135,7 @@ case_fatality_rate_df <- function(x, deaths, group = NULL, conf_level = 0.95,
160135
}
161136

162137
#' @rdname attack_rate
138+
#' @importFrom scales number
163139
#' @export
164140
mortality_rate <- function(deaths, population, conf_level = 0.95,
165141
multiplier = 10^4, mergeCI = FALSE, digits = 2) {
@@ -182,6 +158,7 @@ mortality_rate <- function(deaths, population, conf_level = 0.95,
182158
#' @param conf_level confidence level for the confidence interval
183159
#' @param multiplier multiplier for the proportion
184160
#'
161+
#' @importFrom binom binom.wilson
185162
#' @return a data frame with five columns: x, n, prop, lower, and upper
186163
#' @noRd
187164
#'

R/gt_cfr.R

Lines changed: 71 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
1+
# Suppress CMD check notes for NSE variables and base functions used across gtsummary wrapper functions
2+
utils::globalVariables(c(
3+
"case_n", # column name created in dplyr operations
4+
"deaths_n", # column name created in dplyr operations
5+
"Cases", # column name in gtsummary table_body
6+
"Deaths", # column name in gtsummary table_body
7+
"mortality per 10 000" # column name created by mortality_rate function
8+
))
19

2-
#' A mortality rate wrapper function (using the gtsummary package)that takes
3-
#' a gtsummary object and returns a gtsummary object with attack rate (per given
4-
#' multiple) with 95% confidence interval
5-
#'
6-
#' @param gts_object A data frame, passed by the gtsummary::add_stat function.
7-
#'
8-
#' @param deaths_var logical variable name that indicates deaths
9-
#'
10-
#' @param variable Name of a variable as the outcome of interest, passed by the
11-
#' gtsummary::add_stat function (e.g. illness)
12-
#'
13-
#' @param by Name of a variable for stratifying, passed by the gtsummary::add_stat
14-
#' function (e.g. illness).
10+
11+
12+
#' Add mortality rate statistics to a gtsummary table
1513
#'
16-
#'#'@param population the number of individuals in the population, passed to
17-
#'`epitabulate::mortality_rate`
14+
#' This function wraps `gtsummary::add_stat()` to calculate and display
15+
#' **mortality rates** (deaths per given population multiplier) with 95%
16+
#' confidence intervals, using the `epitabulate::mortality_rate()` function
17+
#' internally.
1818
#'
19-
#' @param ... additional params that may be passed from gtsummary functions.
19+
#' @param gts_object A gtsummary object created with functions such as
20+
#' `gtsummary::tbl_summary()` or `gtsummary::tbl_cross()`.
21+
#' @param deaths_var A logical variable name in the data indicating death
22+
#' status (e.g. `TRUE` for death).
23+
#' @param population Optional numeric vector giving the population size for
24+
#' the denominator. If `NULL`, the population is inferred from the data.
25+
#' @param multiplier Numeric multiplier used to scale the mortality rate
26+
#' (e.g. `1` = proportion, `100` = percent, `10^4` = per 10,000).
27+
#' @param drop_tblsummary_stat Logical; if `TRUE`, removes the original
28+
#' `tbl_summary` statistic column (default = `FALSE`).
2029
#'
21-
#' @return a single-row gtsummary object with attack rate results cases,
22-
#' population, attack rate, and 95% confidence interval.
30+
#' @return A modified gtsummary object with additional columns showing
31+
#' the number of deaths, population, mortality rate, and 95% confidence
32+
#' interval.
2333
#'
24-
#' @rdname gt_attackrate
34+
#' @rdname add_mr
2535
#'
2636
#' @export
2737
add_mr <- function(gts_object,
@@ -89,32 +99,32 @@ add_mr <- function(gts_object,
8999
return(gts_object)
90100
}
91101

92-
#' An attack rate wrapper function (using the gtsummary package)that takes
93-
#' a gtsummary object and returns a gtsummary object withattack rate (per given
94-
#' multiple) with 95% confidence interval
102+
#' Add attack rate statistics to a gtsummary table
95103
#'
96-
#' @param gts_object A data frame, passed by the gtsummary::add_stat function.
104+
#' This function wraps `gtsummary::add_stat()` to calculate and display
105+
#' **attack rates** (cases per given population multiplier) with 95% confidence
106+
#' intervals, using the `epitabulate::attack_rate()` function internally.
97107
#'
98-
#' @param variable Name of a variable as the outcome of interest, passed by the
99-
#' gtsummary::add_stat function (e.g. illness)
108+
#' @param gts_object A gtsummary object created with functions such as
109+
#' `gtsummary::tbl_summary()`
100110
#'
101-
#' @param by Name of a variable for stratifying, passed by the gtsummary::add_stat
102-
#' function (e.g. illness).
111+
#' @param case_var A logical variable name in the data indicating case status
112+
#' (e.g. `TRUE` for cases).
103113
#'
104-
#'#'@param population the number of individuals in the population, passed to
105-
#'`epitabulate::attack_rate`
106114
#'
107-
#'@param multiplier The base by which to multiply the output:
108-
# '1: multiplier = 1: ratio between 0 and 1;
109-
# '2: multiplier = 100:proportion;
110-
# '3: multiplier = 10^4: x per 10,000 people; passed to `epitabulate::attack_rate`
115+
#' @param population Optional numeric vector giving the population size for
116+
#' the denominator. If `NULL`, the population is inferred from the data.
111117
#'
112-
#' @param ... additional params that may be passed from gtsummary functions.
118+
#' @param multiplier Numeric multiplier used to scale the attack rate
119+
#' (e.g. `1` = proportion, `100` = percent, `10^4` = per 10,000).
113120
#'
114-
#' @return a single-row gtsummary object with attack rate results cases,
115-
#' population, attack rate, and 95% confidence interval.
121+
#' @param drop_tblsummary_stat Logical; if `TRUE`, removes the original
122+
#' `tbl_summary` statistic column (default = `FALSE`).
123+
#'
124+
#' @return A modified gtsummary object with additional columns showing
125+
#' the number of cases, population, attack rate, and 95% confidence interval.
116126
#'
117-
#' @rdname gt_attackrate
127+
#' @rdname add_ar
118128
#'
119129
#' @export
120130
#'
@@ -183,27 +193,21 @@ add_ar <- function(gts_object,
183193

184194

185195

186-
#' An case fatality rate wrapper function (using the gtsummary package)
187-
#' that takes a gtsummary object and returns a gtsummary object with number
188-
#' of deaths, case fatality rate, and 95% confidence interval.
189-
#'
190-
#' @param data A data frame, passed by the gtsummary::add_stat function.
191-
#'
192-
#' @param variable Name of a variable as the outcome of interest, passed by the
193-
#' gtsummary::add_stat function (e.g. illness).
194-
#'
195-
#' @param by Name of a variable for stratifying, passed by the gtsummary::add_stat function
196-
#' (e.g. illness).
196+
#' Add case fatality rate (CFR) statistics to a gtsummary table
197197
#'
198-
#' @param deaths_var the name of a logical column in the data that indicates that the case died,
199-
#' is passed as the first argument to `epitabulate::case_fatality_rate_df`
200-
#'
201-
#' @param ... additional params that may be passed from gtsummary functions.
198+
#' This function wraps `gtsummary::add_stat()` to calculate and display
199+
#' **case fatality rates** (deaths among cases) with 95% confidence intervals,
200+
#' using the `epitabulate::case_fatality_rate_df()` function internally.
202201
#'
203-
#' @return a single row gtsummary object with case fatality rate results for
204-
#' deaths, cases, cfr, and 95% confidence interval.
202+
#' @param gts_object A gtsummary object created with functions such as
203+
#' `gtsummary::tbl_summary()` or `gtsummary::tbl_cross()`.
204+
#' @param deaths_var A logical variable name in the data indicating death
205+
#' status (e.g. `TRUE` for death).
206+
#' @return A modified gtsummary object with additional columns showing
207+
#' the number of deaths, number of cases, case fatality rate, and 95%
208+
#' confidence interval.
205209
#'
206-
#' @rdname gt_attackrate
210+
#' @rdname add_cfr
207211
#' @import dplyr
208212
#' @export
209213
#'
@@ -247,7 +251,7 @@ add_cfr <- function(gts_object, deaths_var) {
247251

248252
#' @return a gtsummary object without the named column
249253
#'
250-
#' @rdname gtsummary_wrappers
254+
#' @rdname gt_remove_stat
251255
#' @export
252256
#'
253257
gt_remove_stat <- function(gts_object, col_name = "stat_0") {
@@ -382,6 +386,8 @@ add_gt_cfr_stat_level <- function(data, variable, by, deaths_var, ...) {
382386
#'
383387
#' @param ... additional params that may be passed from gtsummary functions.
384388
#'
389+
#' @importFrom stats setNames
390+
#'
385391
#' @return a single row gtsummary object with with attack rate results with
386392
#' cases, attack rate, and 95% confidence interval.
387393
#'
@@ -459,6 +465,9 @@ add_gt_attack_rate_stat_label <-
459465
#'
460466
#' @param ... additional params that may be passed from gtsummary functions.
461467
#'
468+
#' @importFrom rlang :=
469+
#' @importFrom stats setNames
470+
#'
462471
#' @return a single-row gtsummary object with attack rate results cases,
463472
#' population, attack rate, and 95% confidence interval.
464473
#'
@@ -552,6 +561,9 @@ add_gt_attack_rate_level <-
552561
return(ar)
553562
}
554563

564+
565+
#' @importFrom stats setNames
566+
#' @noRd
555567
add_gt_mortality_rate_stat_label <-
556568
function(data, variable, by=NULL, deaths_var, population = NULL,
557569
multiplier = 10^4, drop_total = TRUE, drop_deaths = TRUE, ...) {
@@ -629,6 +641,9 @@ add_gt_mortality_rate_stat_label <-
629641
#'
630642
#' @param ... additional params that may be passed from gtsummary functions.
631643
#'
644+
#' @importFrom rlang :=
645+
#' @importFrom stats setNames
646+
#'
632647
#' @return a single-row gtsummary object with attack rate results cases,
633648
#' population, attack rate, and 95% confidence interval.
634649
#'

R/gt_univariate_crosstabs.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
# Suppress CMD check notes for variables used in NSE (column names from gtsummary)
2+
utils::globalVariables(c("n_obs", "n_event", "n_nonevent", "estimate",
3+
"var_type", "variable", "stratifier",
4+
"reference_row", "var_label"))
15

2-
#' A {gtsummary} wrapper function that takes a gtsummary univariate regression
6+
7+
#' A \{gtsummary\} wrapper function that takes a gtsummary univariate regression
38
#' table and adds appropriate cross tabs by exposure and outcome
49
#'
510
#'@param x Object with class `tbl_uvregression` from the gtsummary

0 commit comments

Comments
 (0)