Skip to content

Instantly share code, notes, and snippets.

@eliocamp
Last active November 5, 2020 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliocamp/91ea0c322b5062f9945c6f12a4f1592e to your computer and use it in GitHub Desktop.
Save eliocamp/91ea0c322b5062f9945c6f12a4f1592e to your computer and use it in GitHub Desktop.
ditto <- function(expr) {
plus_fun <- function(e1, e2) {
if (is.list(e1)) {
c(e1, list(e2))
} else {
list(e1, e2)
}
}
geom_list <- eval(substitute(expr), envir = list("+.gg" = plus_fun), enclos = parent.frame())
e1 <- geom_list[[1]]
for (i in seq_along(geom_list)[-1]) {
e2 <- geom_list[[i]]
geom_list[[i]]$geom_params <- stomp(e2$geom_params,
e1$geom_params)
geom_list[[i]]$aes_params <- stomp(e2$aes_params,
e1$aes_params)
geom_list[[i]]$stat_params <- stomp(e2$stat_params,
e1$stat_params)
geom_list[[i]]$mapping <- e1$mapping
}
geom_list
}
stomp <- function(original, new) {
names <- names(new)
for (name in names) {
original[[name]] <- new[[name]]
}
original
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment