Skip to content

Commit

Permalink
Avoid R if vectorisation error
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Jul 16, 2024
1 parent c954a09 commit 8c139c8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/replace.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
# useful error if not vectorised correctly

Code
str_replace_all(x, "a|c", ~ if (nchar(.x)) "x" else "y")
str_replace_all(x, "a|c", ~ if (length(x) > 1) stop("Bad"))
Condition
Error in `str_replace_all()`:
! Failed to apply `replacement` function.
i It must accept a character vector of any length.
Caused by error in `if (nchar(.x)) ...`:
! the condition has length > 1
Caused by error in `replacement()`:
! Bad

# replacement function must return correct type/length

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-replace.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ test_that("is forgiving of 0 matches with paste", {
test_that("useful error if not vectorised correctly", {
x <- c("a", "b", "c")
expect_snapshot(
str_replace_all(x, "a|c", ~ if (nchar(.x)) "x" else "y"),
str_replace_all(x, "a|c", ~ if (length(x) > 1) stop("Bad")),
error = TRUE
)
})
Expand Down

0 comments on commit 8c139c8

Please sign in to comment.