Skip to content

Commit 09eef8d

Browse files
committed
Use S7 properties
1 parent b038e8f commit 09eef8d

File tree

10 files changed

+83
-75
lines changed

10 files changed

+83
-75
lines changed

R/coord-sf.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,13 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
334334

335335
# we don't draw the graticules if the major panel grid is
336336
# turned off
337-
if (inherits(el, "element_blank")) {
337+
if (S7::S7_inherits(el, element_blank)) {
338338
grobs <- list(element_render(theme, "panel.background"))
339339
} else {
340340
line_gp <- gg_par(
341-
col = el$colour,
342-
lwd = el$linewidth,
343-
lty = el$linetype
341+
col = el@colour,
342+
lwd = el@linewidth,
343+
lty = el@linetype
344344
)
345345
grobs <- c(
346346
list(element_render(theme, "panel.background")),

R/geom-.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ eval_from_theme <- function(aesthetics, theme) {
248248
return(aesthetics)
249249
}
250250
settings <- calc_element("geom", theme) %||% .default_geom_element
251-
lapply(aesthetics[themed], eval_tidy, data = settings)
251+
lapply(aesthetics[themed], eval_tidy, data = S7::props(settings))
252252
}
253253

254254
#' Graphical units

R/guide-axis-theta.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ GuideAxisTheta <- ggproto(
153153
}
154154

155155
offset <- max(unit(0, "pt"), elements$major_length, elements$minor_length)
156-
elements$offset <- offset + max(elements$text$margin %||% unit(0, "pt"))
156+
elements$offset <- offset + max(elements$text@margin %||% unit(0, "pt"))
157157
elements
158158
},
159159

@@ -197,7 +197,7 @@ GuideAxisTheta <- ggproto(
197197

198198
# Resolve text angle
199199
if (is.waiver(params$angle) || is.null(params$angle)) {
200-
angle <- elements$text$angle
200+
angle <- elements$text@angle
201201
} else {
202202
angle <- flip_text_angle(params$angle - rad2deg(key$theta))
203203
}
@@ -273,14 +273,14 @@ GuideAxisTheta <- ggproto(
273273

274274
# Resolve text angle
275275
if (is.waiver(params$angle %||% waiver())) {
276-
angle <- elements$text$angle
276+
angle <- elements$text@angle
277277
} else {
278278
angle <- flip_text_angle(params$angle - rad2deg(key$theta))
279279
}
280280
angle <- key$theta + deg2rad(angle)
281281

282282
# Set margin
283-
margin <- rep(max(elements$text$margin), length.out = 4)
283+
margin <- rep(max(elements$text@margin), length.out = 4)
284284

285285
# Measure size of each individual label
286286
single_labels <- lapply(labels, function(lab) {

R/guide-axis.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,13 +450,13 @@ GuideAxis <- ggproto(
450450
# rather than dimensions of this axis alone.
451451
if (has_labels && params$position %in% c("left", "right")) {
452452
where <- layout$l[-c(1, length(layout$l))]
453-
just <- with(elements$text, rotate_just(angle, hjust, vjust))$hjust %||% 0.5
453+
just <- with(S7::props(elements$text), rotate_just(angle, hjust, vjust))$hjust %||% 0.5
454454
gt <- gtable_add_cols(gt, unit(just, "null"), pos = min(where) - 1)
455455
gt <- gtable_add_cols(gt, unit(1 - just, "null"), pos = max(where) + 1)
456456
}
457457
if (has_labels && params$position %in% c("top", "bottom")) {
458458
where <- layout$t[-c(1, length(layout$t))]
459-
just <- with(elements$text, rotate_just(angle, hjust, vjust))$vjust %||% 0.5
459+
just <- with(S7::props(elements$text), rotate_just(angle, hjust, vjust))$vjust %||% 0.5
460460
gt <- gtable_add_rows(gt, unit(1 - just, "null"), pos = min(where) - 1)
461461
gt <- gtable_add_rows(gt, unit(just, "null"), pos = max(where) + 1)
462462
}
@@ -612,8 +612,8 @@ label_angle_heuristic <- function(element, position, angle) {
612612
hjust <- switch(position, left = cosine, right = 1 - cosine, top = 1 - sine, sine)
613613
vjust <- switch(position, left = 1 - sine, right = sine, top = 1 - cosine, cosine)
614614

615-
element$angle <- angle %||% element$angle
616-
element$hjust <- hjust %||% element$hjust
617-
element$vjust <- vjust %||% element$vjust
615+
element@angle <- angle %||% element@angle
616+
element@hjust <- hjust %||% element@hjust
617+
element@vjust <- vjust %||% element@vjust
618618
element
619619
}

R/guide-custom.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ GuideCustom <- ggproto(
113113

114114
gt <- self$add_title(
115115
gt, title, title_position,
116-
with(elems$title, rotate_just(angle, hjust, vjust))
116+
with(S7::props(elems$title), rotate_just(angle, hjust, vjust))
117117
)
118118

119119
# Add padding and background

R/guide-legend.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ GuideLegend <- ggproto(
325325
# Resolve title. The trick here is to override the main text element, so
326326
# that any settings declared in `legend.title` will be honoured but we have
327327
# custom defaults for the guide.
328-
margin <- calc_element("text", theme)$margin
328+
margin <- calc_element("text", theme)@margin
329329
title <- theme(text = element_text(
330330
hjust = 0, vjust = 0.5,
331331
margin = position_margin(title_position, margin, gap)
@@ -573,7 +573,7 @@ GuideLegend <- ggproto(
573573

574574
gt <- self$add_title(
575575
gt, grobs$title, elements$title_position,
576-
with(elements$title, rotate_just(angle, hjust, vjust))
576+
with(S7::props(elements$title), rotate_just(angle, hjust, vjust))
577577
)
578578

579579
gt <- gtable_add_padding(gt, unit(elements$padding, "cm"))

R/plot-build.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,20 +387,20 @@ table_add_tag <- function(table, label, theme) {
387387
if (location %in% c("plot", "panel")) {
388388
if (!is.numeric(position)) {
389389
if (right || left) {
390-
x <- (1 - element$hjust) * width
390+
x <- (1 - element@hjust) * width
391391
if (right) {
392392
x <- unit(1, "npc") - x
393393
}
394394
} else {
395-
x <- unit(element$hjust, "npc")
395+
x <- unit(element@hjust, "npc")
396396
}
397397
if (top || bottom) {
398-
y <- (1 - element$vjust) * height
398+
y <- (1 - element@vjust) * height
399399
if (top) {
400400
y <- unit(1, "npc") - y
401401
}
402402
} else {
403-
y <- unit(element$vjust, "npc")
403+
y <- unit(element@vjust, "npc")
404404
}
405405
} else {
406406
x <- unit(position[1], "npc")

R/theme.R

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,14 +746,22 @@ calc_element <- function(element, theme, verbose = FALSE, skip_blank = FALSE,
746746
if (verbose) cli::cli_inform("nothing (top level)")
747747

748748
# Check that all the properties of this element are non-NULL
749-
nullprops <- vapply(el_out, is.null, logical(1))
749+
if (inherits(el_out, "ggplot2::element")) {
750+
nullprops <- lengths(S7::props(el_out)) == 0
751+
} else {
752+
nullprops <- vapply(el_out, is.null, logical(1))
753+
}
750754
if (!any(nullprops)) {
751755
return(el_out) # no null properties, return element as is
752756
}
753757

754758
# if we have null properties, try to fill in from ggplot_global$theme_default
755759
el_out <- combine_elements(el_out, ggplot_global$theme_default[[element]])
756-
nullprops <- vapply(el_out, is.null, logical(1))
760+
if (inherits(el_out, "ggplot2::element")) {
761+
nullprops <- lengths(S7::props(el_out)) == 0
762+
} else {
763+
nullprops <- vapply(el_out, is.null, logical(1))
764+
}
757765
if (!any(nullprops)) {
758766
return(el_out) # no null properties remaining, return element
759767
}

tests/testthat/_snaps/theme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# incorrect theme specifications throw meaningful errors
2626

2727
Can't merge the `line` theme element.
28-
Caused by error in `merge_element()`:
28+
Caused by error in `method(merge_element, list(ggplot2::element, class_any))`:
2929
! Only elements of the same class can be merged.
3030

3131
---
@@ -74,7 +74,7 @@
7474
Code
7575
merge_element(text_base, rect_base)
7676
Condition
77-
Error in `merge_element()`:
77+
Error in `method(merge_element, list(ggplot2::element, class_any))`:
7878
! Only elements of the same class can be merged.
7979

8080
# Theme elements are checked during build

0 commit comments

Comments
 (0)