Skip to content
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

Rename and mutate variables #12

Closed
paul-buerkner opened this issue Oct 27, 2019 · 11 comments
Closed

Rename and mutate variables #12

paul-buerkner opened this issue Oct 27, 2019 · 11 comments
Assignees
Labels
feature New feature or request

Comments

@paul-buerkner
Copy link
Collaborator

For users it might be convenient to have rename and mutate functionality to make changes to variables similar to how we do it with the related dplyr functions. To avoid function masking, we may want to name them rename_variables and mutate_variables which would also be consistent with our naming conventions.

If we decide to implement such functions, we need to think of a proper backend for non-standard evaluation. We could of course use the one of tidyverse but I am not sure how much additional dependencies this implies. @mjskay you may have more experience with this than I have.

@paul-buerkner paul-buerkner added the feature New feature or request label Oct 29, 2019
@mjskay
Copy link
Collaborator

mjskay commented Oct 30, 2019

Should we wish to use tidyverse-style NSE I believe the only dependency we need is rlang, which itself has no other dependencies (and which we already have a dependency on via tibble).

I don't think we would need to support more complex syntax available in select() (things like starts_with(), one_of(), etc), which would require a dependency on tidyselect, which itself has some other dependencies I don't think we already have (glue and purrr).

So supporting tidyverse NSE should in effect add no new dependencies (rlang would move from being an indirect to a direct dependency).

@jgabry
Copy link
Member

jgabry commented Oct 30, 2019

Should we wish to use tidyverse-style NSE I believe the only dependency we need is rlang

Yeah I think you're right that rlang is the only dependency we'd need for that. In my experience working with rlang a bit (although not a ton) I've found it to be super cool but also quite confusing. If I'm not using it consistently then every time I come back to it I have to reteach myself what everything means! Anyway, that doesn't mean we shouldn't use it, just that it could make our code base inaccessible to anyone who doesn't want to do a deep dive into rlang (which is probably most people). That's one of the annoying things about a lot of the source code for the tidyverse (not trying to criticize and it's probably unavoidable for them, but still unfortunate). Of course if we're just using it for a few things then that's no big deal and also probably worth it even if we use it a lot because it allows for nice features. Anyway, just thinking out loud.

@paul-buerkner
Copy link
Collaborator Author

Having the rlang dependency` is fine for me but I don't have much experience with its NSE semantics. @mjskay you presumably know how it works, right?

@mjskay
Copy link
Collaborator

mjskay commented Oct 31, 2019

Yeah, I've used rlang's NSE stuff. It is largely a matter of adopt eval_tidy and/or quo instead of eval and quote; those functions resolve the tidyverse quasiquotation syntax for you (e.g. !!x or {{ x }} for substituting expressions). rename_variables would be relatively straightforward I think. mutate_variables might be more of a pain as I think it would require some format-specific verification that the result of the mutate can actually be stored in the format being used.

If we want this I can take a stab at it.

@paul-buerkner
Copy link
Collaborator Author

Thank you! I think it is a nice feature that people will ask for and I would be happy if you could take a shot at it. Perhaps starting with the draw_df class where things should be easier than for the other formats.

@jgabry
Copy link
Member

jgabry commented Oct 31, 2019

Same! Happy for you to take a stab at it, thanks! It does seem like a nice feature to offer.

@paul-buerkner
Copy link
Collaborator Author

Just a quick note that I am working on mutate_variables now.

@paul-buerkner paul-buerkner self-assigned this Nov 11, 2019
paul-buerkner added a commit that referenced this issue Nov 11, 2019
@paul-buerkner
Copy link
Collaborator Author

paul-buerkner commented Nov 11, 2019

mutate_variables is now supported as well.

x <- as_draws_df(example_draws())
x <- subset(x, variable = c("mu", "tau"))

mutate_variables(x, tau2 = tau^2)
mutate_variables(x, scale = 1.96 * tau, lower = mu - scale)

@jgabry
Copy link
Member

jgabry commented Nov 12, 2019

Cool! One small thing from testthat:

test-mutate_variables.R:13: warning: mutate_variables works correctly for draws_df objects
Using `as.character()` on a quosure is deprecated as of rlang 0.3.0.
Please use `as_label()` or `as_name()` instead.

@paul-buerkner
Copy link
Collaborator Author

Thanks. Should be fixed now.

@ASKurz
Copy link

ASKurz commented Mar 27, 2024

When adding a new variable to as_draws_df() output, is there any advantage to using mutate_variables() versus mutate()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants