Skip to content

Commit 5463c93

Browse files
authored
data_summary() works with bayestestR::ci() (#483)
* Draft new `data_summary()` function * check if we can avoid duplicated code * pkgdown * fix * fixes * lintr * add tests * code style * desc, news * fix * add print method and snapshot test * add test * correct english form * test * include NA, sort output * add test * meaningful code comments * add test * Update data_summary.R * fix
1 parent 978cde7 commit 5463c93

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: datawizard
33
Title: Easy Data Wrangling and Statistical Transformations
4-
Version: 0.13.0.14
4+
Version: 0.13.0.15
55
Authors@R: c(
66
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
77
comment = c(ORCID = "0000-0003-1995-6531")),

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ CHANGES
2525
* The `replacement` argument in `data_rename()` now supports glue-styled
2626
tokens (#563).
2727

28+
* `data_summary()` also accepts the results of `bayestestR::ci()` as summary
29+
function (#483).
30+
2831
BUG FIXES
2932

3033
* `describe_distribution()` no longer errors if the sample was too sparse to compute

R/data_summary.R

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ data_summary.data.frame <- function(x, ..., by = NULL, remove_na = FALSE) {
122122
# bind grouping-variables and values
123123
summarised_data <- cbind(s[1, by], summarised_data)
124124
# make sure we have proper column names
125-
colnames(summarised_data) <- c(by, vapply(summarise, names, character(1)))
125+
colnames(summarised_data) <- c(by, unlist(lapply(summarise, names)))
126126
summarised_data
127127
})
128128
out <- do.call(rbind, out)
@@ -187,18 +187,24 @@ data_summary.grouped_df <- function(x, ..., by = NULL, remove_na = FALSE) {
187187

188188
out <- lapply(seq_along(dots), function(i) {
189189
new_variable <- .get_new_dots_variable(dots, i, data)
190-
stats::setNames(new_variable, names(dots)[i])
190+
if (inherits(new_variable, c("bayestestR_ci", "bayestestR_eti"))) {
191+
stats::setNames(new_variable, c("CI", "CI_low", "CI_high"))
192+
} else {
193+
stats::setNames(new_variable, names(dots)[i])
194+
}
191195
})
192196
}
193197

194198
# check for correct length of output - must be a single value!
195-
if (any(lengths(out) != 1)) {
199+
# Exception: bayestestR::ci()
200+
wrong_length <- !sapply(out, inherits, what = c("bayestestR_ci", "bayestestR_eti")) & lengths(out) != 1 # nolint
201+
if (any(wrong_length)) {
196202
insight::format_error(
197203
paste0(
198204
"Each expression must return a single value. Following expression",
199-
ifelse(sum(lengths(out) != 1) > 1, "s", " "),
205+
ifelse(sum(wrong_length) > 1, "s", " "),
200206
" returned more than one value: ",
201-
text_concatenate(vapply(dots[lengths(out) != 1], insight::safe_deparse, character(1)), enclose = "\"")
207+
text_concatenate(vapply(dots[wrong_length], insight::safe_deparse, character(1)), enclose = "\"")
202208
)
203209
)
204210
}
@@ -214,6 +220,11 @@ print.dw_data_summary <- function(x, ...) {
214220
if (nrow(x) == 0) {
215221
cat("No matches found.\n")
216222
} else {
223+
if (all(c("CI", "CI_low", "CI_high") %in% colnames(x))) {
224+
ci <- insight::format_table(x[c("CI", "CI_low", "CI_high")], ...)
225+
x$CI <- x$CI_low <- x$CI_high <- NULL
226+
x <- cbind(x, ci)
227+
}
217228
cat(insight::export_table(x, missing = "<NA>", ...))
218229
}
219230
}

tests/testthat/test-data_summary.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,26 @@ test_that("data_summary, extra functions", {
228228
out <- data_summary(mtcars, n = n(), by = c("am", "gear"))
229229
expect_identical(out$n, c(15L, 4L, 8L, 5L))
230230
})
231+
232+
233+
test_that("data_summary, bayestestR::ci", {
234+
skip_if_not_installed("bayestesR")
235+
data(mtcars)
236+
out <- data_summary(
237+
mtcars,
238+
mean_value = mean(mpg),
239+
ci = bayestestR::ci(mpg),
240+
by = c("am", "gear")
241+
)
242+
expect_snapshot(out)
243+
expect_error(
244+
data_summary(
245+
mtcars,
246+
mw = mean(mpg),
247+
test = bayestestR::ci(mpg),
248+
yolo = c(mean(mpg), sd(mpg)),
249+
by = c("am", "gear")
250+
),
251+
regex = "Each expression"
252+
)
253+
})

0 commit comments

Comments
 (0)