Skip to content

Commit b4163e0

Browse files
committed
convert theme to S7
1 parent 0154671 commit b4163e0

File tree

4 files changed

+31
-30
lines changed

4 files changed

+31
-30
lines changed

NAMESPACE

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method("$","ggplot2::theme")
34
S3method("$",ggproto)
45
S3method("$",ggproto_parent)
5-
S3method("$",theme)
66
S3method("$<-",uneval)
77
S3method("+",gg)
88
S3method("[",mapped_discrete)
@@ -66,7 +66,6 @@ S3method(ggplot_add,data.frame)
6666
S3method(ggplot_add,default)
6767
S3method(ggplot_add,labels)
6868
S3method(ggplot_add,list)
69-
S3method(ggplot_add,theme)
7069
S3method(ggplot_add,uneval)
7170
S3method(ggplot_build,ggplot)
7271
S3method(ggplot_build,ggplot_built)

R/plot-construction.R

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,7 @@ ggplot_add.function <- function(object, plot, object_name) {
133133
"i" = "Did you forget to add parentheses, as in {.fn {object_name}}?"
134134
))
135135
}
136-
#' @export
137-
ggplot_add.theme <- function(object, plot, object_name) {
138-
plot$theme <- add_theme(plot$theme, object)
139-
plot
140-
}
136+
141137
#' @export
142138
ggplot_add.Scale <- function(object, plot, object_name) {
143139
plot$scales$add(object)

R/theme.R

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -549,25 +549,39 @@ theme <- function(...,
549549
el
550550
})
551551
}
552-
structure(
552+
S7::new_object(
553553
elements,
554-
class = c("theme", "gg"),
555554
complete = complete,
556555
validate = validate
557556
)
558557
}
559558

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+
560573
#' @export
561574
#' @rdname is_tests
562-
is.theme <- function(x) inherits(x, "theme")
575+
is.theme <- function(x) S7::S7_inherits(x, theme)
563576

564577
# 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+
}
566581

567582
# check whether theme should be validated
568583
is_theme_validate <- function(x) {
569-
validate <- attr(x, "validate", exact = TRUE)
570-
isTRUE(validate %||% TRUE)
584+
!is.theme(x) || isTRUE(x@validate)
571585
}
572586

573587
check_theme <- function(theme, tree = get_element_tree(), call = caller_env()) {
@@ -604,16 +618,9 @@ complete_theme <- function(theme = NULL, default = theme_get()) {
604618
}
605619
check_object(default, is.theme, "a {.cls theme} object")
606620
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
617624
}
618625

619626
# 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()) {
677684
}
678685
)
679686

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+
}
683690

684691
# 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)
687693

688694
t1
689695
}
@@ -949,7 +955,7 @@ combine_elements <- function(e1, e2) {
949955
}
950956

951957
#' @export
952-
`$.theme` <- function(x, ...) {
958+
`$.ggplot2::theme` <- function(x, ...) {
953959
.subset2(x, ...)
954960
}
955961

tests/testthat/test-theme.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ test_that("complete_theme completes a theme", {
634634
# `NULL` should match default
635635
gray <- theme_gray()
636636
new <- complete_theme(NULL, default = gray)
637-
expect_equal(new, gray, ignore_attr = "validate")
637+
expect_equal(S7::S7_data(new), S7::S7_data(gray))
638638

639639
# Elements are propagated
640640
new <- complete_theme(theme(axis.line = element_line("red")), gray)

0 commit comments

Comments
 (0)