Skip to content

Commit 8b07991

Browse files
authored
Simplify zeroGrob() (#6391)
* cleanup branch * For backward compatibility, `zeroGrob()` returns a `nullGrob()` subclass * adjust tests * add news bullet * migrate to grob utilities * avoid including fragile grobName in snapshot * use `add_class()` in other places too * fix misresolution of merge conflict
1 parent 7caada4 commit 8b07991

File tree

13 files changed

+28
-42
lines changed

13 files changed

+28
-42
lines changed

DESCRIPTION

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ Collate:
177177
'ggplot2-package.R'
178178
'grob-absolute.R'
179179
'grob-dotstack.R'
180-
'grob-null.R'
181180
'grouping.R'
182181
'properties.R'
183182
'margins.R'

NAMESPACE

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ S3method(as.list,ggproto)
2020
S3method(autolayer,default)
2121
S3method(autoplot,default)
2222
S3method(c,mapped_discrete)
23-
S3method(drawDetails,zeroGrob)
2423
S3method(format,ggproto)
2524
S3method(format,ggproto_method)
2625
S3method(format,rd_section_aesthetics)
@@ -52,9 +51,7 @@ S3method(ggplot,default)
5251
S3method(ggplot_add,default)
5352
S3method(grid.draw,absoluteGrob)
5453
S3method(grobHeight,absoluteGrob)
55-
S3method(grobHeight,zeroGrob)
5654
S3method(grobWidth,absoluteGrob)
57-
S3method(grobWidth,zeroGrob)
5855
S3method(grobX,absoluteGrob)
5956
S3method(grobY,absoluteGrob)
6057
S3method(guide_gengrob,default)
@@ -63,7 +60,6 @@ S3method(guide_merge,default)
6360
S3method(guide_train,default)
6461
S3method(guide_transform,default)
6562
S3method(heightDetails,titleGrob)
66-
S3method(heightDetails,zeroGrob)
6763
S3method(limits,Date)
6864
S3method(limits,POSIXct)
6965
S3method(limits,POSIXlt)
@@ -117,7 +113,6 @@ S3method(vec_ptype2,mapped_discrete.factor)
117113
S3method(vec_ptype2,mapped_discrete.integer)
118114
S3method(vec_ptype2,mapped_discrete.mapped_discrete)
119115
S3method(widthDetails,titleGrob)
120-
S3method(widthDetails,zeroGrob)
121116
export("%+%")
122117
export("%+replace%")
123118
export(.data)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# ggplot2 (development version)
22

3+
* (internal) `zeroGrob()` now returns a `grid::nullGrob()` (#6390).
34
* `stat_ydensity()` now only requires the `x` or `y` aesthetic. The other will
45
be populated with 0, similar to `stat_boxplot()` (@teunbrand, #6600)
56
* Implemented `as.list()` and `S7::convert()` methods for lists and classes in ggplot2

R/aes-variants.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ aes_all <- function(vars) {
113113
# Quosure the symbols in the empty environment because they can only
114114
# refer to the data mask
115115
x <- class_mapping(lapply(vars, function(x) new_quosure(as.name(x), emptyenv())))
116-
class(x) <- union("unlabelled", class(x))
117-
x
116+
add_class(x, "unlabelled")
118117
}
119118

120119
#' Automatic aesthetic mapping

R/grob-null.R

Lines changed: 0 additions & 26 deletions
This file was deleted.

R/theme.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ theme <- function(...,
501501
}
502502

503503
t <- class_theme(elements, complete = complete, validate = validate)
504-
class(t) <- union("theme", class(t))
504+
t <- add_class(t, "theme")
505505
t
506506
}
507507

R/utilities-grid.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ gg_par <- function(..., stroke = NULL, pointsize = NULL) {
5353
inject(gpar(!!!args))
5454
}
5555

56+
#' The zero grob draws nothing and has zero size.
57+
#'
58+
#' @keywords internal
59+
#' @export
60+
zeroGrob <- function() .zeroGrob
61+
62+
.zeroGrob <- NULL
63+
on_load(.zeroGrob <- add_class(nullGrob(), "zeroGrob"))
64+
65+
is_zero <- function(x) is.null(x) || inherits(x, "null")
66+
5667
width_cm <- function(x) {
5768
if (is.grob(x)) {
5869
convertWidth(grobWidth(x), "cm", TRUE)

R/utilities.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,15 @@ compute_data_size <- function(data, size, default = 0.9,
942942
data
943943
}
944944

945+
add_class <- function(x, new_class) {
946+
new_class <- setdiff(new_class, class(x))
947+
if (length(new_class) < 1) {
948+
return(x)
949+
}
950+
class(x) <- union(new_class, class(x))
951+
x
952+
}
953+
945954
try_prop <- function(object, name, default = NULL) {
946955
if (!S7::S7_inherits(object)) {
947956
return(default)

man/zeroGrob.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/theme-elements.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@
6464
---
6565

6666
Code
67-
element_grob(el, label = element_blank())
67+
x <- element_grob(el, label = element_blank())
6868
Condition
6969
Warning:
7070
`label` cannot be a <ggplot2::element_blank> object.
71-
Output
72-
zeroGrob[NULL]
7371

0 commit comments

Comments
 (0)