Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
library(ggplot2)
library(rlang)

ggplot <- function(...) {
  f <- function(...) list(clone = f)
  structure(
    list(
      call = sys.call()
    ),
    class = "gg"
  )
}

`+.gg` <- function(e1, e2) {
  e2 <- enexpr(e2)
  e1$call <- expr(!!e1$call + !!e2)
  e1
}

p <- ggplot(mpg, aes(cty)) +
  geom_bar()

p
#> $call
#> ggplot(mpg, aes(cty)) + geom_bar()
#> 
#> attr(,"class")
#> [1] "gg"

eval_bare(p$call, env(ggplot = ggplot2::ggplot, `+.gg` = ggplot2:::`+.gg`))

Created on 2020-01-31 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.