-
Notifications
You must be signed in to change notification settings - Fork 3
Recursively extracting checkers for all arguments #48
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
c3f25e9
c93aa95
710bad8
9715876
210c245
39531f3
f0f3e06
1329a04
ef1b8b4
de163c1
9e7ebea
6820467
7573b31
f4b4bc8
fa6535b
eb6772c
fbf26d5
3ba8b8c
465e287
b2659db
e32bfd6
a142b84
cb65d1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,202 @@ | ||||||||||||||||||||||||||||||||||||
| # Helper function to check if expression contains UseMethod call | ||||||||||||||||||||||||||||||||||||
| contains_use_method <- function(expr) { | ||||||||||||||||||||||||||||||||||||
| if (is.call(expr) && identical(expr[[1]], quote(UseMethod))) { | ||||||||||||||||||||||||||||||||||||
| return(TRUE) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| if (is.call(expr) || is.list(expr)) { | ||||||||||||||||||||||||||||||||||||
| for (i in seq_along(expr)) { | ||||||||||||||||||||||||||||||||||||
| if (contains_use_method(expr[[i]])) { | ||||||||||||||||||||||||||||||||||||
| return(TRUE) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| FALSE | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # Helper function to find UseMethod call in function body and extract generic name | ||||||||||||||||||||||||||||||||||||
| find_use_method <- function(expr) { | ||||||||||||||||||||||||||||||||||||
| if (is.call(expr) && identical(expr[[1]], quote(UseMethod))) { | ||||||||||||||||||||||||||||||||||||
| # UseMethod("name") - extract the name | ||||||||||||||||||||||||||||||||||||
| if (length(expr) >= 2 && is.character(expr[[2]])) { | ||||||||||||||||||||||||||||||||||||
| return(expr[[2]]) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| if (is.call(expr) || is.list(expr)) { | ||||||||||||||||||||||||||||||||||||
| for (i in seq_along(expr)) { | ||||||||||||||||||||||||||||||||||||
| result <- find_use_method(expr[[i]]) | ||||||||||||||||||||||||||||||||||||
| if (!is.null(result)) { | ||||||||||||||||||||||||||||||||||||
| return(result) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| NULL | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # Helper function to check if a function is an S3 generic | ||||||||||||||||||||||||||||||||||||
| is_s3_generic <- function(fun) { | ||||||||||||||||||||||||||||||||||||
| if (!is.null(attr(fun, "generic"))) { | ||||||||||||||||||||||||||||||||||||
| return(TRUE) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| fun_body <- body(fun) | ||||||||||||||||||||||||||||||||||||
| if (is.null(fun_body)) { | ||||||||||||||||||||||||||||||||||||
| return(FALSE) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| contains_use_method(fun_body) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # Helper function to get S3 methods for a generic function | ||||||||||||||||||||||||||||||||||||
| get_s3_methods <- function(fun) { | ||||||||||||||||||||||||||||||||||||
| # Try to get the generic name from the function's attributes first | ||||||||||||||||||||||||||||||||||||
| if (!is.null(attr(fun, "generic"))) { | ||||||||||||||||||||||||||||||||||||
| fun_name <- attr(fun, "generic") | ||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||
| # Extract from UseMethod call in function body | ||||||||||||||||||||||||||||||||||||
| fun_body <- body(fun) | ||||||||||||||||||||||||||||||||||||
| fun_name <- find_use_method(fun_body) | ||||||||||||||||||||||||||||||||||||
| if (is.null(fun_name)) { | ||||||||||||||||||||||||||||||||||||
| stop("Cannot determine generic name from function") | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # Get method names | ||||||||||||||||||||||||||||||||||||
| method_names <- utils::.S3methods(fun_name, envir = environment(fun)) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # Retrieve actual method functions as a named list | ||||||||||||||||||||||||||||||||||||
| method_funs <- list() | ||||||||||||||||||||||||||||||||||||
| for (method in method_names) { | ||||||||||||||||||||||||||||||||||||
| tryCatch( | ||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||
| # Extract class name from method name | ||||||||||||||||||||||||||||||||||||
| class_name <- sub(paste0("^", fun_name, "\\."), "", method) | ||||||||||||||||||||||||||||||||||||
| method_fun <- getS3method( | ||||||||||||||||||||||||||||||||||||
| fun_name, | ||||||||||||||||||||||||||||||||||||
| class_name, | ||||||||||||||||||||||||||||||||||||
| envir = environment(fun) | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| # Use the full method name as the list name | ||||||||||||||||||||||||||||||||||||
| method_funs[[as.character(method)]] <- method_fun | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| error = function(e) { | ||||||||||||||||||||||||||||||||||||
| # Skip methods we can't access | ||||||||||||||||||||||||||||||||||||
| NULL | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| method_funs | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # Helper function to combine and deduplicate results from multiple method analyses | ||||||||||||||||||||||||||||||||||||
| combine_method_results <- function(results_list, fun_args) { | ||||||||||||||||||||||||||||||||||||
| combined <- list() | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| for (arg_name in fun_args) { | ||||||||||||||||||||||||||||||||||||
| # Collect all expressions for this argument across all methods | ||||||||||||||||||||||||||||||||||||
| all_exprs <- list() | ||||||||||||||||||||||||||||||||||||
| for (method_result in results_list) { | ||||||||||||||||||||||||||||||||||||
| if (!is.null(method_result[[arg_name]])) { | ||||||||||||||||||||||||||||||||||||
| all_exprs <- c(all_exprs, method_result[[arg_name]]) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # Remove duplicates by comparing language objects directly | ||||||||||||||||||||||||||||||||||||
| if (length(all_exprs) > 0) { | ||||||||||||||||||||||||||||||||||||
| unique_exprs <- list() | ||||||||||||||||||||||||||||||||||||
| for (expr in all_exprs) { | ||||||||||||||||||||||||||||||||||||
| # Check if this expression is already in unique_exprs | ||||||||||||||||||||||||||||||||||||
| is_duplicate <- FALSE | ||||||||||||||||||||||||||||||||||||
| for (unique_expr in unique_exprs) { | ||||||||||||||||||||||||||||||||||||
| if (identical(expr, unique_expr)) { | ||||||||||||||||||||||||||||||||||||
| is_duplicate <- TRUE | ||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| if (!is_duplicate) { | ||||||||||||||||||||||||||||||||||||
| unique_exprs[[length(unique_exprs) + 1]] <- expr | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+107
to
+117
|
||||||||||||||||||||||||||||||||||||
| for (expr in all_exprs) { | |
| # Check if this expression is already in unique_exprs | |
| is_duplicate <- FALSE | |
| for (unique_expr in unique_exprs) { | |
| if (identical(expr, unique_expr)) { | |
| is_duplicate <- TRUE | |
| break | |
| } | |
| } | |
| if (!is_duplicate) { | |
| unique_exprs[[length(unique_exprs) + 1]] <- expr | |
| seen_exprs <- character() | |
| for (expr in all_exprs) { | |
| expr_str <- if (exists("deparse1", mode = "function")) deparse1(expr) else paste(deparse(expr), collapse = "") | |
| if (!(expr_str %in% seen_exprs)) { | |
| unique_exprs[[length(unique_exprs) + 1]] <- expr | |
| seen_exprs <- c(seen_exprs, expr_str) |
Uh oh!
There was an error while loading. Please reload this page.