Skip to content

Commit 8fc5ed9

Browse files
committed
fix non-element theme inheritance in R 3.1 and 3.2
1 parent 3ea7b2c commit 8fc5ed9

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

R/theme.r

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,21 +612,31 @@ merge_element.element <- function(new, old) {
612612
new
613613
}
614614

615-
# Combine the properties of two elements
616-
#
617-
# @param e1 An element object
618-
# @param e2 An element object which e1 inherits from
615+
#' Combine the properties of two elements
616+
#'
617+
#' @param e1 An element object
618+
#' @param e2 An element object from which e1 inherits
619+
#'
620+
#' @noRd
621+
#'
619622
combine_elements <- function(e1, e2) {
620623

621624
# If e2 is NULL, nothing to inherit
622625
if (is.null(e2) || inherits(e1, "element_blank")) return(e1)
626+
623627
# If e1 is NULL inherit everything from e2
624628
if (is.null(e1)) return(e2)
629+
630+
# If neither of e1 or e2 are element_* objects, return e1
631+
if (!inherits(e1, "element") && !inherits(e2, "element")) return(e1)
632+
625633
# If e2 is element_blank, and e1 inherits blank inherit everything from e2,
626634
# otherwise ignore e2
627635
if (inherits(e2, "element_blank")) {
628-
if (e1$inherit.blank) return(e2)
629-
else return(e1)
636+
if (e1$inherit.blank)
637+
return(e2)
638+
else
639+
return(e1)
630640
}
631641

632642
# If e1 has any NULL properties, inherit them from e2

tests/testthat/test-theme.r

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ test_that("elements can be merged", {
213213
)
214214
})
215215

216+
test_that("theme elements that don't inherit from element can be combined", {
217+
expect_identical(combine_elements(1, NULL), 1)
218+
expect_identical(combine_elements(NULL, 1), 1)
219+
expect_identical(combine_elements(1, 0), 1)
220+
})
221+
216222
test_that("complete plot themes shouldn't inherit from default", {
217223
default_theme <- theme_gray() + theme(axis.text.x = element_text(colour = "red"))
218224
base <- qplot(1, 1)

0 commit comments

Comments
 (0)