Skip to content

Commit

Permalink
Handle deprecated labellers
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Oct 16, 2015
1 parent 854ca37 commit 1fba535
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions R/facet-grid-.r
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ facet_grid <- function(facets, margins = FALSE, scales = "fixed", space = "fixed
stop("Must specify at least one variable to facet by", call. = FALSE)
}

# Check for deprecated labellers
labeller <- check_labeller(labeller)

facet(
rows = rows, cols = cols, margins = margins, shrink = shrink,
free = free, space_free = space_free, labeller = labeller,
Expand Down
18 changes: 18 additions & 0 deletions R/facet-labels.r
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,21 @@ adjust_angle <- function(angle) {
angle + 180
}
}

# Check for old school labeller
check_labeller <- function(labeller) {
labeller <- match.fun(labeller)
is_deprecated <- all(c("variable", "value") %in% names(formals(labeller)))

if (is_deprecated) {
old_labeller <- labeller
labeller <- function(labels) {
Map(old_labeller, names(labels), labels)
}
warning("The labeller API has been updated. Labellers taking `variable`",
"and `value` arguments are now deprecated. See labellers documentation.",
call. = FALSE)
}

labeller
}
3 changes: 3 additions & 0 deletions R/facet-wrap.r
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ facet_wrap <- function(facets, nrow = NULL, ncol = NULL, scales = "fixed",
ncol <- sanitise_dim(ncol)
}

# Check for deprecated labellers
labeller <- check_labeller(labeller)

facet(
facets = as.quoted(facets), free = free, shrink = shrink,
as.table = as.table, switch = switch,
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-facet-labels.r
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,17 @@ test_that("as_labeller() deals with non-labellers", {
p2 <- p + facet_wrap(~am, labeller = labeller(am = function(x) paste0(x, "-foo")))
expect_equal(get_labels_matrix(p2), cbind(c("0-foo", "1-foo")))
})

test_that("old school labellers still work", {
my_labeller <- function(variable, value) {
paste0("var = ", as.character(value))
}

expect_warning(p <-
ggplot(mtcars, aes(disp, drat)) +
geom_point() +
facet_grid(~cyl, labeller = my_labeller))

expected_labels <- cbind(paste("var =", c(4, 6, 8)))
expect_identical(get_labels_matrix(p, "cols"), expected_labels)
})

0 comments on commit 1fba535

Please sign in to comment.