From abc3ca62ab7dd111fd56db195669aa7b8f0dd2a5 Mon Sep 17 00:00:00 2001 From: etiennebacher Date: Tue, 4 Feb 2025 15:11:20 +0100 Subject: [PATCH] init --- R/data_modify.R | 2 +- R/select_nse.R | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/R/data_modify.R b/R/data_modify.R index 3e30b8f68..119a64e21 100644 --- a/R/data_modify.R +++ b/R/data_modify.R @@ -346,7 +346,7 @@ data_modify.grouped_df <- function(data, ..., .if = NULL, .at = NULL, .modify = # like: data_modify(iris, new_var = "a") # this one should be recycled instead. if (!is.character(symbol)) { - eval_symbol <- .dynEval(symbol, ifnotfound = NULL) + eval_symbol <- .dynEval(symbol, ifnotfound = NULL, data = data) if (is.character(eval_symbol)) { symbol <- try(str2lang(paste0(names(dots)[i], " = ", eval_symbol)), silent = TRUE) # we may have the edge-case of having a function that returns a character diff --git a/R/select_nse.R b/R/select_nse.R index be1565483..a635cbd43 100644 --- a/R/select_nse.R +++ b/R/select_nse.R @@ -607,17 +607,27 @@ # error. Returns NULL if can never be evaluated. # # Custom arg "remove_n_top_env" to remove the first environments which are -# ".select_nse()" and the other custom functions +# ".select_nse()" and the other custom functions. +# +# Arg "data" is here if we want to start searching in the data instead of the +# lowest environment. .dynEval <- function(x, ifnotfound = stop(gettextf("%s not found", sQuote(x)), domain = NA, call. = FALSE), minframe = 1L, inherits = FALSE, - remove_n_top_env = 0) { + remove_n_top_env = 0, + data = NULL) { + iter <- 0 n <- sys.nframe() - remove_n_top_env x <- insight::safe_deparse(x) while (n > minframe) { - n <- n - 1L - env <- sys.frame(n) + if (iter == 0 && !is.null(data)) { + env <- data + iter <- iter + 1 + } else { + n <- n - 1L + env <- sys.frame(n) + } r <- try(eval(str2lang(x), envir = env), silent = TRUE) if (!inherits(r, "try-error") && !is.null(r)) { return(r)