Skip to content

Commit 2bce668

Browse files
authored
Only RHS-formula accepted as lambda function (#6385)
* only cast RHS formula to function * add test * don't cast `bquote()` to lambda functions
1 parent a583f0e commit 2bce668

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

R/scale-.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1775,5 +1775,6 @@ check_continuous_limits <- function(limits, ...,
17751775
}
17761776

17771777
allow_lambda <- function(x) {
1778-
if (is_formula(x)) as_function(x) else x
1778+
# we check the 'call' class to prevent interpreting `bquote()` calls as a function
1779+
if (is_formula(x, lhs = FALSE) && !inherits(x, "call")) as_function(x) else x
17791780
}

tests/testthat/test-utilities.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,24 @@ test_that("expose/ignore_data() can round-trip a data.frame", {
188188

189189
})
190190

191+
test_that("allow_lambda converts the correct cases", {
192+
193+
f <- allow_lambda(function(x) x + 1)
194+
expect_equal(f(1), 2)
195+
196+
f <- allow_lambda(~ .x + 1)
197+
expect_equal(f(1), 2)
198+
199+
f <- allow_lambda("A")
200+
expect_equal(f, "A")
201+
202+
f <- allow_lambda(expression(A))
203+
expect_equal(f, expression(A))
204+
205+
f <- allow_lambda(bquote("foo"~"bar"))
206+
expect_equal(f, call("~", "foo", "bar"))
207+
})
208+
191209
test_that("summary method gives a nice summary", {
192210
# This test isn't important enough to break anything on CRAN
193211
skip_on_cran()

0 commit comments

Comments
 (0)