Skip to content

Commit 53c1440

Browse files
authored
Give label_bquote() access to calling environment (#4453)
1 parent cc3951c commit 53c1440

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

NEWS.md

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

3+
* Make sure `label_bquote()` has access to the calling environment when
4+
evaluating the labels (@thomasp85, #4141)
35

46
* Fix bug in `annotate_logticks()` that would cause an error when used together
57
with `coord_flip()` (@thomasp85, #3954)

R/labeller.r

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ label_bquote <- function(rows = NULL, cols = NULL,
205205
rows_quoted <- substitute(rows)
206206
has_warned <- FALSE
207207

208+
call_env <- env_parent()
209+
208210
fun <- function(labels) {
209211
quoted <- resolve_labeller(rows_quoted, cols_quoted, labels)
210212
if (is.null(quoted)) {
@@ -225,7 +227,7 @@ label_bquote <- function(rows = NULL, cols = NULL,
225227
}
226228
params$x <- params[[1]]
227229
}
228-
230+
params <- as_environment(params, call_env)
229231
eval(substitute(bquote(expr, params), list(expr = quoted)))
230232
}
231233
list(do.call("Map", c(list(f = evaluate), labels)))

tests/testthat/test-labellers.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
context("Labellers")
2+
3+
test_that("label_bquote has access to functions in the calling environment", {
4+
labels <- data.frame(lab = letters[1:2])
5+
attr(labels, "facet") <- "wrap"
6+
labeller <- label_bquote(rows = .(paste0(lab, ":")))
7+
labels_calc <- labeller(labels)
8+
expect_equal(labels_calc[[1]][[1]], "a:")
9+
})

0 commit comments

Comments
 (0)