Skip to content

Theme element calculation fails for derived element classes #3549

Closed
@clauswilke

Description

@clauswilke

The internal function calc_element() assumes that parent and child elements always have exactly the same set of member variables. In the general case, this is not going to be the case, in particular if we want to enable new elements such as element_markdown() or element_textbox().

Reprex:

library(ggplot2)

# like element_rect but with a meaningless dummy argument
element_dummyrect <- function(fill = NULL, colour = NULL, dummy = NULL, size = NULL, 
                              linetype = NULL) {
  structure(
    list(
      fill = fill, colour = colour, dummy = dummy, size = size,
      linetype = linetype, inherit.blank = FALSE
    ),
    class = c("element_dummyrect", "element_rect", "element")
  )
}

ggplot2:::calc_element(
  "panel.background",
  theme(
    rect = element_rect(fill = "white", colour = "black", size = 0.5, linetype = 1),
    panel.background = element_dummyrect(dummy = 5))
)
#> Warning in e1[n] <- e2[n]: number of items to replace is not a multiple of
#> replacement length
#> List of 6
#>  $ fill         : chr "white"
#>  $ colour       : chr "black"
#>  $ dummy        : num 0.5
#>  $ size         : num 1
#>  $ linetype     : NULL
#>  $ inherit.blank: chr "white"
#>  - attr(*, "class")= chr [1:3] "element_dummyrect" "element_rect" "element"

Created on 2019-10-04 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.0 (2019-04-26)
#>  os       macOS Mojave 10.14.5        
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/Chicago             
#>  date     2019-10-04                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version    date       lib source        
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 3.6.0)
#>  backports     1.1.4      2019-04-10 [1] CRAN (R 3.6.0)
#>  callr         3.3.1      2019-07-18 [1] CRAN (R 3.6.0)
#>  cli           1.1.0      2019-03-19 [1] CRAN (R 3.6.0)
#>  colorspace    1.4-1      2019-03-18 [1] CRAN (R 3.6.0)
#>  crayon        1.3.4      2017-09-16 [1] CRAN (R 3.6.0)
#>  desc          1.2.0      2018-05-01 [1] CRAN (R 3.6.0)
#>  devtools      2.0.2      2019-04-08 [1] CRAN (R 3.6.0)
#>  digest        0.6.20     2019-07-04 [1] CRAN (R 3.6.0)
#>  dplyr         0.8.3      2019-07-04 [1] CRAN (R 3.6.0)
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 3.6.0)
#>  fs            1.3.1      2019-05-06 [1] CRAN (R 3.6.0)
#>  ggplot2     * 3.2.1.9000 2019-10-04 [1] local         
#>  glue          1.3.1      2019-03-12 [1] CRAN (R 3.6.0)
#>  gtable        0.3.0      2019-03-25 [1] CRAN (R 3.6.0)
#>  highr         0.8        2019-03-20 [1] CRAN (R 3.6.0)
#>  htmltools     0.3.6      2017-04-28 [1] CRAN (R 3.6.0)
#>  knitr         1.25       2019-09-18 [1] CRAN (R 3.6.0)
#>  magrittr      1.5        2014-11-22 [1] CRAN (R 3.6.0)
#>  memoise       1.1.0      2017-04-21 [1] CRAN (R 3.6.0)
#>  munsell       0.5.0      2018-06-12 [1] CRAN (R 3.6.0)
#>  pillar        1.4.2      2019-06-29 [1] CRAN (R 3.6.0)
#>  pkgbuild      1.0.4      2019-08-05 [1] CRAN (R 3.6.0)
#>  pkgconfig     2.0.2      2018-08-16 [1] CRAN (R 3.6.0)
#>  pkgload       1.0.2      2018-10-29 [1] CRAN (R 3.6.0)
#>  prettyunits   1.0.2      2015-07-13 [1] CRAN (R 3.6.0)
#>  processx      3.4.1      2019-07-18 [1] CRAN (R 3.6.0)
#>  ps            1.3.0      2018-12-21 [1] CRAN (R 3.6.0)
#>  purrr         0.3.2      2019-03-15 [1] CRAN (R 3.6.0)
#>  R6            2.4.0      2019-02-14 [1] CRAN (R 3.6.0)
#>  Rcpp          1.0.2      2019-07-25 [1] CRAN (R 3.6.0)
#>  remotes       2.1.0      2019-06-24 [1] CRAN (R 3.6.0)
#>  rlang         0.4.0      2019-06-25 [1] CRAN (R 3.6.0)
#>  rmarkdown     1.15       2019-08-21 [1] CRAN (R 3.6.0)
#>  rprojroot     1.3-2      2018-01-03 [1] CRAN (R 3.6.0)
#>  scales        1.0.0      2018-08-09 [1] CRAN (R 3.6.0)
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.6.0)
#>  stringi       1.4.3      2019-03-12 [1] CRAN (R 3.6.0)
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 3.6.0)
#>  testthat      2.1.1      2019-04-23 [1] CRAN (R 3.6.0)
#>  tibble        2.1.3      2019-06-06 [1] CRAN (R 3.6.0)
#>  tidyselect    0.2.5      2018-10-11 [1] CRAN (R 3.6.0)
#>  usethis       1.5.0      2019-04-07 [1] CRAN (R 3.6.0)
#>  withr         2.1.2      2018-03-15 [1] CRAN (R 3.6.0)
#>  xfun          0.9        2019-08-21 [1] CRAN (R 3.6.0)
#>  yaml          2.2.0      2018-07-25 [1] CRAN (R 3.6.0)
#> 
#> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library

The offending code is here:

ggplot2/R/theme.r

Lines 657 to 659 in 047b528

# If e1 has any NULL properties, inherit them from e2
n <- vapply(e1[names(e2)], is.null, logical(1))
e1[n] <- e2[n]

Because it uses logical indexing instead of name indexing, it breaks if the two elements have different named components. I will prepare a fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions