Skip to content

Commit

Permalink
Fix rlang::inject with render functions
Browse files Browse the repository at this point in the history
Render functions use quoToFunction() to convert quosures to
functions; quoToFunction() was using new_function, which leads
to non-tidy evaluation, so nested quosures are not evaluated.

See rstudio#3361 (comment)
  • Loading branch information
jcheng5 committed Apr 22, 2021
1 parent 1558c84 commit f374a15
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 1 addition & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,7 @@ installExprFunction <- function(expr, name, eval.env = parent.frame(2),
#' @export
quoToFunction <- function(q, label, ..stacktraceon = FALSE) {
q <- as_quosure(q)
# Use new_function() instead of as_function(), because as_function() adds an
# extra parent environment. (This may not actually be a problem, though.)
func <- new_function(NULL, get_expr(q), get_env(q))
func <- as_function(q)
wrapFunctionLabel(func, label, ..stacktraceon = ..stacktraceon)
}

Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,15 @@ test_that("dateYMD works", {
c("2019/11/05", "")
)
})

test_that("quoToFunction handles nested quosures", {
quo_inner <- local({
x <- 1
rlang::quo(x)
})

quo_outer <- rlang::quo(!!quo_inner + 1)

func <- quoToFunction(quo_outer, "foo")
expect_identical(func(), 2)
})

0 comments on commit f374a15

Please sign in to comment.