Skip to content

Commit

Permalink
avoid nesting inside function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil committed Nov 21, 2023
1 parent a82ed6e commit f02ba74
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
16 changes: 8 additions & 8 deletions R/pairwise_comparisons.R
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ pairwise_comparisons <- function(data,
}

if (type != "robust") {
df <- suppressWarnings(exec(
df_pair <- suppressWarnings(exec(
.f,
# Dunn, Games-Howell, Student's t-test
x = y_vec,
Expand Down Expand Up @@ -216,7 +216,7 @@ pairwise_comparisons <- function(data,
.f.args <- list(y = quote(y_vec), groups = quote(x_vec), blocks = quote(g_vec))
}

df <- eval(call2(.ns = .ns, .fn = .fn, tr = tr, !!!.f.args)) %>% tidy_model_parameters()
df_pair <- eval(call2(.ns = .ns, .fn = .fn, tr = tr, !!!.f.args)) %>% tidy_model_parameters()

test <- "Yuen's trimmed means"
}
Expand All @@ -227,8 +227,8 @@ pairwise_comparisons <- function(data,
df_tidy <- map_dfr(
# creating a list of data frames with subsections of data
.x = map2(
.x = as.character(df$group1),
.y = as.character(df$group2),
.x = as.character(df_pair$group1),
.y = as.character(df_pair$group2),
.f = function(a, b) droplevels(filter(data, {{ x }} %in% c(a, b)))
),
.f = ~ two_sample_test(
Expand All @@ -244,18 +244,18 @@ pairwise_comparisons <- function(data,
mutate(expression = glue("list(log[e]*(BF['01'])=='{format_value(-log(bf10), k)}')")) %>%
mutate(test = "Student's t")

Check warning on line 245 in R/pairwise_comparisons.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/pairwise_comparisons.R,line=245,col=7,[consecutive_mutate_linter] Unify consecutive calls to mutate().

df <- bind_cols(select(df, group1, group2), df_tidy)
df_pair <- bind_cols(select(df_pair, group1, group2), df_tidy)
}

# expression formatting ----------------------------------

df %<>%
df_pair %<>%
mutate(across(where(is.factor), ~ as.character())) %>%
arrange(group1, group2) %>%
select(group1, group2, everything())

if (type != "bayes") {
df %<>%
df_pair %<>%
mutate(
p.value = stats::p.adjust(p = p.value, method = p.adjust.method),
p.adjust.method = p_adjust_text(p.adjust.method),
Expand All @@ -269,7 +269,7 @@ pairwise_comparisons <- function(data,
)
}

select(df, everything(), -matches("p.adjustment|^method$")) %>%
select(df_pair, everything(), -matches("p.adjustment|^method$")) %>%
.glue_to_expression()
}

Expand Down
30 changes: 12 additions & 18 deletions tests/testthat/test-long_to_wide_converter.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ test_that(
test_that(
desc = "with .rowid - without NA",
code = {
df <- structure(
df_original <- structure(
list(
score = c(90, 90, 72.5, 45),
condition = structure(c(1L, 2L, 2L, 1L), .Label = c("4", "5"), class = "factor"),
Expand All @@ -117,18 +117,16 @@ test_that(
class = c("tbl_df", "tbl", "data.frame")
)

df1 <- arrange(df, id)
df_arranged <- arrange(df_original, id)

expect_identical(
long_to_wide_converter(df1, condition, score),
long_to_wide_converter(df, condition, score, id)
long_to_wide_converter(df_arranged, condition, score),
long_to_wide_converter(df_original, condition, score, id)
)

expect_identical(
long_to_wide_converter(df1, condition, score, spread = FALSE) %>%
arrange(.rowid),
long_to_wide_converter(df, condition, score, id, spread = FALSE) %>%
arrange(.rowid)
arrange(long_to_wide_converter(df_arranged, condition, score, spread = FALSE), .rowid),
arrange(long_to_wide_converter(df_original, condition, score, id, spread = FALSE), .rowid)
)
}
)
Expand All @@ -139,21 +137,17 @@ test_that(
test_that(
desc = "with .rowid - with NA",
code = {
df <- bugs_long
df1 <- arrange(bugs_long, subject)
df_original <- bugs_long
df_arranged <- arrange(bugs_long, subject)

expect_identical(
long_to_wide_converter(df1, condition, desire) %>%
select(-.rowid),
long_to_wide_converter(df, condition, desire, subject) %>%
select(-.rowid)
select(long_to_wide_converter(df_arranged, condition, desire), -.rowid),
select(long_to_wide_converter(df_original, condition, desire, subject), -.rowid)
)

expect_identical(
long_to_wide_converter(df1, condition, desire, spread = FALSE) %>%
select(-.rowid),
long_to_wide_converter(df, condition, desire, subject, spread = FALSE) %>%
select(-.rowid)
select(long_to_wide_converter(df_arranged, condition, desire, spread = FALSE), -.rowid),
select(long_to_wide_converter(df_original, condition, desire, subject, spread = FALSE), -.rowid)
)
}
)

0 comments on commit f02ba74

Please sign in to comment.