@@ -797,69 +797,63 @@ calc_element <- function(element, theme, verbose = FALSE, skip_blank = FALSE,
797
797
# ' # Adopt size but ignore colour
798
798
# ' merge_element(new, old)
799
799
# '
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
+ }
803
812
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]}}." )
814
815
}
815
816
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
833
821
}
834
822
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
+ }
839
829
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
+ }
845
834
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 ])
848
840
849
- new
841
+ # Update non-NULL items
842
+ S7 :: props(new )[idx ] <- S7 :: props(old , idx )
843
+
844
+ new
850
845
}
851
846
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
860
856
}
861
- new
862
- }
863
857
864
858
# ' Combine the properties of two elements
865
859
# '
@@ -871,7 +865,7 @@ merge_element.margin <- function(new, old) {
871
865
combine_elements <- function (e1 , e2 ) {
872
866
873
867
# 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 )) {
875
869
return (e1 )
876
870
}
877
871
@@ -904,44 +898,44 @@ combine_elements <- function(e1, e2) {
904
898
}
905
899
906
900
# 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 )) {
908
902
return (e1 )
909
903
}
910
904
911
905
# If e2 is element_blank, and e1 inherits blank inherit everything from e2,
912
906
# 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 ) {
915
909
return (e2 )
916
910
} else {
917
911
return (e1 )
918
912
}
919
913
}
920
914
921
915
# 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 ]
924
918
925
919
# 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 )
928
922
}
929
923
930
924
# 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 )
933
927
}
934
928
935
929
if (inherits(e1 , " element_text" )) {
936
- e1 $ margin <- combine_elements(e1 $ margin , e2 $ margin )
930
+ e1 @ margin <- combine_elements(e1 @ margin , e2 @ margin )
937
931
}
938
932
939
933
# If e2 is 'richer' than e1, fill e2 with e1 parameters
940
934
is_subclass <- ! any(inherits(e2 , class(e1 ), which = TRUE ) == 0 )
941
935
is_subclass <- is_subclass && length(setdiff(class(e2 ), class(e1 )) > 0 )
942
936
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
945
939
return (e2 )
946
940
}
947
941
0 commit comments