I am using the {patchwork} package internally in my package. I would like to utilize the patchwork arithmetic operators (ie |
, \
, etc).
I export methods for my class of objects. For example:
"|.ggsurvfit" <- function(e1, e2) {
build_and_wrap(e1) | build_and_wrap(e2)
}
In the example above, when a figure of class 'ggsurvfit' is passed, it is processed and converted to class 'ggplot', then executed with the patchwork method "|.ggplot"
.
The issue I am encountering is that users need to combine a typical ggplot with a ggsurvfit plot. For example p1 | p2
where p1 has class 'ggplot' and p2
has class 'ggsurvfit'. The patchwork package exports a |.ggplot
method which is being used. But we get an error because the |.ggplot
doesn't know how to do the processing needed for the second argument that is class 'ggsurvfit'.
I am trying to write an S3 method |.ggplot
that would handle the second argument appropriately. But I can't get anything to work...ugh!
Is there a way to force execution of some code via a specified S3 method, i.e. how can I force execution with patchwork's |.ggplot
method?
"|.ggplot" <- function(e1, e2) {
e2 <- build_and_wrap(e2) # process the second argument
# patch together the figures using the patwork `|.ggplot` method
withr::with_namespace(
package = "patchwork",
code = e1 | e2
)
}