Skip to content

Commit c1908f1

Browse files
authored
Move global variables into an environment. (#2741)
1 parent 3129bf8 commit c1908f1

File tree

10 files changed

+59
-39
lines changed

10 files changed

+59
-39
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ BugReports: https://github.com/tidyverse/ggplot2/issues
6161
LazyData: true
6262
Collate:
6363
'ggproto.r'
64+
'ggplot-global.R'
6465
'aaa-.r'
6566
'aes-calculated.r'
6667
'aes-colour-fill-alpha.r'

R/aaa-.r

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#' @include ggplot-global.R
12
#' @include ggproto.r
23
NULL
34

R/aes.r

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,6 @@
11
#' @include utilities.r
22
NULL
33

4-
.all_aesthetics <- c("adj", "alpha", "angle", "bg", "cex", "col", "color",
5-
"colour", "fg", "fill", "group", "hjust", "label", "linetype", "lower",
6-
"lty", "lwd", "max", "middle", "min", "pch", "radius", "sample", "shape",
7-
"size", "srt", "upper", "vjust", "weight", "width", "x", "xend", "xmax",
8-
"xmin", "xintercept", "y", "yend", "ymax", "ymin", "yintercept", "z")
9-
10-
.base_to_ggplot <- c(
11-
"col" = "colour",
12-
"color" = "colour",
13-
"pch" = "shape",
14-
"cex" = "size",
15-
"lty" = "linetype",
16-
"lwd" = "size",
17-
"srt" = "angle",
18-
"adj" = "hjust",
19-
"bg" = "fill",
20-
"fg" = "colour",
21-
"min" = "ymin",
22-
"max" = "ymax"
23-
)
24-
254
#' Construct aesthetic mappings
265
#'
276
#' Aesthetic mappings describe how variables in the data are mapped to visual
@@ -167,10 +146,10 @@ print.uneval <- function(x, ...) {
167146
# Rename American or old-style aesthetics name
168147
rename_aes <- function(x) {
169148
# Convert prefixes to full names
170-
full <- match(names(x), .all_aesthetics)
171-
names(x)[!is.na(full)] <- .all_aesthetics[full[!is.na(full)]]
149+
full <- match(names(x), ggplot_global$all_aesthetics)
150+
names(x)[!is.na(full)] <- ggplot_global$all_aesthetics[full[!is.na(full)]]
172151

173-
plyr::rename(x, .base_to_ggplot, warn_missing = FALSE)
152+
plyr::rename(x, ggplot_global$base_to_ggplot, warn_missing = FALSE)
174153
}
175154

176155
# Look up the scale that should be used for a given aesthetic
@@ -315,7 +294,7 @@ aes_auto <- function(data = NULL, ...) {
315294
}
316295

317296
# automatically detected aes
318-
vars <- intersect(.all_aesthetics, vars)
297+
vars <- intersect(ggplot_global$all_aesthetics, vars)
319298
names(vars) <- vars
320299
aes <- lapply(vars, function(x) parse(text = x)[[1]])
321300

R/ggplot-global.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Environment that holds various global variables and settings for ggplot,
2+
# such as the current theme. It is not exported and should not be directly
3+
# manipulated by other packages.
4+
ggplot_global <- new.env(parent = emptyenv())
5+
6+
# The current theme. Defined here only as placeholder, and defined properly
7+
# in file "theme-current.R". This setup avoids circular dependencies among
8+
# the various source files.
9+
ggplot_global$theme_current <- list()
10+
11+
# Element tree for the theme elements. Defined here only as placeholder, and
12+
# defined properly in file "theme-elements.r".
13+
ggplot_global$element_tree <- list()
14+
15+
# List of all aesthetics known to ggplot
16+
ggplot_global$all_aesthetics <- c(
17+
"adj", "alpha", "angle", "bg", "cex", "col", "color",
18+
"colour", "fg", "fill", "group", "hjust", "label", "linetype", "lower",
19+
"lty", "lwd", "max", "middle", "min", "pch", "radius", "sample", "shape",
20+
"size", "srt", "upper", "vjust", "weight", "width", "x", "xend", "xmax",
21+
"xmin", "xintercept", "y", "yend", "ymax", "ymin", "yintercept", "z"
22+
)
23+
24+
# Aesthetic aliases
25+
ggplot_global$base_to_ggplot <- c(
26+
"col" = "colour",
27+
"color" = "colour",
28+
"pch" = "shape",
29+
"cex" = "size",
30+
"lty" = "linetype",
31+
"lwd" = "size",
32+
"srt" = "angle",
33+
"adj" = "hjust",
34+
"bg" = "fill",
35+
"fg" = "colour",
36+
"min" = "ymin",
37+
"max" = "ymax"
38+
)

R/quick-plot.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ qplot <- function(x, y, ..., data, facets = NULL, margins = FALSE,
7575
is_missing <- vapply(exprs, rlang::quo_is_missing, logical(1))
7676
# treat arguments as regular parameters if they are wrapped into I() or
7777
# if they don't have a name that is in the list of all aesthetics
78-
is_constant <- (!names(exprs) %in% .all_aesthetics) |
78+
is_constant <- (!names(exprs) %in% ggplot_global$all_aesthetics) |
7979
vapply(exprs, rlang::quo_is_call, logical(1), name = "I")
8080

8181
mapping <- new_aes(exprs[!is_missing & !is_constant], env = parent.frame())

R/stat-sum.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ StatSum <- ggproto("StatSum", Stat,
4040
compute_panel = function(data, scales) {
4141
if (is.null(data$weight)) data$weight <- 1
4242

43-
group_by <- setdiff(intersect(names(data), .all_aesthetics), "weight")
43+
group_by <- setdiff(intersect(names(data), ggplot_global$all_aesthetics), "weight")
4444

4545
counts <- plyr::count(data, group_by, wt_var = "weight")
4646
counts <- plyr::rename(counts, c(freq = "n"), warn_missing = FALSE)

R/theme-current.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#' @include theme-defaults.r
22
#' @include theme-elements.r
3-
theme_env <- new.env(parent = emptyenv())
4-
theme_env$current <- theme_gray()
3+
ggplot_global$theme_current <- theme_gray()
54

65
#' Get, set, and modify the active theme
76
#'
@@ -66,7 +65,7 @@ theme_env$current <- theme_gray()
6665
#' # theme_update() and theme_replace() are similar except they
6766
#' # apply directly to the current/active theme.
6867
theme_get <- function() {
69-
theme_env$current
68+
ggplot_global$theme_current
7069
}
7170

7271
#' @rdname theme_get
@@ -79,8 +78,8 @@ theme_set <- function(new) {
7978
paste(missing, collapse = ", "), call. = FALSE)
8079
}
8180

82-
old <- theme_env$current
83-
theme_env$current <- new
81+
old <- ggplot_global$theme_current
82+
ggplot_global$theme_current <- new
8483
invisible(old)
8584
}
8685

R/theme-elements.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
257257

258258
# This data structure represents the theme elements and the inheritance
259259
# among them.
260-
.element_tree <- list(
260+
ggplot_global$element_tree <- list(
261261
line = el_def("element_line"),
262262
rect = el_def("element_rect"),
263263
text = el_def("element_text"),
@@ -364,7 +364,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
364364
# @param el an element
365365
# @param elname the name of the element
366366
validate_element <- function(el, elname) {
367-
eldef <- .element_tree[[elname]]
367+
eldef <- ggplot_global$element_tree[[elname]]
368368

369369
if (is.null(eldef)) {
370370
stop('"', elname, '" is not a valid theme element name.')

R/theme.r

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,12 +561,12 @@ calc_element <- function(element, theme, verbose = FALSE) {
561561
# If the element is defined (and not just inherited), check that
562562
# it is of the class specified in .element_tree
563563
if (!is.null(theme[[element]]) &&
564-
!inherits(theme[[element]], .element_tree[[element]]$class)) {
565-
stop(element, " should have class ", .element_tree[[element]]$class)
564+
!inherits(theme[[element]], ggplot_global$element_tree[[element]]$class)) {
565+
stop(element, " should have class ", ggplot_global$element_tree[[element]]$class)
566566
}
567567

568568
# Get the names of parents from the inheritance tree
569-
pnames <- .element_tree[[element]]$inherit
569+
pnames <- ggplot_global$element_tree[[element]]$inherit
570570

571571
# If no parents, this is a "root" node. Just return this element.
572572
if (is.null(pnames)) {

tests/testthat/test-guides.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ test_that("guides title and text are positioned correctly", {
191191
name = "value",
192192
guide = guide_colorbar(
193193
title.theme = element_text(size = 11, angle = 0, hjust = 0.5, vjust = 1),
194-
label.theme = element_text(size = 0.8*11, angle = 270, hjust = 0.5, vjust = 1)
194+
label.theme = element_text(size = 0.8*11, angle = 270, hjust = 0.5, vjust = 1),
195+
order = 2 # set guide order to keep visual test stable
195196
)
196197
) +
197198
scale_fill_continuous(
@@ -204,7 +205,8 @@ test_that("guides title and text are positioned correctly", {
204205
title.position = "top",
205206
label.position = "bottom",
206207
title.theme = element_text(size = 11, angle = 180, hjust = 0, vjust = 1),
207-
label.theme = element_text(size = 0.8*11, angle = 90, hjust = 1, vjust = 0.5)
208+
label.theme = element_text(size = 0.8*11, angle = 90, hjust = 1, vjust = 0.5),
209+
order = 1
208210
)
209211
)
210212

0 commit comments

Comments
 (0)