Skip to content

Commit df02b37

Browse files
committed
replace merge_element by S7
1 parent 202b80d commit df02b37

File tree

3 files changed

+60
-82
lines changed

3 files changed

+60
-82
lines changed

NAMESPACE

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ S3method(limits,character)
8787
S3method(limits,factor)
8888
S3method(limits,numeric)
8989
S3method(makeContext,dotstackGrob)
90-
S3method(merge_element,default)
91-
S3method(merge_element,element)
92-
S3method(merge_element,element_blank)
93-
S3method(merge_element,margin)
9490
S3method(pattern_alpha,GridPattern)
9591
S3method(pattern_alpha,GridTilingPattern)
9692
S3method(pattern_alpha,default)

R/theme.R

Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -797,69 +797,63 @@ calc_element <- function(element, theme, verbose = FALSE, skip_blank = FALSE,
797797
#' # Adopt size but ignore colour
798798
#' merge_element(new, old)
799799
#'
800-
merge_element <- function(new, old) {
801-
UseMethod("merge_element")
802-
}
800+
merge_element <- S7::new_generic("merge_element", c("new", "old"))
801+
802+
S7::method(merge_element, list(S7::class_any, S7::class_any)) <-
803+
function(new, old, ...) {
804+
if (is.null(old) || S7::S7_inherits(old, element_blank)) {
805+
# If old is NULL or element_blank, then just return new
806+
return(new)
807+
} else if (is.null(new) || is.character(new) || is.numeric(new) || is.unit(new) ||
808+
is.logical(new)) {
809+
# If new is NULL, or a string, numeric vector, unit, or logical, just return it
810+
return(new)
811+
}
803812

804-
#' @rdname merge_element
805-
#' @export
806-
merge_element.default <- function(new, old) {
807-
if (is.null(old) || inherits(old, "element_blank")) {
808-
# If old is NULL or element_blank, then just return new
809-
return(new)
810-
} else if (is.null(new) || is.character(new) || is.numeric(new) || is.unit(new) ||
811-
is.logical(new)) {
812-
# If new is NULL, or a string, numeric vector, unit, or logical, just return it
813-
return(new)
813+
# otherwise we can't merge
814+
cli::cli_abort("No method for merging {.cls {class(new)[1]}} into {.cls {class(old)[1]}}.")
814815
}
815816

816-
# otherwise we can't merge
817-
cli::cli_abort("No method for merging {.cls {class(new)[1]}} into {.cls {class(old)[1]}}.")
818-
}
819-
820-
#' @rdname merge_element
821-
#' @export
822-
merge_element.element_blank <- function(new, old) {
823-
# If new is element_blank, just return it
824-
new
825-
}
826-
827-
#' @rdname merge_element
828-
#' @export
829-
merge_element.element <- function(new, old) {
830-
if (is.null(old) || inherits(old, "element_blank")) {
831-
# If old is NULL or element_blank, then just return new
832-
return(new)
817+
S7::method(merge_element, list(element_blank, S7::class_any)) <-
818+
function(new, old, ...) {
819+
# If new is element_blank, just return it
820+
new
833821
}
834822

835-
# actual merging can only happen if classes match
836-
if (!inherits(new, class(old)[1])) {
837-
cli::cli_abort("Only elements of the same class can be merged.")
838-
}
823+
S7::method(merge_element, list(element, S7::class_any)) <-
824+
function(new, old, ...) {
825+
if (is.null(old) || S7::S7_inherits(old, element_blank)) {
826+
# If old is NULL or element_blank, then just return new
827+
return(new)
828+
}
839829

840-
# Override NULL properties of new with the values in old
841-
# Get logical vector of NULL properties in new
842-
idx <- vapply(new, is.null, logical(1))
843-
# Get the names of TRUE items
844-
idx <- names(idx[idx])
830+
# actual merging can only happen if classes match
831+
if (!inherits(new, class(old)[1])) {
832+
cli::cli_abort("Only elements of the same class can be merged.")
833+
}
845834

846-
# Update non-NULL items
847-
new[idx] <- old[idx]
835+
# Override NULL properties of new with the values in old
836+
# Get logical vector of NULL properties in new
837+
idx <- lengths(S7::props(new)) == 0
838+
# Get the names of TRUE items
839+
idx <- names(idx[idx])
848840

849-
new
841+
# Update non-NULL items
842+
S7::props(new)[idx] <- S7::props(old, idx)
843+
844+
new
850845
}
851846

852-
#' @rdname merge_element
853-
#' @export
854-
merge_element.margin <- function(new, old) {
855-
if (is.null(old) || inherits(old, "element_blank")) {
856-
return(new)
857-
}
858-
if (anyNA(new)) {
859-
new[is.na(new)] <- old[is.na(new)]
847+
S7::method(merge_element, list(S7::new_S3_class("margin"), S7::class_any)) <-
848+
function(new, old, ...) {
849+
if (is.null(old) || S7::S7_inherits(old, element_blank)) {
850+
return(new)
851+
}
852+
if (anyNA(new)) {
853+
new[is.na(new)] <- old[is.na(new)]
854+
}
855+
new
860856
}
861-
new
862-
}
863857

864858
#' Combine the properties of two elements
865859
#'
@@ -871,7 +865,7 @@ merge_element.margin <- function(new, old) {
871865
combine_elements <- function(e1, e2) {
872866

873867
# If e2 is NULL, nothing to inherit
874-
if (is.null(e2) || inherits(e1, "element_blank")) {
868+
if (is.null(e2) || S7::S7_inherits(e1, element_blank)) {
875869
return(e1)
876870
}
877871

@@ -904,44 +898,44 @@ combine_elements <- function(e1, e2) {
904898
}
905899

906900
# If neither of e1 or e2 are element_* objects, return e1
907-
if (!inherits(e1, "element") && !inherits(e2, "element")) {
901+
if (!S7::S7_inherits(e1, element) && !S7::S7_inherits(e2, element)) {
908902
return(e1)
909903
}
910904

911905
# If e2 is element_blank, and e1 inherits blank inherit everything from e2,
912906
# otherwise ignore e2
913-
if (inherits(e2, "element_blank")) {
914-
if (e1$inherit.blank) {
907+
if (S7::S7_inherits(e2, element_blank)) {
908+
if (S7::prop_exists(e1, "inherit.blank") && e1@inherit.blank) {
915909
return(e2)
916910
} else {
917911
return(e1)
918912
}
919913
}
920914

921915
# If e1 has any NULL properties, inherit them from e2
922-
n <- names(e1)[vapply(e1, is.null, logical(1))]
923-
e1[n] <- e2[n]
916+
n <- S7::prop_names(e1)[lengths(S7::props(e1)) == 0]
917+
S7::props(e1)[n] <- S7::props(e2)[n]
924918

925919
# Calculate relative sizes
926-
if (is.rel(e1$size)) {
927-
e1$size <- e2$size * unclass(e1$size)
920+
if (S7::prop_exists(e1, "size") && is.rel(e1@size)) {
921+
e1@size <- e2@size * unclass(e1@size)
928922
}
929923

930924
# Calculate relative linewidth
931-
if (is.rel(e1$linewidth)) {
932-
e1$linewidth <- e2$linewidth * unclass(e1$linewidth)
925+
if (S7::prop_exists(e1, "linewidth") && is.rel(e1@linewidth)) {
926+
e1@linewidth <- e2@linewidth * unclass(e1@linewidth)
933927
}
934928

935929
if (inherits(e1, "element_text")) {
936-
e1$margin <- combine_elements(e1$margin, e2$margin)
930+
e1@margin <- combine_elements(e1@margin, e2@margin)
937931
}
938932

939933
# If e2 is 'richer' than e1, fill e2 with e1 parameters
940934
is_subclass <- !any(inherits(e2, class(e1), which = TRUE) == 0)
941935
is_subclass <- is_subclass && length(setdiff(class(e2), class(e1)) > 0)
942936
if (is_subclass) {
943-
new <- defaults(e1, e2)
944-
e2[names(new)] <- new
937+
new <- defaults(S7::props(e1), S7::props(e2))
938+
S7::props(e2)[names(new)] <- new
945939
return(e2)
946940
}
947941

man/merge_element.Rd

Lines changed: 1 addition & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)