Skip to content

Instantly share code, notes, and snippets.

@yutannihilation
Created January 31, 2020 02:47
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 yutannihilation/ce6e6a72b208570ea288f7e2eeca7959 to your computer and use it in GitHub Desktop.
Save yutannihilation/ce6e6a72b208570ea288f7e2eeca7959 to your computer and use it in GitHub Desktop.
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