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

purrr::partial for simple wrappers #3

Open
mabafaba opened this issue May 9, 2019 · 0 comments
Open

purrr::partial for simple wrappers #3

mabafaba opened this issue May 9, 2019 · 0 comments

Comments

@mabafaba
Copy link
Contributor

mabafaba commented May 9, 2019

Just a general concept that could be REALLY useful for this package is 'partial functions'.
Basically take an existing function and copy it, but set some fixed values for some parameters.
this is implemented in purrr::partial():

Example:
we want a version of scale_y_continuous, but impact style and for percent. It's basically the same as ggplot2::scale_y_continuous, except that some parameters are defined by us: "limits" and "labels".

so we can do:


scale_y_percent_impact <- purrr::partial(ggplot2::scale_y_continuous,
                                         limits = c(0,100),
                                         labels = scales::percent_format(scale=1,accuracy = 0.01)
                                     )

this made a copy of scale_y_continuous, but some of the parameters are now fixed; this makes an impact scale:

ggplot() + scale_y_percent_impact()

see how it's used exactly like scale_y_continuous (because purrr::partial returned an almost-copy of scale_y_continuous)
because we only set some parameters, it still works to add additional ones:

ggplot() + scale_y_percent_impact(minor_breaks = seq(0,100,1))

but the following doesnt work, because "limits" is already set:

ggplot() + scale_y_percent_impact(limits=c(0,-12))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant