Skip to content

Capture error with hint when piping plots into facets #4385

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 1 commit into from
Mar 22, 2021
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
installed, which will offer to install the package before continuing (#4375,
@malcolmbarrett)

* Improved error with hint when piping a `ggplot` object into a facet function
(#4379, @mitchelloharawild).

# ggplot2 3.3.3
This is a small patch release mainly intended to address changes in R and CRAN.
It further changes the licensing model of ggplot2 to an MIT license.
Expand Down
17 changes: 14 additions & 3 deletions R/facet-.r
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,7 @@ df.grid <- function(a, b) {
# facetting variables.

as_facets_list <- function(x) {
if (inherits(x, "uneval")) {
abort("Please use `vars()` to supply facet variables")
}
x <- validate_facets(x)
if (is_quosures(x)) {
x <- quos_auto_name(x)
return(list(x))
Expand Down Expand Up @@ -315,6 +313,19 @@ as_facets_list <- function(x) {
x
}

validate_facets <- function(x) {
if (inherits(x, "uneval")) {
abort("Please use `vars()` to supply facet variables")
}
if (inherits(x, "ggplot")) {
abort(
"Please use `vars()` to supply facet variables\nDid you use %>% instead of +?"
)
}
x
}


# Flatten a list of quosures objects to a quosures object, and compact it
compact_facets <- function(x) {
x <- flatten_if(x, is_list)
Expand Down
9 changes: 8 additions & 1 deletion R/facet-grid-.r
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,14 @@ grid_as_facets_list <- function(rows, cols) {
is_rows_vars <- is.null(rows) || is_quosures(rows)
if (!is_rows_vars) {
if (!is.null(cols)) {
abort("`rows` must be `NULL` or a `vars()` list if `cols` is a `vars()` list")
msg <- "`rows` must be `NULL` or a `vars()` list if `cols` is a `vars()` list"
if(inherits(rows, "ggplot")) {
msg <- paste0(
msg, "\n",
"Did you use %>% instead of +?"
)
}
abort(msg)
}
# For backward-compatibility
facets_list <- as_facets_list(rows)
Expand Down
13 changes: 7 additions & 6 deletions R/facet-wrap.r
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ facet_wrap <- function(facets, nrow = NULL, ncol = NULL, scales = "fixed",
x = any(scales %in% c("free_x", "free")),
y = any(scales %in% c("free_y", "free"))
)

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

# Flatten all facets dimensions into a single one
facets <- wrap_as_facets_list(facets)

if (!is.null(switch)) {
.Deprecated("strip.position", old = "switch")
strip.position <- if (switch == "x") "bottom" else "left"
Expand All @@ -102,12 +109,6 @@ facet_wrap <- function(facets, nrow = NULL, ncol = NULL, scales = "fixed",
ncol <- sanitise_dim(ncol)
}

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

# Flatten all facets dimensions into a single one
facets <- wrap_as_facets_list(facets)

ggproto(NULL, FacetWrap,
shrink = shrink,
params = list(
Expand Down