Skip to content

check the input faceting names #4922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 1, 2022
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
11 changes: 11 additions & 0 deletions R/facet-.r
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,17 @@ check_layout <- function(x) {
cli::cli_abort("Facet layout has a bad format. It must contain columns {.col PANEL}, {.col SCALE_X}, and {.col SCALE_Y}")
}

check_facet_vars <- function(..., name) {
vars_names <- c(...)
reserved_names <- c("PANEL", "ROW", "COL", "SCALE_X", "SCALE_Y")
problems <- intersect(vars_names, reserved_names)
if (length(problems) != 0) {
cli::cli_abort(c(
"{.val {problems}} {?is/are} not {?an/} allowed name{?/s} for faceting variables",
"i" = "Change the name of your data columns to not be {.or {.str {reserved_names}}}"
), call = call2(name))
}
}

#' Get the maximal width/length of a list of grobs
#'
Expand Down
6 changes: 4 additions & 2 deletions R/facet-grid-.r
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,18 @@ grid_as_facets_list <- function(rows, cols) {
FacetGrid <- ggproto("FacetGrid", Facet,
shrink = TRUE,

compute_layout = function(data, params) {
compute_layout = function(self, data, params) {
rows <- params$rows
cols <- params$cols

check_facet_vars(names(rows), names(cols), name = snake_class(self))

dups <- intersect(names(rows), names(cols))
if (length(dups) > 0) {
cli::cli_abort(c(
"Faceting variables can only appear in {.arg rows} or {.arg cols}, not both.\n",
"i" = "Duplicated variables: {.val {dups}}"
))
), call = call2(snake_class(self)))
}

base_rows <- combine_vars(data, params$plot_env, rows, drop = params$drop)
Expand Down
2 changes: 2 additions & 0 deletions R/facet-wrap.r
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ FacetWrap <- ggproto("FacetWrap", Facet,
return(layout_null())
}

check_facet_vars(names(vars), name = snake_class(self))

base <- combine_vars(data, params$plot_env, vars, drop = params$drop)

id <- id(base, drop = TRUE)
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/_snaps/facet-layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,18 @@

Free scales cannot be mixed with a fixed aspect ratio

# facet_wrap and facet_grid throws errors when using reserved words

"ROW" is not an allowed name for faceting variables
i Change the name of your data columns to not be "PANEL", "ROW", "COL", "SCALE_X", or "SCALE_Y"

---

"ROW" and "PANEL" are not allowed names for faceting variables
i Change the name of your data columns to not be "PANEL", "ROW", "COL", "SCALE_X", or "SCALE_Y"

---

"ROW" is not an allowed name for faceting variables
i Change the name of your data columns to not be "PANEL", "ROW", "COL", "SCALE_X", or "SCALE_Y"

12 changes: 12 additions & 0 deletions tests/testthat/test-facet-layout.r
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,15 @@ test_that("facet_grid throws errors at bad layout specs", {
theme(aspect.ratio = 1)
expect_snapshot_error(ggplotGrob(p))
})

test_that("facet_wrap and facet_grid throws errors when using reserved words", {
mtcars2 <- mtcars
mtcars2$PANEL <- mtcars2$cyl
mtcars2$ROW <- mtcars2$gear

p <- ggplot(mtcars2) +
geom_point(aes(mpg, disp))
expect_snapshot_error(ggplotGrob(p + facet_grid(ROW ~ gear)))
expect_snapshot_error(ggplotGrob(p + facet_grid(ROW ~ PANEL)))
expect_snapshot_error(ggplotGrob(p + facet_wrap(~ROW)))
})