Skip to content

Commit 27cdb73

Browse files
committed
fix misc issues
1 parent 62d8db4 commit 27cdb73

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

R/guide-.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ Guide <- ggproto(
376376
# Renders tickmarks
377377
build_ticks = function(key, elements, params, position = params$position,
378378
length = elements$ticks_length) {
379+
force(length)
379380
if (!is.theme_element(elements)) {
380381
elements <- elements$ticks
381382
}

R/guide-legend.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,13 +690,15 @@ keep_key_data <- function(key, data, aes, show) {
690690

691691
position_margin <- function(position, margin = NULL, gap = unit(0, "pt")) {
692692
margin <- margin %||% margin()
693-
switch(
693+
margin <- switch(
694694
position,
695695
top = replace(margin, 3, margin[3] + gap),
696696
bottom = replace(margin, 1, margin[1] + gap),
697697
left = replace(margin, 2, margin[2] + gap),
698698
right = replace(margin, 4, margin[4] + gap)
699699
)
700+
class(margin) <- union("margin", class(margin))
701+
margin
700702
}
701703

702704
# Function implementing backward compatibility with the old way of specifying

R/theme-elements.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ check_element <- function(el, elname, element_tree, call = caller_env()) {
858858
}
859859
}
860860

861-
if ("margin" %in% class) {
861+
if (is.character(class) && "margin" %in% class) {
862862
if (!is.unit(el) && length(el) == 4)
863863
cli::cli_abort("The {.var {elname}} theme element must be a {.cls unit} vector of length 4.", call = call)
864864
} else if (!inherits(el, class) && !S7::S7_inherits(el, element_blank)) {

tests/testthat/test-theme.R

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,10 @@ test_that("calculating theme element inheritance works", {
142142
expect_identical(calc_element('axis.title.x', t), element_blank())
143143

144144
# Check that inheritance from derived class works
145-
element_dummyrect <- function(dummy) { # like element_rect but w/ dummy argument
146-
structure(list(
147-
fill = NULL, colour = NULL, dummy = dummy, linewidth = NULL,
148-
linetype = NULL, inherit.blank = FALSE
149-
), class = c("element_dummyrect", "element_rect", "element"))
150-
}
145+
element_dummyrect <- S7::new_class(
146+
"element_dummyrect", parent = element_rect,
147+
properties = c(element_rect@properties, list(dummy = S7::class_any))
148+
)
151149

152150
e <- calc_element(
153151
"panel.background",
@@ -160,10 +158,10 @@ test_that("calculating theme element inheritance works", {
160158

161159
expect_identical(
162160
e,
163-
structure(list(
161+
element_dummyrect(
164162
fill = "white", colour = "black", dummy = 5, linewidth = 0.5, linetype = 1,
165163
inherit.blank = TRUE # this is true because we're requesting a complete theme
166-
), class = c("element_dummyrect", "element_rect", "element"))
164+
)
167165
)
168166

169167
# Check that blank elements are skipped in inheritance tree if and only if elements
@@ -283,9 +281,10 @@ test_that("theme validation happens at build stage", {
283281
test_that("incorrect theme specifications throw meaningful errors", {
284282
expect_snapshot_error(add_theme(theme_grey(), theme(line = element_rect())))
285283
expect_snapshot_error(calc_element("line", theme(line = element_rect())))
286-
register_theme_elements(element_tree = list(test = el_def("element_rect")))
284+
register_theme_elements(element_tree = list(test = el_def(element_rect)))
287285
expect_snapshot_error(calc_element("test", theme_gray() + theme(test = element_rect())))
288286
expect_snapshot_error(set_theme("foo"))
287+
reset_theme_settings()
289288
})
290289

291290
test_that("element tree can be modified", {
@@ -305,7 +304,7 @@ test_that("element tree can be modified", {
305304

306305
# things work once we add a new element to the element tree
307306
register_theme_elements(
308-
element_tree = list(blablabla = el_def("element_text", "text"))
307+
element_tree = list(blablabla = el_def(element_text, "text"))
309308
)
310309
expect_silent(ggplotGrob(p))
311310

@@ -334,13 +333,13 @@ test_that("element tree can be modified", {
334333
expect_identical(e1@colour, "red") # not inherited from element_text
335334

336335
# existing elements can be overwritten
337-
ed <- el_def("element_rect", "rect")
336+
ed <- el_def(element_rect, "rect")
338337
register_theme_elements(
339338
element_tree = list(axis.title = ed)
340339
)
341340
expect_identical(get_element_tree()$axis.title, ed)
342341

343-
reset_theme_settings(reset_current = FALSE) # revert back to defaults
342+
reset_theme_settings() # revert back to defaults
344343
})
345344

346345
test_that("all elements in complete themes have inherit.blank=TRUE", {
@@ -424,7 +423,7 @@ test_that("current theme can be updated with new elements", {
424423
# element tree gets merged properly
425424
register_theme_elements(
426425
abcde = element_text(color = "blue", hjust = 0, vjust = 1),
427-
element_tree = list(abcde = el_def("element_text", "text"))
426+
element_tree = list(abcde = el_def(element_text, "text"))
428427
)
429428

430429
e1 <- calc_element("abcde", plot_theme(b2))
@@ -647,7 +646,7 @@ test_that("complete_theme completes a theme", {
647646
# Registered elements are included
648647
register_theme_elements(
649648
test = element_text(),
650-
element_tree = list(test = el_def("element_text", "text"))
649+
element_tree = list(test = el_def(element_text, "text"))
651650
)
652651
new <- complete_theme(default = gray)
653652
expect_s3_class(new$test, "ggplot2::element_text")
@@ -959,15 +958,11 @@ test_that("Legends can on all sides of the plot with custom justification", {
959958
})
960959

961960
test_that("Strips can render custom elements", {
962-
element_test <- function(...) {
963-
el <- element_text(...)
964-
class(el) <- c('element_test', 'element_text', 'element')
965-
el
966-
}
967-
element_grob.element_test <- function(element, label = "", x = NULL, y = NULL, ...) {
968-
rectGrob(width = unit(1, "cm"), height = unit(1, "cm"))
969-
}
970-
registerS3method("element_grob", "element_test", element_grob.element_test)
961+
element_test <- S7::new_class("element_test", element_text)
962+
S7::method(element_grob, element_test) <-
963+
function(element, label = "", x = NULL, y = NULL, ...) {
964+
rectGrob(width = unit(1, "cm"), height = unit(1, "cm"))
965+
}
971966

972967
df <- data_frame(x = 1:3, y = 1:3, a = letters[1:3])
973968
plot <- ggplot(df, aes(x, y)) +

0 commit comments

Comments
 (0)