-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Variable scoping issue with .data inside lambda Ffunction used in across() #7016
Comments
I noticed a possibly related problem from this Stack Overflow question. This code results in an error
Same issue if using
But if you define the function first rather than inline, it will run
Looking at the trace, it seems the problem is actually coming from
Basically the |
You'd have the same issue if you nest You can use the following trick:
For data it's a bit different I think, in the given examples .data shouldn't be used, it's not to be considered as an object in scope, but as a special operator at the top level for mutate, it's not very clear from the dot though. It seems a macro is run (so if you set a browser() in the function it won't be triggered for instance) when we use .data, where using means calling it with brackets. This works around it :
|
Hello,
This error was driving me crazy and took me a while to isolate it.
if one wants to use
.data
into a lambda function into across, you cannott index .data with variables created into the lambda function itself, otherwise R will complain that such variable doesn't exist!example:
Which was driving me crazy, since the variable clearly exists in the scope.
but if
other_col_name
is defined outside no problem:If this is by design and you don't plan to fix it, could be useful to have a clearer error and some extra documentation somewhere!
There cases in which for example the index name is defined dynamically based on the x or cur_column() value. Now that I now the issue I'll use pick()[1], unless you have better solutions.
The text was updated successfully, but these errors were encountered: