Skip to content

Commit

Permalink
use is_align_free generic function to determine whether we should ali…
Browse files Browse the repository at this point in the history
…gn observation
  • Loading branch information
Yunuuuu committed Dec 16, 2024
1 parent b478987 commit fb12882
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ S3method(inherit_scheme,"NULL")
S3method(inherit_scheme,scheme_align)
S3method(inherit_scheme,scheme_data)
S3method(inherit_scheme,scheme_theme)
S3method(is_align_free,Align)
S3method(is_align_free,Cross)
S3method(is_align_free,Free)
S3method(is_coord_okay,CoordCartesian)
S3method(is_coord_okay,CoordTrans)
S3method(is_coord_okay,default)
Expand Down
1 change: 0 additions & 1 deletion R/align-.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ summary.Align <- function(object, ...) {
#' @rdname align
#' @include plot-.R
Align <- ggproto("Align", AlignProto,
free_align = FALSE,
parameters = function(self) {
c(
align_method_params(
Expand Down
1 change: 0 additions & 1 deletion R/cross-.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ summary.Cross <- function(object, ...) c(TRUE, FALSE)
#' @importFrom ggplot2 ggproto ggplot
#' @include plot-.R
Cross <- ggproto("Cross", AlignProto,
free_align = FALSE,
layout = function(self, layout_data, layout_coords, layout_name) {
if (is.null(.subset2(layout_coords, "nobs"))) {
cli_abort(sprintf(
Expand Down
2 changes: 1 addition & 1 deletion R/layout-.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ methods::setClass("LayoutProto",
schemes = "list", # used to provide global parameters for all plots
# control the layout, `theme` will also be used by `ggsave`
titles = "list",
annotation = "list", # To-DO add `pacth_titles` for layout
annotation = "list", # To-Do add `pacth_titles` for layout
theme = "ANY",
`_namespace` = "ANY"
),
Expand Down
21 changes: 16 additions & 5 deletions R/plot-.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ methods::setMethod("+", c("ggalign_plot", "ANY"), function(e1, e2) {
#' @importFrom methods is
is_ggalign_plot <- function(x) is(x, "ggalign_plot")

is_align_free <- function(x) UseMethod("is_align_free")

#' @export
is_align_free.Align <- function(x) FALSE

#' @export
is_align_free.Cross <- function(x) FALSE

#' @export
is_align_free.Free <- function(x) TRUE

#' Summary the action of `AlignProto`
#'
#' @param object A `AlignProto` object
Expand All @@ -119,9 +130,8 @@ summary.AlignProto <- function(object, ...) c(FALSE, FALSE)
#' @importFrom ggplot2 ggproto
AlignProto <- ggproto("AlignProto",
locked = TRUE,
# A boolean value indicates whether this plot is free
# if `FALSE`, we'll check whether the layout need align observations
free_align = FALSE,
# we always prevent user from modifying the object in `$build_plot()` and
# `$finish_plot()` methods
lock = function(self) {
assign("locked", value = TRUE, envir = self)
},
Expand Down Expand Up @@ -176,6 +186,7 @@ align_method_params <- function(f, remove = character()) {
#' @export
stack_layout_add.ggalign_plot <- function(object, stack, object_name) {
align <- object@align
# To-Do: Use S7 and double dispatch
if (is_cross(align) && !is_cross_layout(stack)) {
# we prevent from adding `cross_*` to normal `stack_layout()`
cli_abort(c(
Expand All @@ -188,7 +199,7 @@ stack_layout_add.ggalign_plot <- function(object, stack, object_name) {
}
if (is.null(active_index <- stack@active) ||
is_ggalign_plot(plot <- .subset2(stack@plot_list, active_index))) {
if (is.null(old_coords <- stack@layout) && !align$free_align) {
if (is.null(old_coords <- stack@layout) && !is_align_free(align)) {
cli_abort(c(
sprintf(
"Cannot add {.var {object_name}} to %s",
Expand Down Expand Up @@ -258,7 +269,7 @@ quad_layout_add.ggalign_plot <- function(object, quad, object_name) {
# check if we can align in this direction
direction <- to_direction(position)
align <- object@align
if (!align$free_align && is.null(slot(quad, direction))) {
if (!is_align_free(align) && is.null(slot(quad, direction))) {
cli_abort(c(
sprintf(
"Cannot add {.var {object_name}} to %s", object_name(quad)
Expand Down

0 comments on commit fb12882

Please sign in to comment.