Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Geom, Stat & Position #1258

Closed
hadley opened this issue Aug 9, 2015 · 0 comments
Closed

Refactor Geom, Stat & Position #1258

hadley opened this issue Aug 9, 2015 · 0 comments

Comments

@hadley
Copy link
Member

hadley commented Aug 9, 2015

Need to make as homogeneous as possible.

  • setup_data(): called once per layer. Returns modified data. (aka geom$parameterise(),
  • setup_params(): called one per layer. Returns modified params.
  • compute_layer() calls compute_panel() calls compute_group(). Only ever override one of the three. The further up the stack, the more work, but also the more options for efficiency savings.

Biggest difference is scales/panel ranges, because geom called after facets are trained, so get panel ranges rather than scales. I think the main difference here is due to the interaction between coords and scales for computing the expansion. It's too hard to change this now, so it just needs to be clearly documented.

Other stuff to change:

  • layer() needs compute_geom(), compute_stat(), compute_position()
  • build_grob() in ggplot_gtable() should call compute_geom() with by_layer()
  • put ggplot_build() and ggplot_gtable() in the same file
  • need to rename panel_info argument to scales in Stat$compute_panel() and scales argument to panel_ranges in Geom$compute_panel()
  • Need to call remove_missing() by default in compute_data(). Need to document that if you override this method you need to call it yourself.

Some code ideas

adjust_position = function(self, data) {
  if (empty(data)) return(data.frame())
  params <- self$position$compute_defaults(data)

  plyr::ddply(data, "PANEL", function(data) {
    if (empty(data)) return(data.frame())

    self$position$adjust(data, params)
  })
}

compute_position = function(self, data, scales) {
  if (empty(data)) return(data.frame())

  params <- self$position$setup_params(data)
  data <- self$position$setup_data(data, params)
  self$position$compute_layer(data, scales)
}

# --------------------------------------------

calc_statistic = function(self, data, panel) {
  if (empty(data))
    return(data.frame())

  params <- self$stat$compute_defaults(data, self$stat_params)
  data <- self$stat$compute_data(data, self$stat_params)

  check_required_aesthetics(
    self$stat$required_aes,
    c(names(data), names(params)),
    snake_class(self$stat)
  )

  args <- c(list(data = quote(panel_data), panel_info = quote(panel_info)), params)
  plyr::ddply(data, "PANEL", function(panel_data) {
    panel_info <- panel_scales(panel, panel_data$PANEL[1])
    tryCatch(do.call(self$stat$compute_panel, args), error = function(e) {
      warning("Computation failed in `", snake_class(self$stat), "()`:\n",
        e$message, call. = FALSE)
      data.frame()
    })
  })
},

compute_statistic = function(self, data, panel) {
  if (empty(data)) return(data.frame())

  params <- self$stat$setup_params(data, self$stat_param)
  data <- self$stat$setup_data(data, params)
  self$stat$compute_layer(data, scales)
}
@hadley hadley changed the title Refactor calc_statistic Refactor Geom, Stat & Position Aug 25, 2015
hadley added a commit that referenced this issue Aug 25, 2015
hadley added a commit that referenced this issue Aug 25, 2015
@hadley hadley closed this as completed in 3713910 Aug 25, 2015
@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant