@@ -549,25 +549,39 @@ theme <- function(...,
549
549
el
550
550
})
551
551
}
552
- structure (
552
+ S7 :: new_object (
553
553
elements ,
554
- class = c(" theme" , " gg" ),
555
554
complete = complete ,
556
555
validate = validate
557
556
)
558
557
}
559
558
559
+ theme <- S7 :: new_class(
560
+ " theme" , S7 :: new_S3_class(" gg" ),
561
+ properties = list (
562
+ complete = S7 :: class_logical ,
563
+ validate = S7 :: class_logical
564
+ ),
565
+ constructor = theme
566
+ )
567
+
568
+ S7 :: method(ggplot_add , theme ) <- function (object , plot , object_name , ... ) {
569
+ plot $ theme <- add_theme(plot $ theme , object )
570
+ plot
571
+ }
572
+
560
573
# ' @export
561
574
# ' @rdname is_tests
562
- is.theme <- function (x ) inherits (x , " theme" )
575
+ is.theme <- function (x ) S7 :: S7_inherits (x , theme )
563
576
564
577
# check whether theme is complete
565
- is_theme_complete <- function (x ) isTRUE(attr(x , " complete" , exact = TRUE ))
578
+ is_theme_complete <- function (x ) {
579
+ is.theme(x ) && isTRUE(x @ complete )
580
+ }
566
581
567
582
# check whether theme should be validated
568
583
is_theme_validate <- function (x ) {
569
- validate <- attr(x , " validate" , exact = TRUE )
570
- isTRUE(validate %|| % TRUE )
584
+ ! is.theme(x ) || isTRUE(x @ validate )
571
585
}
572
586
573
587
check_theme <- function (theme , tree = get_element_tree(), call = caller_env()) {
@@ -604,16 +618,9 @@ complete_theme <- function(theme = NULL, default = theme_get()) {
604
618
}
605
619
check_object(default , is.theme , " a {.cls theme} object" )
606
620
theme <- plot_theme(list (theme = theme ), default = default )
607
-
608
- # Using `theme(!!!theme)` drops `NULL` entries, so strip most attributes and
609
- # construct a new theme
610
- attributes(theme ) <- list (names = attr(theme , " names" ))
611
- structure(
612
- theme ,
613
- class = c(" theme" , " gg" ),
614
- complete = TRUE , # This theme is complete and has no missing elements
615
- validate = FALSE # Settings have already been validated
616
- )
621
+ theme @ complete <- TRUE
622
+ theme @ validate <- FALSE
623
+ theme
617
624
}
618
625
619
626
# Combine plot defaults with current theme to get complete theme for a plot
@@ -677,13 +684,12 @@ add_theme <- function(t1, t2, t2name, call = caller_env()) {
677
684
}
678
685
)
679
686
680
- # make sure the "complete" attribute is set; this can be missing
681
- # when t1 is an empty list
682
- attr( t1 , " complete " ) <- is_theme_complete( t1 )
687
+ if ( ! is.theme( t1 ) && is.list( t1 )) {
688
+ t1 <- theme( !!! t1 )
689
+ }
683
690
684
691
# Only validate if both themes should be validated
685
- attr(t1 , " validate" ) <-
686
- is_theme_validate(t1 ) && is_theme_validate(t2 )
692
+ t1 @ validate <- is_theme_validate(t1 ) && is_theme_validate(t2 )
687
693
688
694
t1
689
695
}
@@ -949,7 +955,7 @@ combine_elements <- function(e1, e2) {
949
955
}
950
956
951
957
# ' @export
952
- `$.theme` <- function (x , ... ) {
958
+ `$.ggplot2:: theme` <- function (x , ... ) {
953
959
.subset2(x , ... )
954
960
}
955
961
0 commit comments