Skip to content

Commit 9a70b83

Browse files
author
Yunuuuu
authored
feat: add common generics to support extensions (#85)
* feat: add common generics to support extensions * refactor: implement Scheme system using common generics * feat: allow NA values as valid tags * feat: implement layout_add using ggalign_update
1 parent 1933e33 commit 9a70b83

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1146
-905
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ Roxygen: list(markdown = TRUE)
6262
RoxygenNote: 7.3.2
6363
Language: en-GB
6464
Collate:
65-
'active.R'
6665
'alignpatch-.R'
6766
'ggplot-theme.R'
6867
'alignpatch-theme.R'
68+
'generics.R'
6969
'utils-ggplot.R'
7070
'alignpatch-tags.R'
7171
'alignpatch-title.R'
@@ -104,6 +104,7 @@ Collate:
104104
'craft-cross-none.R'
105105
'utils-assert.R'
106106
'scheme-.R'
107+
'plot.R'
107108
'craftbox.R'
108109
'domain.R'
109110
'fortify-data-frame-.R'

NAMESPACE

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ S3method(free_space,ggalign_free_align)
102102
S3method(free_space,ggalign_free_space)
103103
S3method(free_vp,default)
104104
S3method(free_vp,ggalign_free_vp)
105-
S3method(ggalign_gtable,gtable)
106105
S3method(ggalign_stat,"ggalign::CraftBox")
107106
S3method(ggalign_stat,"ggalign::QuadLayout")
108107
S3method(ggalign_stat,"ggalign::StackLayout")
@@ -272,7 +271,6 @@ export(.mark_draw)
272271
export(CraftAlign)
273272
export(Patch)
274273
export(Scheme)
275-
export(Schemes)
276274
export(active)
277275
export(align)
278276
export(align_dendro)
@@ -332,9 +330,14 @@ export(geom_tile3d)
332330
export(ggalign)
333331
export(ggalignGrob)
334332
export(ggalign_attr)
333+
export(ggalign_build)
335334
export(ggalign_data_set)
335+
export(ggalign_gtable)
336+
export(ggalign_inherit)
337+
export(ggalign_init)
336338
export(ggalign_lvls)
337339
export(ggalign_stat)
340+
export(ggalign_update)
338341
export(ggcross)
339342
export(ggfree)
340343
export(ggheatmap)
@@ -404,10 +407,7 @@ export(scale_z_discrete)
404407
export(scale_z_ordinal)
405408
export(scheme_align)
406409
export(scheme_data)
407-
export(scheme_inherit)
408-
export(scheme_init)
409410
export(scheme_theme)
410-
export(scheme_update)
411411
export(stack_active)
412412
export(stack_align)
413413
export(stack_alignh)

R/active.R

Lines changed: 0 additions & 103 deletions
This file was deleted.

R/alignpatch-.R

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -113,38 +113,6 @@ make_patch_table <- function() {
113113
)
114114
}
115115

116-
#' Initialize an S7 Object
117-
#'
118-
#' Helper generic to initialize an object before use. This may include
119-
#' setting properties or other elements to their default values, particularly
120-
#' if the object's internal defaults differ from the defaults required for
121-
#' normal usage.
122-
#'
123-
#' @param input An S7 object to initialize.
124-
#'
125-
#' @return The initialized object, ready for use.
126-
#'
127-
#' @noRd
128-
init_object <- S7::new_generic("init_object", "input")
129-
130-
#' Generate a plot grob.
131-
#'
132-
#' @param x An object to be converted into a [grob][grid::grob].
133-
#' @return A [`grob()`][grid::grob] object.
134-
#' @examples
135-
#' ggalignGrob(ggplot())
136-
#' @export
137-
ggalignGrob <- function(x) ggalign_gtable(ggalign_build(x))
138-
139-
# Now, we only define `ggalign_gtable` method for `alignpatches` and `ggplot`
140-
# `ggalign_build` must return these objects
141-
ggalign_build <- function(x) UseMethod("ggalign_build")
142-
143-
ggalign_gtable <- function(x) UseMethod("ggalign_gtable")
144-
145-
#' @export
146-
ggalign_gtable.gtable <- function(x) x
147-
148116
#' Get Patch representation
149117
#'
150118
#' @description

R/alignpatch-alignpatches.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ PatchAlignpatches <- ggproto(
176176
}
177177
out
178178
})
179-
layout <- init_object(prop(self$plot, "layout"))
179+
layout <- ggalign_init(prop(self$plot, "layout"))
180180

181181
# get the design areas and dims ------------------
182182
panel_widths <- prop(layout, "widths")

R/alignpatch-build.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ S7::method(ggalign_gtable, alignpatches) <- function(x) {
1919
matrix_respect <- is.matrix(.subset2(table, "respect"))
2020

2121
# Add title, subtitle, and caption -------------------
22-
titles <- init_object(prop(x, "titles"))
22+
titles <- ggalign_init(prop(x, "titles"))
2323
# https://github.com/tidyverse/ggplot2/blob/2e08bba0910c11a46b6de9e375fade78b75d10dc/R/plot-build.R#L219C3-L219C9
2424
title <- element_render(
2525
theme, "plot.title", prop(titles, "title"),

R/alignpatch-design.R

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -114,50 +114,53 @@ layout_design <- S7::new_class("layout_design",
114114
)
115115

116116
#' @importFrom S7 prop prop<-
117-
S7::method(init_object, layout_design) <- function(input) {
118-
if (identical(prop(input, "ncol"), NA_real_)) {
119-
prop(input, "ncol", check = FALSE) <- NULL
117+
S7::method(ggalign_init, layout_design) <- function(x) {
118+
if (identical(prop(x, "ncol"), NA_real_)) {
119+
prop(x, "ncol", check = FALSE) <- NULL
120120
}
121-
if (identical(prop(input, "nrow"), NA_real_)) {
122-
prop(input, "nrow", check = FALSE) <- NULL
121+
if (identical(prop(x, "nrow"), NA_real_)) {
122+
prop(x, "nrow", check = FALSE) <- NULL
123123
}
124-
if (identical(prop(input, "byrow"), NA)) {
125-
prop(input, "byrow", check = FALSE) <- TRUE
124+
if (identical(prop(x, "byrow"), NA)) {
125+
prop(x, "byrow", check = FALSE) <- TRUE
126126
}
127-
prop(input, "widths", check = FALSE) <- prop(input, "widths") %||% NA
128-
prop(input, "heights", check = FALSE) <- prop(input, "heights") %||% NA
129-
prop(input, "area", check = FALSE) <- prop(input, "area") %|w|% NULL
130-
if (identical(prop(input, "guides"), NA_character_)) {
131-
prop(input, "guides", check = FALSE) <- waiver()
127+
prop(x, "widths", check = FALSE) <- prop(x, "widths") %||% NA
128+
prop(x, "heights", check = FALSE) <- prop(x, "heights") %||% NA
129+
prop(x, "area", check = FALSE) <- prop(x, "area") %|w|% NULL
130+
if (identical(prop(x, "guides"), NA_character_)) {
131+
prop(x, "guides", check = FALSE) <- waiver()
132132
}
133-
input
133+
x
134134
}
135135

136136
#' @importFrom ggplot2 is_waiver
137+
S7::method(ggalign_update, list(layout_design, layout_design)) <-
138+
function(x, object) {
139+
if (!identical(prop(object, "ncol"), NA_real_)) {
140+
prop(x, "ncol", check = FALSE) <- prop(object, "ncol")
141+
}
142+
if (!identical(prop(object, "nrow"), NA_real_)) {
143+
prop(x, "nrow", check = FALSE) <- prop(object, "nrow")
144+
}
145+
if (!identical(prop(object, "byrow"), NA)) {
146+
prop(x, "byrow", check = FALSE) <- prop(object, "byrow")
147+
}
148+
if (!is.null(prop(object, "widths"))) {
149+
prop(x, "widths", check = FALSE) <- prop(object, "widths")
150+
}
151+
if (!is.null(prop(object, "heights"))) {
152+
prop(x, "heights", check = FALSE) <- prop(object, "heights")
153+
}
154+
if (!is_waiver(prop(object, "area"))) {
155+
prop(x, "area", check = FALSE) <- prop(object, "area")
156+
}
157+
if (!identical(prop(object, "guides"), NA_character_)) {
158+
prop(x, "guides", check = FALSE) <- prop(object, "guides")
159+
}
160+
x
161+
}
162+
137163
local(
138164
S7::method(`+`, list(layout_design, layout_design)) <-
139-
function(e1, e2) {
140-
if (!identical(prop(e2, "ncol"), NA_real_)) {
141-
prop(e1, "ncol", check = FALSE) <- prop(e2, "ncol")
142-
}
143-
if (!identical(prop(e2, "nrow"), NA_real_)) {
144-
prop(e1, "nrow", check = FALSE) <- prop(e2, "nrow")
145-
}
146-
if (!identical(prop(e2, "byrow"), NA)) {
147-
prop(e1, "byrow", check = FALSE) <- prop(e2, "byrow")
148-
}
149-
if (!is.null(prop(e2, "widths"))) {
150-
prop(e1, "widths", check = FALSE) <- prop(e2, "widths")
151-
}
152-
if (!is.null(prop(e2, "heights"))) {
153-
prop(e1, "heights", check = FALSE) <- prop(e2, "heights")
154-
}
155-
if (!is_waiver(prop(e2, "area"))) {
156-
prop(e1, "area", check = FALSE) <- prop(e2, "area")
157-
}
158-
if (!identical(prop(e2, "guides"), NA_character_)) {
159-
prop(e1, "guides", check = FALSE) <- prop(e2, "guides")
160-
}
161-
e1
162-
}
165+
function(e1, e2) ggalign_update(e1, e2)
163166
)

0 commit comments

Comments
 (0)