Skip to content

purrr::partial for simple wrappers #3

@mabafaba

Description

@mabafaba

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))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions