Vignette: discuss optional string input#6215
Vignette: discuss optional string input#6215teunbrand wants to merge 3 commits intotidyverse:mainfrom
Conversation
|
Tangentially related, but I wonder whether rlang::data_sym(NULL)
#> NULLInstead of its current behavior: rlang::data_sym(NULL)
#> Error in `sym()`:
#> ! Can't convert `NULL` to a symbol.Especially because I feel like this "optional character-input" problem has already been solved in tidyselect (with df <- data.frame(x = 1)
# Tidyselect behavior
tidyselect_get <- function(df, col) {
tidyselect::eval_select(tidyselect::all_of(col), df)
}
tidyselect_get(df, "x")
#> x
#> 1
tidyselect_get(df, NULL)
#> named integer(0)
# Tidyeval with `data_sym()`
tidyeval_get <- function(df, col) {
rlang::eval_tidy(rlang::data_sym(col), df)
}
tidyeval_get(df, "x")
#> [1] 1
tidyeval_get(df, NULL)
#> Error in `sym()`:
#> ! Can't convert `NULL` to a symbol.
# Tidyeval with modified `data_sym2()` that passes NULL through
data_sym2 <- \(x) if (!is.null(x)) rlang::data_sym(x)
tidyeval_get2 <- function(df, col) {
rlang::eval_tidy(data_sym2(col), df)
}
tidyeval_get2(df, "x")
#> [1] 1
tidyeval_get2(df, NULL)
#> NULL |
|
Thanks June, your comments make a lot of sense to me. I'd agree that this having |
Yeah, that's the intended build-up of my comment! Would be nice for the FR to be backed by a need in ggplot 😄 |
|
I'm personally partial to advocating embrace for people wanting to build ggplot2 wrapper functions. The issue noted in the original issue (name of column coming as a string) is easily solved: plot_embrace <- function(dat,xvar,yvar,colorvar=NULL,shapevar=NULL) {
myplot <- ggplot(dat,aes(x={{ xvar }},y={{ yvar }},color={{ colorvar }},shape={{ shapevar }})) +
geom_point()
return(myplot)
}
whichvar <- sample(c('sex','employment'),1)
plot_embrace(plotdat,weight,height,colorvar=age,shapevar=!!sym(whichvar)) |
|
I'm happy to recommend the |
This PR aims to fix #6208.
If somebody has better advice for dealing with optional character-input, I'd be glad to adjust.