-
Notifications
You must be signed in to change notification settings - Fork 2.1k
POC: double dispatch for ggplot_add()
#5537
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
Changes from 2 commits
dd9c09d
aaefb1e
cd7cc4e
a4e4c35
1b00a60
d8bf5e3
ffc347e
92f1e00
33f242b
1fc5bea
75b860c
7e7a0b1
30edf64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ Imports: | |
MASS, | ||
mgcv, | ||
rlang (>= 1.1.0), | ||
S7, | ||
scales (>= 1.2.0), | ||
stats, | ||
tibble, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,49 +87,80 @@ add_ggplot <- function(p, object, objectname) { | |
#' | ||
#' @keywords internal | ||
#' @export | ||
ggplot_add <- function(object, plot, object_name) { | ||
UseMethod("ggplot_add") | ||
} | ||
#' @export | ||
ggplot_add.default <- function(object, plot, object_name) { | ||
ggplot_add <- new_generic( | ||
"ggplot_add", | ||
dispatch_args = c("object", "plot"), | ||
fun = function(object, plot, object_name) S7_dispatch() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be tempted to leave There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair point, I'll leave this comment unresolved to not forget about this for now. |
||
) | ||
|
||
class_ggplot <- new_S3_class("ggplot") | ||
|
||
method( | ||
ggplot_add, | ||
list(object = class_any, plot = class_ggplot) | ||
) <- function(object, plot, object_name) { | ||
teunbrand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cli::cli_abort("Can't add {.var {object_name}} to a {.cls ggplot} object.") | ||
} | ||
#' @export | ||
ggplot_add.NULL <- function(object, plot, object_name) { | ||
|
||
method( | ||
ggplot_add, | ||
list(object = new_S3_class("NULL"), plot = class_ggplot) | ||
teunbrand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) <- function(object, plot, object_name) { | ||
plot | ||
} | ||
#' @export | ||
ggplot_add.data.frame <- function(object, plot, object_name) { | ||
|
||
method( | ||
ggplot_add, | ||
list(object = new_S3_class("data.frame"), plot = class_ggplot) | ||
teunbrand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) <- function(object, plot, object_name) { | ||
plot$data <- object | ||
plot | ||
} | ||
#' @export | ||
ggplot_add.function <- function(object, plot, object_name) { | ||
|
||
method( | ||
ggplot_add, | ||
list(object = new_S3_class("function"), plot = class_ggplot) | ||
teunbrand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) <- function(object, plot, object_name) { | ||
cli::cli_abort(c( | ||
"Can't add {.var {object_name}} to a {.cls ggplot} object", | ||
"Can't add {.var {object_name}} to a {.cls ggplot} object", | ||
"i" = "Did you forget to add parentheses, as in {.fn {object_name}}?" | ||
)) | ||
} | ||
#' @export | ||
ggplot_add.theme <- function(object, plot, object_name) { | ||
|
||
method( | ||
ggplot_add, | ||
list(object = new_S3_class("theme"), plot = class_ggplot) | ||
teunbrand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) <- function(object, plot, object_name) { | ||
plot$theme <- add_theme(plot$theme, object) | ||
plot | ||
} | ||
#' @export | ||
ggplot_add.Scale <- function(object, plot, object_name) { | ||
|
||
method( | ||
ggplot_add, | ||
list(object = new_S3_class("Scale"), plot = class_ggplot) | ||
) <- function(object, plot, object_name) { | ||
plot$scales$add(object) | ||
plot | ||
} | ||
#' @export | ||
ggplot_add.labels <- function(object, plot, object_name) { | ||
|
||
method( | ||
ggplot_add, | ||
list(object = new_S3_class("labels"), plot = class_ggplot) | ||
) <- function(object, plot, object_name) { | ||
update_labels(plot, object) | ||
} | ||
#' @export | ||
ggplot_add.Guides <- function(object, plot, object_name) { | ||
|
||
method( | ||
ggplot_add, | ||
list(object = new_S3_class("Guides"), plot = class_ggplot) | ||
) <- function(object, plot, object_name) { | ||
update_guides(plot, object) | ||
} | ||
#' @export | ||
ggplot_add.uneval <- function(object, plot, object_name) { | ||
|
||
method( | ||
ggplot_add, | ||
list(object = new_S3_class("uneval"), plot = class_ggplot) | ||
) <- function(object, plot, object_name) { | ||
plot$mapping <- defaults(object, plot$mapping) | ||
# defaults() doesn't copy class, so copy it. | ||
class(plot$mapping) <- class(object) | ||
|
@@ -138,34 +169,48 @@ ggplot_add.uneval <- function(object, plot, object_name) { | |
names(labels) <- names(object) | ||
update_labels(plot, labels) | ||
} | ||
#' @export | ||
ggplot_add.Coord <- function(object, plot, object_name) { | ||
|
||
method( | ||
ggplot_add, | ||
list(object = new_S3_class("Coord"), plot = class_ggplot) | ||
) <- function(object, plot, object_name) { | ||
if (!isTRUE(plot$coordinates$default)) { | ||
cli::cli_inform("Coordinate system already present. Adding new coordinate system, which will replace the existing one.") | ||
} | ||
|
||
plot$coordinates <- object | ||
plot | ||
} | ||
#' @export | ||
ggplot_add.Facet <- function(object, plot, object_name) { | ||
|
||
method( | ||
ggplot_add, | ||
list(object = new_S3_class("Facet"), plot = class_ggplot) | ||
) <- function(object, plot, object_name) { | ||
plot$facet <- object | ||
plot | ||
} | ||
#' @export | ||
ggplot_add.list <- function(object, plot, object_name) { | ||
|
||
method( | ||
ggplot_add, | ||
list(object = class_list, plot = class_ggplot) | ||
) <- function(object, plot, object_name) { | ||
for (o in object) { | ||
plot <- plot %+% o | ||
} | ||
plot | ||
} | ||
#' @export | ||
ggplot_add.by <- function(object, plot, object_name) { | ||
|
||
method( | ||
ggplot_add, | ||
list(object = new_S3_class("by"), plot = class_ggplot) | ||
) <- function(object, plot, object_name) { | ||
ggplot_add.list(object, plot, object_name) | ||
} | ||
|
||
#' @export | ||
ggplot_add.Layer <- function(object, plot, object_name) { | ||
method( | ||
ggplot_add, | ||
list(object = new_S3_class("Layer"), plot = class_ggplot) | ||
) <- function(object, plot, object_name) { | ||
plot$layers <- append(plot$layers, object) | ||
|
||
# Add any new labels | ||
|
Uh oh!
There was an error while loading. Please reload this page.