Skip to content
Open
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
16 changes: 15 additions & 1 deletion R/facet-layout.r
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,22 @@ layout_base <- function(data, vars = NULL, drop = TRUE) {

# Form the base data frame which contains all combinations of facetting
# variables that appear in the data
has_all <- unlist(plyr::llply(values, length)) == length(vars)
has_all <- unlist(lapply(values, length)) == length(vars)
if (!any(has_all)) {
all_data_vars <- unique(unlist(lapply(data, names)))
missing_vars <- setdiff(names(vars)[names(vars) != "."], all_data_vars)
if (length(missing_vars) > 0) {
stop(sprintf(
"Facet variable%s not found in data: %s\nAvailable columns: %s\nUse string notation like facet_wrap(\"var\") instead of formula notation facet_wrap(. ~ var)",
if (length(missing_vars) > 1) "s" else "",
paste(missing_vars, collapse = ", "),
paste(all_data_vars, collapse = ", ")
))
}
# Reached when all facet variables exist somewhere in the data, but no
# single layer has all of them together. For example, facet_grid(X ~ Y)
# where layer 1 only has column X and layer 2 only has column Y — neither
# layer alone satisfies all facetting variables, so has_all is FALSE for every layer.
stop("At least one layer must contain all variables used for facetting")
}

Expand Down
2 changes: 1 addition & 1 deletion R/z_animintHelpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -1097,4 +1097,4 @@ selectSSandCS <- function(aesthetics_list){
}
## TODO: how to handle showSelected$ignored in prev animint code??
return(aes.list)
}
}
34 changes: 34 additions & 0 deletions tests/testthat/test-renderer-facet-error-messages.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
acontext("Facet error messages")
test_that("facet_wrap formula with missing variable gives clear error", {
viz <- list(
scatter = ggplot() +
facet_wrap(. ~ NonExistentColumn) +
geom_point(aes(Sepal.Length, Petal.Length), data = iris)
)
expect_error(
animint2dir(viz, out.dir = tempfile(), open.browser = FALSE),
"Facet variable not found in data: NonExistentColumn\nAvailable columns: Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species\nUse string notation like facet_wrap(\"var\") instead of formula notation facet_wrap(. ~ var)",
fixed = TRUE
)
})
test_that("facet_grid formula with missing variable gives clear error", {
viz <- list(
scatter = ggplot() +
facet_grid(. ~ MissingVar) +
geom_point(aes(Sepal.Length, Petal.Length), data = iris)
)
expect_error(
animint2dir(viz, out.dir = tempfile(), open.browser = FALSE),
"Facet variable not found in data: MissingVar\nAvailable columns: Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species\nUse string notation like facet_wrap(\"var\") instead of formula notation facet_wrap(. ~ var)",
fixed = TRUE
)
})
test_that("facet_wrap string notation works", {
viz <- list(
scatter = ggplot() +
facet_wrap("Species") +
geom_point(aes(Sepal.Length, Petal.Length), data = iris)
)
info <- animint2dir(viz, out.dir = tempfile(), open.browser = FALSE)
expect_true(file.exists(file.path(info$out.dir, "index.html")))
})
Loading