Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

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

* Fix bug in `annotate_logticks()` that would cause an error when used together
with `coord_flip()` (@thomasp85, #3954)
Expand Down
4 changes: 3 additions & 1 deletion R/labeller.r
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ label_bquote <- function(rows = NULL, cols = NULL,
rows_quoted <- substitute(rows)
has_warned <- FALSE

call_env <- env_parent()

fun <- function(labels) {
quoted <- resolve_labeller(rows_quoted, cols_quoted, labels)
if (is.null(quoted)) {
Expand All @@ -225,7 +227,7 @@ label_bquote <- function(rows = NULL, cols = NULL,
}
params$x <- params[[1]]
}

params <- as_environment(params, call_env)
eval(substitute(bquote(expr, params), list(expr = quoted)))
}
list(do.call("Map", c(list(f = evaluate), labels)))
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-labellers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
context("Labellers")

test_that("label_bquote has access to functions in the calling environment", {
labels <- data.frame(lab = letters[1:2])
attr(labels, "facet") <- "wrap"
labeller <- label_bquote(rows = .(paste0(lab, ":")))
labels_calc <- labeller(labels)
expect_equal(labels_calc[[1]][[1]], "a:")
})