R/bench.r

Defines functions benchplot

Documented in benchplot

#' Benchmark plot creation time.
#' Broken down into construct, build, render and draw times.
#'
#' @param x code to create ggplot2 plot
#' @export
#' @keywords internal
#' @examples
#' benchplot(ggplot(mtcars, aes(mpg, wt)) + geom_point())
#' benchplot(ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(. ~ cyl))
#'
#' # With tidy eval:
#' p <- expr(ggplot(mtcars, aes(mpg, wt)) + geom_point())
#' benchplot(!!p)

benchplot <- function(x) {
  x <- enquo(x)
  construct <- system.time(x <- eval_tidy(x))
  if (!inherits(x, "ggplot")) {
    abort("`x` must be a ggplot object")
  }

  build <- system.time(data <- ggplot_build(x))
  render <- system.time(grob <- ggplot_gtable(data))
  draw <- system.time(grid.draw(grob))

  times <- rbind(construct, build, render, draw)[, 1:3]
  times <- rbind(times, colSums(times))

  cbind(
    step = c("construct", "build", "render", "draw", "TOTAL"),
    mat_2_df(times)
  )
}

Try the ggplot2 package in your browser

Any scripts or data that you put into this service are public.

ggplot2 documentation built on June 25, 2021, 5:06 p.m.