forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
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
collector4 #2
Open
moodymudskipper
wants to merge
3
commits into
main
Choose a base branch
from
f-collector4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
collector4 #2
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# initiate global tibble to store the results | ||
globals <- new.env() | ||
globals$archive <- tibble( | ||
call = list(), | ||
env = list() | ||
) | ||
|
||
# Add collect_and_rethrow() before the original body | ||
# and set the original function as the "unmodified" attribute | ||
set_collector <- function(fun, force = FALSE) { | ||
new_fun <- fun | ||
body(new_fun) <- call( | ||
"{", | ||
quote(collect_and_rethrow()), | ||
body(fun) | ||
) | ||
# note: trace() calls it "original" so we picked a new name | ||
attr(new_fun, "unmodified") <- fun | ||
new_fun | ||
} | ||
|
||
# * copy caller env and all the chain up to a special env, but every binding is lazy | ||
# * store the call and env in the global tibble | ||
# * on.exit, go through the envs and remove every binding that is still lazy, it's | ||
# not been used, by reference it will update the global tibble too | ||
# * use the original function to eval the call, and do it in the new env, return | ||
# this value from the original function call | ||
collect_and_rethrow <- function() { | ||
new_caller_env <- env_clone_lazy(parent.frame(2)) | ||
call = sys.call(-1) | ||
archive( | ||
call = call, | ||
env = new_caller_env | ||
) | ||
rlang::eval_bare(bquote(on.exit(env_cleanup(.(new_caller_env)))), parent.frame()) | ||
|
||
call[[1]] <- attr(sys.function(-1), "unmodified") | ||
rlang::return_from(parent.frame(), eval(call, new_caller_env)) | ||
} | ||
|
||
|
||
|
||
# note: env_clone doesn't do deep copy, i.e we're not cloning environments that | ||
# are bound in `e`, or nested in lists, or found in attributes, or as function enclosures | ||
env_clone_lazy <- function(env) { | ||
# we stop the recursion when we find a special env, defined by having a name | ||
if (!is.null(attr(env, "name")) || identical(env, emptyenv())) return(env) | ||
parent_clone <- env_clone_lazy(env_parent(env)) | ||
clone <- rlang::env_clone(env, parent = parent_clone) | ||
for (nm in names(clone)) { | ||
#FIXME: assumes env contains no active or lazy bindings, this could be fixed | ||
env_bind_lazy(clone, !!nm := !!env[[nm]]) | ||
} | ||
clone | ||
} | ||
|
||
# drop lazy bindings, since these were not used | ||
env_cleanup <- function(env) { | ||
if (!is.null(attr(env, "name")) || identical(env, emptyenv())) return(env) | ||
env_cleanup(env_parent(env)) | ||
lazy_lgl <- env_binding_are_lazy(env) | ||
rm(list = names(lazy_lgl)[lazy_lgl], envir = env) | ||
} | ||
|
||
archive <- function( | ||
call, | ||
env | ||
) { | ||
globals$archive <- rbind( | ||
globals$archive, | ||
tibble( | ||
call = list(call), | ||
env = list(env) | ||
) | ||
) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,6 +119,7 @@ | |
summarise <- function(.data, ..., .groups = NULL) { | ||
UseMethod("summarise") | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops |
||
#' @rdname summarise | ||
#' @export | ||
summarize <- summarise | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
.onLoad <- function(libname, pkgname) { | ||
mutate <<- set_collector(mutate) | ||
summarise <<- set_collector(summarise) | ||
summarize <<- set_collector(summarize) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. once in a package we can do this in place from outside, and we can easily have a function to reverse the operation, very similar to untrace() |
||
op <- options() | ||
op.dplyr <- list( | ||
dplyr.show_progress = TRUE | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return_from() is overkill, we can have
eval(call, new_caller_env)
and remove the original body in the curried function.