-
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
mutate doesn't allow variable assignment in expression #315
Comments
Very odd. it works the first time, but not on subsequent calls :
|
Another weird thing: after you run it that first time, it leaves
|
It's not obvious to me where Maybe we should just prevent assignment altogether? @wch for your use case, |
Ah. That does help me understand what is going on. The first time around the expression is replaced by:
i.e. cyl is substituted in place. The second time
I think I need to have a fresh look at |
@hadley It makes sense to me to create it in a temporary environment (e.g., a child of the global env) that gets discarded at the end. This is what Is there a performance reason for not creating a new environment in |
I don't that's think that's how they work, and even if it was, it wasn't by design. |
FWIW: invisible(transform(mtcars, cyl2 = { print(devtools::parenvs()); -cyl }))
# label name
# 1 <environment: 0x358d038> ""
# 2 <environment: R_GlobalEnv> ""
invisible(plyr::mutate(mtcars, cyl2 = { print(devtools::parenvs()); -cyl }))
# label name
# 1 <environment: 0x358d038> ""
# 2 <environment: R_GlobalEnv> ""
invisible(dplyr::mutate(mtcars, cyl2 = { print(devtools::parenvs()); -cyl }))
# label name
# 1 <environment: R_GlobalEnv> "" |
I think that's just a result of it being evaluated inside a function. I think we should just disallow assignment inside dplyr functions. |
@romainfrancois can you make @wch I think the semantics are not clear without an explicit local. It should probably be a fresh environment per group, but I think this is sufficiently unusual behaviour that dplyr doesn't need to support it out of the box. |
About not doing anything special with local, then what happens is this:
because it is not local within the environment of the data frame. This works:
Should this be implicit ? There may also be other complications.
Because |
I have forbidden assignments with But not sure how to handle mix of non standard evaluation, e.g. the code with |
I think forbidding |
Sometimes it's useful to use a multi-line expression with
transform
ormutate
, and have some temporary variables in there. But variable assignment within such an expression doesn't work withdplyr::mutate
:The text was updated successfully, but these errors were encountered: