Skip to content

Commit

Permalink
Update lazyeval compat file
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Sep 18, 2018
1 parent be02e89 commit 3977636
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions R/compat_lazyeval.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# nocov - compat-lazyeval
# nocov start - compat-lazyeval (last updated: rlang 0.3.0)

# This file serves as a reference for compatibility functions for lazyeval.
# Please find the most recent version in rlang's repository.
Expand All @@ -23,30 +23,40 @@ compat_lazy <- function(lazy, env = caller_env(), warn = TRUE) {
if (missing(lazy)) {
return(quo())
}
if (is_quosure(lazy)) {
return(lazy)
}
if (is_formula(lazy)) {
return(as_quosure(lazy, env))
}

coerce_type(lazy, "a quosure",
formula = as_quosure(lazy, env),
symbol = ,
language = new_quosure(lazy, env),
string = ,
character = {
if (warn) warn_text_se()
parse_quosure(lazy[[1]], env)
},
logical = ,
integer = ,
double = {
if (length(lazy) > 1) {
warn("Truncating vector to length 1")
lazy <- lazy[[1]]
}
new_quosure(lazy, env)
},
list =
coerce_class(lazy, "a quosure",
lazy = new_quosure(lazy$expr, lazy$env)
)
out <- switch(typeof(lazy),
symbol = ,
language = new_quosure(lazy, env),
character = {
if (warn) warn_text_se()
parse_quo(lazy[[1]], env)
},
logical = ,
integer = ,
double = {
if (length(lazy) > 1) {
warn("Truncating vector to length 1")
lazy <- lazy[[1]]
}
new_quosure(lazy, env)
},
list =
if (inherits(lazy, "lazy")) {
lazy = new_quosure(lazy$expr, lazy$env)
}
)

if (is_null(out)) {
abort(sprintf("Can't convert a %s to a quosure", typeof(lazy)))
} else {
out
}
}

compat_lazy_dots <- function(dots, env, ..., .named = FALSE) {
Expand All @@ -68,7 +78,7 @@ compat_lazy_dots <- function(dots, env, ..., .named = FALSE) {

named <- have_name(dots)
if (.named && any(!named)) {
nms <- purrr::map_chr(dots[!named], f_text)
nms <- vapply(dots[!named], function(x) expr_text(get_expr(x)), character(1))
names(dots)[!named] <- nms
}

Expand All @@ -78,12 +88,12 @@ compat_lazy_dots <- function(dots, env, ..., .named = FALSE) {

compat_as_lazy <- function(quo) {
structure(class = "lazy", list(
expr = f_rhs(quo),
env = f_env(quo)
expr = get_expr(quo),
env = get_env(quo)
))
}
compat_as_lazy_dots <- function(...) {
structure(class = "lazy_dots", purrr::map(quos(...), compat_as_lazy))
structure(class = "lazy_dots", lapply(quos(...), compat_as_lazy))
}


Expand Down

0 comments on commit 3977636

Please sign in to comment.