Skip to content

Consider making add_ggplot as an S3 generic #4743

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

Closed
emitanaka opened this issue Feb 27, 2022 · 2 comments
Closed

Consider making add_ggplot as an S3 generic #4743

emitanaka opened this issue Feb 27, 2022 · 2 comments

Comments

@emitanaka
Copy link

Would it be possible to make add_ggplot as an S3 generic?
Currently we have to resort to overwriting "+.gg" if we want to adopt a different method based on the class of e1, e.g. GGally is a prime example of this. This allows developers to fix issues like #4304 .

The fix, as below, will be relatively simple, but the downside is that you do have to export another function.

#' @export
add_ggplot <- function(p, object, objectname) {
  UseMethod("add_ggplot")
}

#' @export
add_ggplot.ggplot <- function(p, object, objectname) {
  if (is.null(object)) return(p)
  
  p <- plot_clone(p)
  p <- ggplot_add(object, p, objectname)
  set_last_plot(p)
  p
}
@yutannihilation
Copy link
Member

yutannihilation commented Feb 27, 2022

Just a quick note. We had some discussions on this topic, but couldn't reach a consensus, if I remember correctly.

I still believe this should be solved by utilizing double dispatch (probably with vctrs integration), but, considering I (or we) have failed to implement it for this 2 years, we might need to rethink.

@hadley
Copy link
Member

hadley commented Mar 14, 2022

I think @emitanaka's proposal is a reasonable fix. R7 should eventually get to the point where the double dispatch will just work but that's at least a few years away, and using vctrs would be a bit of a hack since ggplot2 objects aren't vectors. @schloerke's PR looks good to be, so I'll going to close this issue as a duplicate of #3986.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants