Created
January 30, 2018 15:26
-
-
Save yutannihilation/08d3451f6133b4bb10da7a5d9a40b228 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(ggplot2) | |
| library(dplyr) | |
| call_as_pipable <- function(g, ...) { | |
| # get the current function name as string | |
| fun_name <- deparse(match.call()[[1]]) | |
| # remove "add_" and find the correspondent function | |
| fun <- get(gsub("^add_", "", fun_name), | |
| envir = asNamespace("ggplot2")) | |
| g + fun(...) | |
| } | |
| ggplot_funs <- ls(asNamespace("ggplot2"), | |
| pattern = "^geom_|^stat_|^scale_|^coord_|^facet_|^theme_") | |
| pipable_funs <- glue::glue("add_{ggplot_funs}") | |
| purrr::walk(pipable_funs, | |
| assign, value = call_as_pipable, envir = .GlobalEnv) | |
| # Usage ---------------------------------------------------------- | |
| ggplot(iris) %>% | |
| add_geom_point(aes(Sepal.Length, Sepal.Width)) %>% | |
| add_theme_minimal() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment