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

Support wrapping englue() #1566

Merged
merged 3 commits into from
Feb 17, 2023
Merged

Support wrapping englue() #1566

merged 3 commits into from
Feb 17, 2023

Conversation

lionel-
Copy link
Member

@lionel- lionel- commented Feb 16, 2023

Closes #1565
cc @hadley

@lionel- lionel- requested a review from DavisVaughan February 16, 2023 11:35
@hadley
Copy link
Member

hadley commented Feb 16, 2023

Nice!

R/nse-inject.R Show resolved Hide resolved
R/nse-inject.R Outdated Show resolved Hide resolved
#' context by default).
#'
#' ```{r}
#' my_englue <- function(text) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my_englue() should also expose env.

I think this is how we will end up documenting .envir in glue, like tidyverse/glue#281 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the main point is ensuring that caller_env() gets used and passed through somehow, so it isn't that important... but it still might be "best practice"?

Or maybe you can say:

If you are creating a low level package on top of englue(), you should also consider exposing env, error_arg and error_call in your englue() wrapper so users can wrap your wrapper.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your note about exposing these arguments to allow wrappers.

There are cases where this would be unwieldy though, e.g. in the tidyr case.

By the way, maybe we should consider an enquo()-based interface for englue? We'd require literal strings as inputs and to wrap englue you'd use {{.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would have the advantage of making it easy to wrap names_glue if the tidyr function adds englue support, without having to expose env.

Doesn't solve the error-call issue though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea being that the quosure created through the internal enquo()-like call would capture and pass on the env for you, right? Seems interesting.

I guess that quosure env becomes the parent env if you want to insert a .qux pronoun or something

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup we'd have to take data instead of env in this case. And if multiple layer masks are ever needed, we could extend to accept rlang data masks which make the layer structure explicit so we'd have all the information needed (even though we wouldn't use eval_tidy() but glue() in the end, I think that should still work).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the compiler would get us in trouble though, since it unwraps strings from promises and then we lose the environment. We can write this idea off.

check_string(x)
englue <- function(x,
env = caller_env(),
error_call = current_env(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have strong opinions, but do you have a reason for using current_env() over caller_env()?

englue() will pretty much always be wrapped inside another function right? Should it default to reporting that function as the location of any issues?

Copy link
Member Author

@lionel- lionel- Feb 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the time englue() will be used as part of tidy eval to program against dplyr or tidyr. In these cases, I think it makes sense to match input errors to the function that the user used, which is englue().

englue <- function(x,
env = caller_env(),
error_call = current_env(),
error_arg = "x") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also mildly surprised this isn't error_arg(x)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most cases (tidyeval prog), caller_arg(x) will be a string instead of a symbol.

error_arg <- as_string(text_expr)
error_call <- NULL

# Find the relevant error frame. There are edge cases where this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only part im a little iffy on but it seems reasonable and the downsides seem slim

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not great but it becomes too complex to document and use if we add a separate error_call for the user frame :-(

lionel- and others added 3 commits February 17, 2023 09:13
Infer the user error frame from `env` instead of passing it around
Co-authored-by: Davis Vaughan <davis@rstudio.com>
@lionel- lionel- merged commit 2772342 into main Feb 17, 2023
@lionel- lionel- deleted the englue-wrapping branch February 17, 2023 08:30
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 this pull request may close these issues.

Support wrapping englue()
3 participants