Created
August 5, 2020 20:40
-
-
Save moodymudskipper/7df1516e530de0f26c5fb56cac294764 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| create_lazy_bindings <- function(x, ...){ | |
| environment() | |
| } | |
| # object df is in the environment but has not been evaluated | |
| e <- create_lazy_bindings(y) | |
| ls(e) | |
| #> [1] "x" | |
| # thus this won't work | |
| as.list(e) | |
| #> Error in as.list.environment(e): objet 'y' introuvable | |
| # but this does! | |
| evalq(substitute(x), envir = e) | |
| #> y | |
| # it also works with the dots! | |
| e <- create_lazy_bindings(cars, speed < 5) | |
| evalq(subset(cars, ...), envir = e) | |
| #> speed dist | |
| #> 1 4 2 | |
| #> 2 4 10 | |
| # and you can even use lazy binding in the global environment! | |
| ... <- e$... | |
| subset(cars, ...) | |
| #> speed dist | |
| #> 1 4 2 | |
| #> 2 4 10 | |
| a <- 1 | |
| b <- 2 | |
| e <- create_lazy_bindings(a + b) | |
| a <- 100 | |
| z <- e$x | |
| a <- 1000 | |
| z | |
| #> [1] 102 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment