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

englue-ing NULL yields uninformative error #1492

Closed
hadley opened this issue Sep 26, 2022 · 4 comments
Closed

englue-ing NULL yields uninformative error #1492

hadley opened this issue Sep 26, 2022 · 4 comments

Comments

@hadley
Copy link
Member

hadley commented Sep 26, 2022

library(tidyverse)

histogram <- function(df, var, binwidth = NULL) {
  label <- rlang::englue("A histogram of {{var}} with binwidth {binwidth}")
  
  df |> 
    ggplot(aes({{ var }})) + 
    geom_histogram(binwidth = binwidth) + 
    labs(title = label)
}

diamonds |> histogram(carat)
#> Error in `rlang::list2()`:
#> ! The LHS of `:=` must be a string or a symbol

#> Backtrace:
#>     ▆
#>  1. ├─global histogram(diamonds, carat)
#>  2. │ ├─rlang::englue("A histogram of {{var}} with binwidth {binwidth}")
#>  3. │ │ └─rlang::inject(rlang::list2(!!expr), caller_env())
#>  4. │ └─rlang::list2(...)
#>  5. └─rlang::abort(message = message)

Created on 2022-09-26 with reprex v2.0.2

@lionel-
Copy link
Member

lionel- commented Oct 20, 2022

This is tricky because this behaviour is from recycling rules of glue():

unclass(glue::glue("foo {NULL}"))
#> character(0)

We could detect a change in size and throw an error about sizes of interpolated elements causing recycling.

@hadley
Copy link
Member Author

hadley commented Oct 20, 2022

Oh yeah: tidyverse/glue#246

@lionel-
Copy link
Member

lionel- commented Feb 16, 2023

@hadley With the recent refactoring of englue() the error is gone:

fn <- function(x) {
  englue("{{ x }} {NULL}")
}

fn(foo)
#> character(0)

But I'm thinking we should enforce a size 1 return from englue()?

@hadley
Copy link
Member Author

hadley commented Feb 16, 2023

Yeah, I think that's a good idea.

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

No branches or pull requests

2 participants