-
Notifications
You must be signed in to change notification settings - Fork 2k
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
is it possible to let ggsave
utilize ggplot_build
method?
#6002
Comments
I'm not sure what you mean with 'incorporate the |
Something like this, other objects can utilize ggsave <- function(filename, plot) {
# before drawing, call `ggplot_build` or maybe `ggplotGrob`
plot <- ggplot_build(plot)
# ... here we do some other common work
grid.draw(plot)
invisible(filename)
} |
Implicitly, it is used in the Also, based on the error message, it seems like your class object doesn't have a |
@Yunuuuu I think it's a good attempt to utilize S4 system to overcome the limitation of S3, although I'm not sure whether it will succeed or not in the end. I'm not familiar with S4, but maybe you can override library(ggheat)
#> Loading required package: ggplot2 p1 <- ggplot()
p1$theme
#> list() p2 <- ggheat(matrix())
p2$theme
#> Error in slot(x, name): no slot of name "theme" for this object of class "ggheatmap" Created on 2024-07-17 with reprex v2.1.0 |
To be clear, the error happens before Line 106 in 25ad0b1
Lines 600 to 601 in 25ad0b1
|
Thanks for your suggestion, @yutannihilation. It is not possible for #' Subset a `ggheatmap` object
#'
#' Used by [ggplot_build][ggplot2::ggplot_build]
#'
#' @param x A ggheatmap object
#' @param name A string of slot name in `ggheatmap` object.
#' @keywords internal
methods::setMethod("$", "ggheatmap", function(x, name) {
if (name == "theme") {
slot(x, "heatmap")$theme
} else if (name == "plot_env") {
slot(x, "plot_env")
} else {
cli::cli_abort(c(
"`$` is just for internal usage for ggplot2 methods",
i = "try to use `@` method instead"
))
}
}) |
I have implemented another S4 class to override the |
By using the tricks in #6002 (comment), I'm able to resolve the problem when using |
Hi, is it possible to let
ggsave
utilizeggplot_build
method? in this way, all objects supportggplot_build
can useggsave
functionI have developed a ggplot2 extension ggheat for creating heatmaps and I would like to utilize all the functions in ggplot, including
ggsave
. However, creating a new function with the same name would result in duplication. Additionally, since the package automatically loads ggplot, there is no need to create a duplicate function. Tt would be beneficial to incorporate theggplot_build
method, and it would make ggplot extension more powerful and allow for seamless integration with the existing ggplot function.Created on 2024-07-17 with reprex v2.1.0
~
The text was updated successfully, but these errors were encountered: