How does `tidyselect::everything()` do to work only when passed as an argument to specific functions

I'm wondering how functions like tidyselect::everything() do to error when executed outside the desired scope/environment. If I understand correctly, everything() will check whether or not data exist in a predefined environment (vars_env here which is defined in the package environment). If the data exist, the function proceeds, otherwise it throws an error. What I don't get though, is when, where and how everything() is evaluated.

Considering the following code:

mutate(a_tibble, across(everything(), as.character))

I'd normally expect everything() to be evalutated first, across() second and mutate() last. Since the data doesn't exist in everything()'s environment, I'd then expect the line above to fail everytime.

I couldn't find in the source code where the data is passed to vars_env. I'd assume it happens within mutate(), which then would mean the way evaluation occurs in the code above is opposite of what I thought.

Any hints to help me understand how all this works?

mutate runs first, across(everything(), as.character) is an input to it it has to understand what to do with, what it will do is make an environment containing stuff like the names in a_tibble that then it can pass on to everything, so that everything can get the names.

1 Like

I actually found were they assign values to the vars_env, I was looking at the wrong place. I manage to replicate most of the behavior of everything(). I now need to figure out how they reset the environment once the function had been executed. It's probably with poke_vars() or scoped_vars().

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.