Skip to content

Commit 3716853

Browse files
committed
Better strategy for finding geoms/stats by name
1 parent cdf36c2 commit 3716853

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

R/geom-defaults.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#' @rdname update_defaults
1414
update_geom_defaults <- function(geom, new) {
1515
if (is.character(geom)) {
16-
g <- find_subclass("Geom", geom)
16+
g <- find_subclass("Geom", geom, parent.frame())
1717
} else if (inherits(geom, "Geom")) {
1818
g <- geom
1919
} else {
@@ -29,7 +29,7 @@ update_geom_defaults <- function(geom, new) {
2929
#' @export
3030
update_stat_defaults <- function(stat, new) {
3131
if (is.character(stat)) {
32-
g <- find_subclass("Stat", stat)
32+
g <- find_subclass("Stat", stat, parent.frame())
3333
} else if (inherits(stat, "Stat")) {
3434
g <- stat
3535
} else {

R/layer.r

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ layer <- function(geom = NULL, stat = NULL,
8787
}
8888

8989
if (is.character(geom))
90-
geom <- find_subclass("Geom", geom)
90+
geom <- find_subclass("Geom", geom, parent.frame())
9191
if (is.character(stat))
92-
stat <- find_subclass("Stat", stat)
92+
stat <- find_subclass("Stat", stat, parent.frame())
9393
if (is.character(position))
94-
position <- find_subclass("Position", position)
94+
position <- find_subclass("Position", position, parent.frame())
9595

9696
# Special case for na.rm parameter needed by all layers
9797
if (is.null(params$na.rm)) {
@@ -316,14 +316,13 @@ Layer <- ggproto("Layer", NULL,
316316
is.layer <- function(x) inherits(x, "Layer")
317317

318318

319-
find_subclass <- function(super, class) {
319+
find_subclass <- function(super, class, env) {
320320
name <- paste0(super, camelize(class, first = TRUE))
321-
if (!exists(name)) {
322-
stop("No ", tolower(super), " called ", name, ".", call. = FALSE)
323-
}
321+
obj <- find_global(name, env = env)
324322

325-
obj <- get(name)
326-
if (!inherits(obj, super)) {
323+
if (is.null(name)) {
324+
stop("No ", tolower(super), " called ", name, ".", call. = FALSE)
325+
} else if (!inherits(obj, super)) {
327326
stop("Found object is not a ", tolower(super), ".", call. = FALSE)
328327
}
329328

R/utilities-help.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
rd_aesthetics <- function(type, name) {
33
obj <- switch(type,
4-
geom = find_subclass("Geom", name),
5-
stat = find_subclass("Stat", name)
4+
geom = find_subclass("Geom", name, globalenv()),
5+
stat = find_subclass("Stat", name, globalenv())
66
)
77
aes <- rd_aesthetics_item(obj)
88

0 commit comments

Comments
 (0)