fix: robust argument evaluation for NSE functions in loops#62
Open
laresbernardo wants to merge 2 commits into
Open
fix: robust argument evaluation for NSE functions in loops#62laresbernardo wants to merge 2 commits into
laresbernardo wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a critical bug introduced in version
5.2.8that causedcorr_var(),ci_var(), andbalance_data()to crash when used inforloops,lapply(), or Shiny reactive environments where column references are parameterized (e.g.,names(iris)[i],n_iris[i], etc.).What was happening:
eval(substitute(var), df)was evaluated in the context of the data frame columnsdf. If the expression passed tovarcontained the dataset name (e.g.,names(iris)[i]oriris[i]), R looked for a column named"iris"withinirisfirst, resulting in evaluation failures.df[[5]]) was evaluated,is.character(var)would evaluate toTRUE. The function then mistakenly treated the entire character vector of lengthif (!var %in% colnames(rs$cor))) to crash withthe condition has length > 1.ci_var()unquoting: Inci_var(), the unquoted string"Fare"insidesummarise(mean(!!var, ...))evaluated to the string literal"Fare"instead of the column symbolFare, which returnedNAvalues with warnings.Proposed Changes:
substitute(var)inparent.frame(): Evaluating in the parent environment prevents R from searching inside the data frame's columns for symbols likeirisordf.is.character(var_eval) && length(var_eval) == 1 && !inherits(var_eval, "try-error")) to guarantee we only use the evaluated representation if it successfully resolves to a single string column name.ci_var():ci_var()now assignssym(var_eval)(a symbol) orenquo(var)(a quosure), allowing it to properly unquote and evaluate inside tidyverse verbs.Files Changed
ci_var(), returning a symbol/quosure for correct tidyverse unquoting.corr_var().balance_data().Verification Plan
Automated/Manual verification
Tested locally using
devtools::load_all()with the following scenarios:All of the above executed without errors and computed correct statistics/correlations.
Reported here: https://stackoverflow.com/questions/78437460/problem-with-assigning-variables-in-a-for-loop-with-lares-corr-var-function/78443283?noredirect=1#comment141107140_78443283