If both functions perform the same tasks for ggplot2 elements, we can use a single function like ggbuild_fn to re-dispatch methods. However, the issue arises when the value of ggplot2::labs() does not have a gg class.
# If both function do same things for ggplot2 elements
# we can re-dispatch methods with a single function like `ggbuild_fn`
first_fn <- function(x) {
UseMethod("first_fn")
}
first_fn.gg <- function(x) {
ggbuild_fn(x)
}
second_fn <- function(x) {
UseMethod("second_fn")
}
second_fn.gg <- function(x) {
ggbuild_fn(x)
}
# re-dispatch methods for exact ggplot2 elements
ggbuild_fn <- function(x) {
UseMethod("ggbuild_fn")
}
ggbuild_fn.theme <- function(x) {
# do something ...
}
ggbuild_fn.Scale <- function(x) {
# do something ...
}
ggbuild_fn.labels <- function(x) {
# do something ...
}
ggbuild_fn.Layer <- function(x) {
# do something ...
}
class(ggplot2::scale_alpha_binned())
#> [1] "ScaleBinned" "Scale" "ggproto" "gg"
class(ggplot2::geom_point())
#> [1] "LayerInstance" "Layer" "ggproto" "gg"
class(ggplot2::theme())
#> [1] "theme" "gg"
class(ggplot2::labs())
#> [1] "labels"
If both functions perform the same tasks for ggplot2 elements, we can use a single function like
ggbuild_fn
to re-dispatch methods. However, the issue arises when the value ofggplot2::labs()
does not have agg
class.Created on 2023-12-01 with reprex v2.0.2