Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Vignette: discuss optional string input#6215

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
teunbrand wants to merge3 commits intotidyverse:main
base:main
Choose a base branch
Loading
fromteunbrand:optional_string_input

Conversation

@teunbrand
Copy link
Collaborator

This PR aims tofix#6208.

If somebody has better advice for dealing with optional character-input, I'd be glad to adjust.

yjunechoe reacted with eyes emoji
@yjunechoe
Copy link
Contributor

Tangentially related, but I wonder whetherrlang::data_sym() passingNULL through could resolve some of this headache:

rlang::data_sym(NULL)#> NULL

Instead 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 (withall_of()/any_of()), so it'd be nice ifdata_sym()/data_syms() could let us get something close to tidyselect semantics within tidyeval contexts (likeaes()) by lettingNULL pass through to mean an empty (NULL) selection. Ex:

df<-data.frame(x=1)# Tidyselect behaviortidyselect_get<-function(df,col) {tidyselect::eval_select(tidyselect::all_of(col),df)}tidyselect_get(df,"x")#> x#> 1tidyselect_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] 1tidyeval_get(df,NULL)#> Error in `sym()`:#> ! Can't convert `NULL` to a symbol.# Tidyeval with modified `data_sym2()` that passes NULL throughdata_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] 1tidyeval_get2(df,NULL)#> NULL

@teunbrand
Copy link
CollaboratorAuthor

Thanks June, your comments make a lot of sense to me. I'd agree that this havingdata_sym() acceptNULL would be the most elegant. I don't think ggplot2 should provide a user-exposed wrapper for this, as we'd have to maintain this into perpetuity. Rather, we could petition {rlang} if they'd be interested in this change. That allows us to include this PR as current advice and update the advice if and once we can usedata_sym(NULL).

yjunechoe reacted with thumbs up emoji

@yjunechoe
Copy link
Contributor

Rather, we could petition {rlang} if they'd be interested in this change.

Yeah, that's the intended build-up of my comment! Would be nice for the FR to be backed by a need in ggplot 😄

@thomasp85
Copy link
Member

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))

@teunbrand
Copy link
CollaboratorAuthor

I'm happy to recommend the!!sym() trick, but it'd be a different approach than this PR, so I'll create a new one.

@teunbrandteunbrand marked this pull request as draftDecember 4, 2025 10:07
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Vignette ggplot2-in-packages does not cover programmatically passing NULL to aes()

3 participants

@teunbrand@yjunechoe@thomasp85

[8]ページ先頭

©2009-2025 Movatter.jp