This returns the name of every second object in the environment
test <- ls()
c(FALSE, TRUE) %>% { test[.] }
But this doesn't (it produces a strange vector of NA
s)
c(FALSE, TRUE) %>% { ls()[.] }
Why doesn't the second method work?
It looks like ls()
can't "figure out" the current environment when it is called in a pipe. When you assign the ls()
outside of a pipe, then call the vector explicitly, there is no searching for the environment. To get around this, just explicitly call the environment in ls()
.
c(FALSE, TRUE) %>% ls(envir = .GlobalEnv)[.]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With