Skip to content

Commit

Permalink
Use magrittr pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Sep 21, 2022
1 parent 54ba640 commit c7ebfd2
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 78 deletions.
128 changes: 72 additions & 56 deletions tests/testthat/_snaps/conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@

Code
# Ungrouped
invisible(mutate(df, x = f()))
df %>% mutate(x = f()) %>% invisible()
Condition
Warning:
There was 1 warning in a `mutate()` step.
Expand All @@ -249,15 +249,16 @@
---
Backtrace:
x
1. +-dplyr::mutate(df, x = f())
2. \-dplyr:::mutate.data.frame(df, x = f())
1. +-df %>% mutate(x = f()) %>% invisible()
2. +-dplyr::mutate(., x = f())
3. \-dplyr:::mutate.data.frame(., x = f())

---

Code
# Grouped
invisible(mutate(group_by(df, id), x = f()))
df %>% group_by(id) %>% mutate(x = f()) %>% invisible()
Condition
Warning:
There were 2 warnings in a `mutate()` step.
Expand All @@ -278,8 +279,9 @@
---
Backtrace:
x
1. +-dplyr::mutate(group_by(df, id), x = f())
2. \-dplyr:::mutate.data.frame(group_by(df, id), x = f())
1. +-df %>% group_by(id) %>% mutate(x = f()) %>% invisible()
2. +-dplyr::mutate(., x = f())
3. \-dplyr:::mutate.data.frame(., x = f())
[[2]]
<warning/rlang_warning>
Expand All @@ -290,15 +292,16 @@
---
Backtrace:
x
1. +-dplyr::mutate(group_by(df, id), x = f())
2. \-dplyr:::mutate.data.frame(group_by(df, id), x = f())
1. +-df %>% group_by(id) %>% mutate(x = f()) %>% invisible()
2. +-dplyr::mutate(., x = f())
3. \-dplyr:::mutate.data.frame(., x = f())

---

Code
# Rowwise
invisible(mutate(rowwise(df), x = f()))
df %>% rowwise() %>% mutate(x = f()) %>% invisible()
Condition
Warning:
There were 2 warnings in a `mutate()` step.
Expand All @@ -319,8 +322,9 @@
---
Backtrace:
x
1. +-dplyr::mutate(rowwise(df), x = f())
2. \-dplyr:::mutate.data.frame(rowwise(df), x = f())
1. +-df %>% rowwise() %>% mutate(x = f()) %>% invisible()
2. +-dplyr::mutate(., x = f())
3. \-dplyr:::mutate.data.frame(., x = f())
[[2]]
<warning/rlang_warning>
Expand All @@ -331,16 +335,17 @@
---
Backtrace:
x
1. +-dplyr::mutate(rowwise(df), x = f())
2. \-dplyr:::mutate.data.frame(rowwise(df), x = f())
1. +-df %>% rowwise() %>% mutate(x = f()) %>% invisible()
2. +-dplyr::mutate(., x = f())
3. \-dplyr:::mutate.data.frame(., x = f())

---

Code
# Multiple type of warnings within multiple verbs
invisible(mutate(group_by(mutate(rowwise(group_by(df, g = f():n())), x = f()),
id), x = f()))
df %>% group_by(g = f():n()) %>% rowwise() %>% mutate(x = f()) %>% group_by(id) %>%
mutate(x = f()) %>% invisible()
Condition
Warning:
There was 1 warning in a `group_by()` step.
Expand Down Expand Up @@ -373,12 +378,13 @@
---
Backtrace:
x
1. +-dplyr::mutate(...)
2. +-dplyr::group_by(...)
3. +-dplyr::mutate(rowwise(group_by(df, g = f():n())), x = f())
4. +-dplyr::rowwise(group_by(df, g = f():n()))
5. +-dplyr::group_by(df, g = f():n())
6. \-dplyr:::group_by.data.frame(df, g = f():n())
1. +-... %>% invisible()
2. +-dplyr::mutate(., x = f())
3. +-dplyr::group_by(., id)
4. +-dplyr::mutate(., x = f())
5. +-dplyr::rowwise(.)
6. +-dplyr::group_by(., g = f():n())
7. \-dplyr:::group_by.data.frame(., g = f():n())
[[2]]
<warning/rlang_warning>
Expand All @@ -389,10 +395,11 @@
---
Backtrace:
x
1. +-dplyr::mutate(...)
2. +-dplyr::group_by(...)
3. +-dplyr::mutate(rowwise(group_by(df, g = f():n())), x = f())
4. \-dplyr:::mutate.data.frame(...)
1. +-... %>% invisible()
2. +-dplyr::mutate(., x = f())
3. +-dplyr::group_by(., id)
4. +-dplyr::mutate(., x = f())
5. \-dplyr:::mutate.data.frame(., x = f())
[[3]]
<warning/rlang_warning>
Expand All @@ -403,10 +410,11 @@
---
Backtrace:
x
1. +-dplyr::mutate(...)
2. +-dplyr::group_by(...)
3. +-dplyr::mutate(rowwise(group_by(df, g = f():n())), x = f())
4. \-dplyr:::mutate.data.frame(...)
1. +-... %>% invisible()
2. +-dplyr::mutate(., x = f())
3. +-dplyr::group_by(., id)
4. +-dplyr::mutate(., x = f())
5. \-dplyr:::mutate.data.frame(., x = f())
[[4]]
<warning/rlang_warning>
Expand All @@ -417,8 +425,9 @@
---
Backtrace:
x
1. +-dplyr::mutate(...)
2. \-dplyr:::mutate.data.frame(...)
1. +-... %>% invisible()
2. +-dplyr::mutate(., x = f())
3. \-dplyr:::mutate.data.frame(., x = f())
[[5]]
<warning/rlang_warning>
Expand All @@ -429,15 +438,16 @@
---
Backtrace:
x
1. +-dplyr::mutate(...)
2. \-dplyr:::mutate.data.frame(...)
1. +-... %>% invisible()
2. +-dplyr::mutate(., x = f())
3. \-dplyr:::mutate.data.frame(., x = f())

---

Code
# Truncated (1 more)
mutate(rowwise(df), x = f())
df %>% rowwise() %>% mutate(x = f())
Condition
Warning:
There were 2 warnings in a `mutate()` step.
Expand Down Expand Up @@ -465,8 +475,9 @@
---
Backtrace:
x
1. +-dplyr::mutate(rowwise(df), x = f())
2. \-dplyr:::mutate.data.frame(rowwise(df), x = f())
1. +-df %>% rowwise() %>% mutate(x = f())
2. +-dplyr::mutate(., x = f())
3. \-dplyr:::mutate.data.frame(., x = f())
Message
... with 1 more warning.
Expand All @@ -477,7 +488,7 @@
Code
# Truncated (several more)
df <- tibble(id = 1:5)
mutate(rowwise(df), x = f())
df %>% rowwise() %>% mutate(x = f())
Condition
Warning:
There were 5 warnings in a `mutate()` step.
Expand Down Expand Up @@ -508,8 +519,9 @@
---
Backtrace:
x
1. +-dplyr::mutate(rowwise(df), x = f())
2. \-dplyr:::mutate.data.frame(rowwise(df), x = f())
1. +-df %>% rowwise() %>% mutate(x = f())
2. +-dplyr::mutate(., x = f())
3. \-dplyr:::mutate.data.frame(., x = f())
Message
... with 4 more warnings.
Expand Down Expand Up @@ -552,11 +564,12 @@
---
Backtrace:
x
1. \-dplyr (local) foo()
2. \-dplyr (local) bar()
3. +-dplyr::mutate(group_by(df, x = f(1):n()), x = f(1, base = FALSE))
4. +-dplyr::group_by(df, x = f(1):n())
5. \-dplyr:::group_by.data.frame(df, x = f(1):n())
1. +-dplyr (local) foo()
2. | \-dplyr (local) bar()
3. | \-df %>% group_by(x = f(1):n()) %>% mutate(x = f(1, base = FALSE))
4. +-dplyr::mutate(., x = f(1, base = FALSE))
5. +-dplyr::group_by(., x = f(1):n())
6. \-dplyr:::group_by.data.frame(., x = f(1):n())
[[2]]
<warning/rlang_warning>
Expand All @@ -567,10 +580,11 @@
---
Backtrace:
x
1. \-dplyr (local) foo()
2. \-dplyr (local) bar()
3. +-dplyr::mutate(group_by(df, x = f(1):n()), x = f(1, base = FALSE))
4. \-dplyr:::mutate.data.frame(...)
1. +-dplyr (local) foo()
2. | \-dplyr (local) bar()
3. | \-df %>% group_by(x = f(1):n()) %>% mutate(x = f(1, base = FALSE))
4. +-dplyr::mutate(., x = f(1, base = FALSE))
5. \-dplyr:::mutate.data.frame(., x = f(1, base = FALSE))
[[3]]
<warning/rlang_warning>
Expand All @@ -581,10 +595,11 @@
---
Backtrace:
x
1. \-dplyr (local) foo()
2. \-dplyr (local) bar()
3. +-dplyr::mutate(group_by(df, x = f(1):n()), x = f(1, base = FALSE))
4. \-dplyr:::mutate.data.frame(...)
1. +-dplyr (local) foo()
2. | \-dplyr (local) bar()
3. | \-df %>% group_by(x = f(1):n()) %>% mutate(x = f(1, base = FALSE))
4. +-dplyr::mutate(., x = f(1, base = FALSE))
5. \-dplyr:::mutate.data.frame(., x = f(1, base = FALSE))
[[4]]
<warning/rlang_warning>
Expand All @@ -595,9 +610,10 @@
---
Backtrace:
x
1. \-dplyr (local) foo()
2. \-dplyr (local) bar()
3. +-dplyr::mutate(group_by(df, x = f(1):n()), x = f(1, base = FALSE))
4. \-dplyr:::mutate.data.frame(...)
1. +-dplyr (local) foo()
2. | \-dplyr (local) bar()
3. | \-df %>% group_by(x = f(1):n()) %>% mutate(x = f(1, base = FALSE))
4. +-dplyr::mutate(., x = f(1, base = FALSE))
5. \-dplyr:::mutate.data.frame(., x = f(1, base = FALSE))

44 changes: 22 additions & 22 deletions tests/testthat/test-conditions.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,50 +80,50 @@ test_that("warnings are collected for `last_dplyr_warnings()`", {
reset_dplyr_warnings()
expect_snapshot({
"Ungrouped"
df |>
mutate(x = f()) |>
df %>%
mutate(x = f()) %>%
invisible()
last_dplyr_warnings()
})

reset_dplyr_warnings()
expect_snapshot({
"Grouped"
df |>
group_by(id) |>
mutate(x = f()) |>
df %>%
group_by(id) %>%
mutate(x = f()) %>%
invisible()
last_dplyr_warnings()
})

reset_dplyr_warnings()
expect_snapshot({
"Rowwise"
df |>
rowwise() |>
mutate(x = f()) |>
df %>%
rowwise() %>%
mutate(x = f()) %>%
invisible()
last_dplyr_warnings()
})

reset_dplyr_warnings()
expect_snapshot({
"Multiple type of warnings within multiple verbs"
df |>
group_by(g = f():n()) |>
rowwise() |>
mutate(x = f()) |>
group_by(id) |>
mutate(x = f()) |>
df %>%
group_by(g = f():n()) %>%
rowwise() %>%
mutate(x = f()) %>%
group_by(id) %>%
mutate(x = f()) %>%
invisible()
last_dplyr_warnings()
})

reset_dplyr_warnings()
expect_snapshot({
"Truncated (1 more)"
df |>
rowwise() |>
df %>%
rowwise() %>%
mutate(x = f())
last_dplyr_warnings(n = 1)
})
Expand All @@ -132,8 +132,8 @@ test_that("warnings are collected for `last_dplyr_warnings()`", {
expect_snapshot({
"Truncated (several more)"
df <- tibble(id = 1:5)
df |>
rowwise() |>
df %>%
rowwise() %>%
mutate(x = f())
last_dplyr_warnings(n = 1)
})
Expand All @@ -160,8 +160,8 @@ test_that("complex backtraces with base and rlang warnings", {

foo <- function() bar()
bar <- function() {
df |>
group_by(x = f(1):n()) |>
df %>%
group_by(x = f(1):n()) %>%
mutate(x = f(1, base = FALSE))
}

Expand All @@ -181,8 +181,8 @@ test_that("`last_dplyr_warnings()` only records 5 backtraces", {
df <- tibble(id = 1:10)

expect_warning(
df |>
group_by(id) |>
df %>%
group_by(id) %>%
mutate(x = f())
)

Expand Down

0 comments on commit c7ebfd2

Please sign in to comment.