Skip to content

Commit dd6e080

Browse files
Grouped report_participants() does not report gender unless argument gender is capitalized (#378)
* Grouped `report_participants()` does not report gender unless argument `gender` is capitalized Fixes #377 * fix lints * last lint * styler --------- Co-authored-by: rempsyc <[email protected]>
1 parent 75ae425 commit dd6e080

File tree

5 files changed

+83
-25
lines changed

5 files changed

+83
-25
lines changed

.lintr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ linters: linters_with_defaults(
1212
todo_comment_linter = NULL,
1313
undesirable_function_linter(c("mapply" = NA, "sapply" = NA, "setwd" = NA)),
1414
undesirable_operator_linter = NULL,
15-
unneeded_concatenation_linter(allow_single_expression = FALSE),
15+
unnecessary_concatenation_linter(allow_single_expression = FALSE),
1616
defaults = linters_with_tags(tags = NULL)
1717
)

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: report
33
Title: Automated Reporting of Results and Statistical Models
4-
Version: 0.5.7.9
4+
Version: 0.5.7.10
55
Authors@R:
66
c(person(given = "Dominique",
77
family = "Makowski",

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Minor changes
2222

2323
Bug fixes
2424

25+
* Fixed issue in `report_participants`, which did not print the `"gender"`
26+
category for grouped output when that argument was written in lower-case.
27+
2528
* Fixed printing issue for intercept-only models.
2629

2730
# report 0.5.7

R/report_participants.R

+21-20
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ report_participants <- function(data,
129129
threshold = 10,
130130
...) {
131131
# Convert empty strings to NA
132-
data.list <- lapply(data, function(x) {
132+
data_list <- lapply(data, function(x) {
133133
x[which(x == "")] <- NA
134134
x
135135
})
136-
data <- as.data.frame(data.list)
136+
data <- as.data.frame(data_list)
137137

138138
# find age variable automatically
139139
if (is.null(age)) {
@@ -173,6 +173,7 @@ report_participants <- function(data,
173173
i,
174174
age = age,
175175
sex = sex,
176+
gender = gender,
176177
education = education,
177178
country = country,
178179
race = race,
@@ -245,8 +246,8 @@ report_participants <- function(data,
245246
threshold = 10,
246247
...) {
247248
# Sanity checks
248-
demo.names <- c("Age", "Sex", "Gender", "Education", "Country", "Race")
249-
data <- .check_df_names(data, names = demo.names)
249+
demo_names <- c("Age", "Sex", "Gender", "Education", "Country", "Race")
250+
data <- .check_df_names(data, names = demo_names)
250251

251252
age <- .replace_names(data, age)
252253
sex <- .replace_names(data, sex)
@@ -404,15 +405,15 @@ report_participants <- function(data,
404405
} else {
405406
data[[country]] <- as.character(data[[country]])
406407
data[which(data[[country]] %in% c(NA, "NA")), country] <- "missing"
407-
frequency.table <- as.data.frame(datawizard::data_tabulate(data[[country]]))[c(2, 4)]
408-
names(frequency.table)[2] <- "Percent"
409-
frequency.table <- frequency.table[-which(is.na(frequency.table$Value)), ]
410-
frequency.table <- frequency.table[order(-frequency.table$Percent), ]
411-
upper <- frequency.table[which(frequency.table$Percent >= threshold), ]
412-
lower <- frequency.table[which(frequency.table$Percent < threshold), ]
408+
frequency_table <- as.data.frame(datawizard::data_tabulate(data[[country]]))[c(2, 4)]
409+
names(frequency_table)[2] <- "Percent"
410+
frequency_table <- frequency_table[-which(is.na(frequency_table$Value)), ]
411+
frequency_table <- frequency_table[order(-frequency_table$Percent), ]
412+
upper <- frequency_table[which(frequency_table$Percent >= threshold), ]
413+
lower <- frequency_table[which(frequency_table$Percent < threshold), ]
413414
if (nrow(lower) > 0) {
414-
lower.sum <- data.frame(Value = "other", Percent = sum(lower$Percent), stringsAsFactors = FALSE)
415-
combined <- rbind(upper, lower.sum)
415+
lower_sum <- data.frame(Value = "other", Percent = sum(lower$Percent), stringsAsFactors = FALSE)
416+
combined <- rbind(upper, lower_sum)
416417
} else {
417418
combined <- upper
418419
}
@@ -426,15 +427,15 @@ report_participants <- function(data,
426427
} else {
427428
data[[race]] <- as.character(data[[race]])
428429
data[which(data[[race]] %in% c(NA, "NA")), race] <- "missing"
429-
frequency.table <- as.data.frame(datawizard::data_tabulate(data[[race]]))[c(2, 4)]
430-
names(frequency.table)[2] <- "Percent"
431-
frequency.table <- frequency.table[-which(is.na(frequency.table$Value)), ]
432-
frequency.table <- frequency.table[order(-frequency.table$Percent), ]
433-
upper <- frequency.table[which(frequency.table$Percent >= threshold), ]
434-
lower <- frequency.table[which(frequency.table$Percent < threshold), ]
430+
frequency_table <- as.data.frame(datawizard::data_tabulate(data[[race]]))[c(2, 4)]
431+
names(frequency_table)[2] <- "Percent"
432+
frequency_table <- frequency_table[-which(is.na(frequency_table$Value)), ]
433+
frequency_table <- frequency_table[order(-frequency_table$Percent), ]
434+
upper <- frequency_table[which(frequency_table$Percent >= threshold), ]
435+
lower <- frequency_table[which(frequency_table$Percent < threshold), ]
435436
if (nrow(lower) > 0) {
436-
lower.sum <- data.frame(Value = "other", Percent = sum(lower$Percent), stringsAsFactors = FALSE)
437-
combined <- rbind(upper, lower.sum)
437+
lower_sum <- data.frame(Value = "other", Percent = sum(lower$Percent), stringsAsFactors = FALSE)
438+
combined <- rbind(upper, lower_sum)
438439
} else {
439440
combined <- upper
440441
}

tests/testthat/test-report_participants.R

+57-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,44 @@
1+
test_that("report_participants, argument gender works", {
2+
# Works when capitalized
3+
data <- data.frame(
4+
"Age" = c(22, 22, 54, 54, 8, 8, 42, 42),
5+
"Gender" = c("N", "N", "W", "M", "M", "M", "Non-Binary", "Non-Binary"),
6+
"Condition" = c("A", "A", "A", "A", "B", "B", "B", "B"),
7+
stringsAsFactors = FALSE
8+
)
9+
out <- report_participants(data)
10+
expect_identical(
11+
out,
12+
paste(
13+
"8 participants (Mean age = 31.5, SD = 19.0, range: [8, 54];",
14+
"Gender: 12.5% women, 37.5% men, 50.00% non-binary)"
15+
)
16+
)
17+
out <- report_participants(data, group = "Condition")
18+
expect_identical(
19+
out,
20+
paste(
21+
"For the 'Condition - A' group: 4 participants (Mean age = 38.0,",
22+
"SD = 18.5, range: [22, 54]; Gender: 25.0% women, 25.0% men,",
23+
"50.00% non-binary) and for the 'Condition - B' group: 4 participants",
24+
"(Mean age = 25.0, SD = 19.6, range: [8, 42]; Gender: 0.0% women,",
25+
"50.0% men, 50.00% non-binary)"
26+
)
27+
)
28+
# works when lowercase
29+
out <- report_participants(data, group = "Condition")
30+
expect_identical(
31+
out,
32+
paste(
33+
"For the 'Condition - A' group: 4 participants (Mean age = 38.0,",
34+
"SD = 18.5, range: [22, 54]; Gender: 25.0% women, 25.0% men,",
35+
"50.00% non-binary) and for the 'Condition - B' group: 4 participants",
36+
"(Mean age = 25.0, SD = 19.6, range: [8, 42]; Gender: 0.0% women,",
37+
"50.0% men, 50.00% non-binary)"
38+
)
39+
)
40+
})
41+
142
test_that("report_participants", {
243
data <- data.frame(
344
"Age" = c(22, 22, 54, 54, 8, 8),
@@ -9,14 +50,24 @@ test_that("report_participants", {
950

1051
expect_snapshot(
1152
variant = "windows",
12-
report_participants(data, age = "Age", sex = "Sex", participant = "Participant")
53+
report_participants(data,
54+
age = "Age", sex = "Sex",
55+
participant = "Participant"
56+
)
1357
)
1458
expect_snapshot(
1559
variant = "windows",
1660
report_participants(data, participant = "Participant", spell_n = TRUE)
1761
)
1862

19-
expect_equal(nchar(report_participants(data, participant = "Participant", spell_n = TRUE)), 160, ignore_attr = TRUE)
63+
expect_equal(
64+
nchar(report_participants(data,
65+
participant = "Participant",
66+
spell_n = TRUE
67+
)),
68+
160,
69+
ignore_attr = TRUE
70+
)
2071

2172
data2 <- data.frame(
2273
"Age" = c(22, 22, 54, 54, 8, 8),
@@ -45,7 +96,10 @@ test_that("report_participants", {
4596
# Add tests for education, country, race
4697
data4 <- data.frame(
4798
"Education" = c(0, 8, -3, -5, 3, 5, NA),
48-
"Education2" = c("Bachelor", "PhD", "Highschool", "Highschool", "Bachelor", "Bachelor", NA),
99+
"Education2" = c(
100+
"Bachelor", "PhD", "Highschool", "Highschool",
101+
"Bachelor", "Bachelor", NA
102+
),
49103
"Country" = c("USA", "Canada", "Canada", "India", "Germany", "USA", NA),
50104
"Race" = c("Black", NA, "White", "Asian", "Black", "Black", "White"),
51105
stringsAsFactors = FALSE

0 commit comments

Comments
 (0)