Skip to content
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

Uninformative error for piping into facets #4379

Closed
mitchelloharawild opened this issue Mar 18, 2021 · 6 comments · Fixed by #4385
Closed

Uninformative error for piping into facets #4379

mitchelloharawild opened this issue Mar 18, 2021 · 6 comments · Fixed by #4385

Comments

@mitchelloharawild
Copy link
Contributor

Once of my students mistook %>% for + when adding facets and couldn't determine the problem based on the error message.
It would be nice if the hint for piping layers ("Did you use %>% instead of +?") was also provided if the first argument for facets was a ggplot object.

library(ggplot2)
library(magrittr)
ggplot(diamonds) %>% 
  geom_point(aes(price, carat))
#> Error: `mapping` must be created by `aes()`
#> Did you use %>% instead of +?

ggplot(diamonds) %>% 
  facet_wrap(vars(cut))
#> Warning: Coercing `nrow` to be an integer.
#> Error in sanitise_dim(nrow): 'list' object cannot be coerced to type 'integer'

Created on 2021-03-18 by the reprex package (v1.0.0)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.2 (2020-06-22)
#>  os       Ubuntu 20.04.1 LTS          
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language en_AU:en                    
#>  collate  en_AU.UTF-8                 
#>  ctype    en_AU.UTF-8                 
#>  tz       Australia/Melbourne         
#>  date     2021-03-18                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.2)
#>  cli           2.3.1   2021-02-23 [1] CRAN (R 4.0.2)
#>  colorspace    2.0-0   2020-11-11 [1] CRAN (R 4.0.2)
#>  crayon        1.4.1   2021-02-08 [1] CRAN (R 4.0.2)
#>  DBI           1.1.1   2021-01-15 [1] CRAN (R 4.0.2)
#>  debugme       1.1.0   2017-10-22 [1] CRAN (R 4.0.2)
#>  digest        0.6.27  2020-10-24 [1] CRAN (R 4.0.2)
#>  dplyr         1.0.5   2021-03-05 [1] CRAN (R 4.0.2)
#>  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.2)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.2)
#>  fansi         0.4.2   2021-01-15 [1] CRAN (R 4.0.2)
#>  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.2)
#>  generics      0.1.0   2020-10-31 [1] CRAN (R 4.0.2)
#>  ggplot2     * 3.3.3   2020-12-30 [1] CRAN (R 4.0.2)
#>  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.2)
#>  gtable        0.3.0   2019-03-25 [1] CRAN (R 4.0.2)
#>  highr         0.8     2019-03-20 [1] CRAN (R 4.0.2)
#>  htmltools     0.5.1.1 2021-01-22 [1] CRAN (R 4.0.2)
#>  knitr         1.31    2021-01-27 [1] CRAN (R 4.0.2)
#>  lifecycle     1.0.0   2021-02-15 [1] CRAN (R 4.0.2)
#>  magrittr    * 2.0.1   2020-11-17 [1] CRAN (R 4.0.2)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.0.2)
#>  pillar        1.5.1   2021-03-05 [1] CRAN (R 4.0.2)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.0.2)
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.0.2)
#>  R6            2.5.0   2020-10-28 [1] CRAN (R 4.0.2)
#>  reprex        1.0.0   2021-01-27 [1] CRAN (R 4.0.2)
#>  rlang         0.4.10  2020-12-30 [1] CRAN (R 4.0.2)
#>  rmarkdown     2.7     2021-02-19 [1] CRAN (R 4.0.2)
#>  rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.0.2)
#>  scales        1.1.1   2020-05-11 [1] CRAN (R 4.0.2)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.2)
#>  stringi       1.5.3   2020-09-09 [1] CRAN (R 4.0.2)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.2)
#>  tibble        3.1.0   2021-02-25 [1] CRAN (R 4.0.2)
#>  tidyselect    1.1.0   2020-05-11 [1] CRAN (R 4.0.2)
#>  utf8          1.2.1   2021-03-12 [1] CRAN (R 4.0.2)
#>  vctrs         0.3.6   2020-12-17 [1] CRAN (R 4.0.2)
#>  withr         2.4.1   2021-01-26 [1] CRAN (R 4.0.2)
#>  xfun          0.22    2021-03-11 [1] CRAN (R 4.0.2)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.2)
#> 
#> [1] /home/mitchell/R/x86_64-pc-linux-gnu-library/4.0
#> [2] /opt/R/4.0.0/lib/R/library
@yutannihilation
Copy link
Member

Thanks, I think we can show similar error message on facets too, but,

ggplot(diamonds) %>% 
  facet_wrap(vars(cut))

is this really a common mistake? I think the realistic code would be something like below. But, I think it's only after the user figured out the fact that ggplot2 uses +, not %>%, at least to geom_*. So I'm curious why they still confuses %>% with + here.

p <- ggplot(diamonds) +
  geom_point(aes(price, carat))

# print w/o facetting
p

# print w/ facetting
p %>%
  facet_wrap(vars(cut))

@mitchelloharawild
Copy link
Contributor Author

In this particular case, we were using an autoplot() method. The output from these functions can be further modified and the student tried to add facets. So the code flow was more like:

<tsibble> %>% 
  autoplot() %>% 
  facet_wrap(vars(col))

@yutannihilation
Copy link
Member

Ah, makes sense.

@yutannihilation
Copy link
Member

Would you like to contribute a PR? I guess it's as easy as adding the similar check like this

ggplot2/R/layer.r

Lines 153 to 158 in 199be05

if (inherits(mapping, "ggplot")) {
msg <- paste0(
msg, "\n",
"Did you use %>% instead of +?"
)
}

to here

as_facets_list <- function(x) {

@mitchelloharawild
Copy link
Contributor Author

PR in #4385. It required a bit of re-ordering in some places, so I collected the other check for using aes() into a validate_facets() helper.

@yutannihilation
Copy link
Member

Oh, sorry I didn't notice it needed some more efforts... Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants