Skip to content

Instantly share code, notes, and snippets.

@yutannihilation
Created January 30, 2018 15:26
Show Gist options
  • Select an option

  • Save yutannihilation/08d3451f6133b4bb10da7a5d9a40b228 to your computer and use it in GitHub Desktop.

Select an option

Save yutannihilation/08d3451f6133b4bb10da7a5d9a40b228 to your computer and use it in GitHub Desktop.
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