Skip to content

Commit

Permalink
Merge pull request rstudio#74 from rstudio-education/compare_default_…
Browse files Browse the repository at this point in the history
…formals

Add function formals when comparing and standarising code for comparison
  • Loading branch information
garrettgman authored Dec 16, 2019
2 parents 94ad3fe + 12355c2 commit 6a6b21c
Show file tree
Hide file tree
Showing 12 changed files with 786 additions and 492 deletions.
33 changes: 33 additions & 0 deletions R/call_standarise_formals.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
call_standardise_formals <- function(code, env = rlang::current_env()) {
# browser()

# try to catch invalid function, i.e., corrupt language object
tryCatch({
fxn <- rlang::call_fn(code, env = env)
}, error = function(e) {
return(code)
})
if (!exists("fxn")) {return(code)} ## some reason the above tryCatch doesn't go to the error part
if(class(fxn) != "function") {return(code)}

# standarise, but dont bother trying to fill out default formals
# for primitives like mean, unable to distinguish between mean and mean.default
if (is_infix(code) || is.primitive(fxn)) {
return(rlang::call_standardise(code))
}

forms <- rlang::fn_fmls(fxn)
default_params <- forms[!vapply(forms, is.symbol, logical(1), USE.NAMES = FALSE)]

code_std <- rlang::call_standardise(code, env = env) # order and label existing params

code_params <- rlang::call_args(code_std) # get arguments passed from user
code_missing_default_args <- default_params[!names(default_params) %in% names(code_params)]
if (length(code_missing_default_args) == 0) {
return(code_std)
}
return(
rlang::call_standardise(rlang::call_modify(code_std, !!!code_missing_default_args),
env = env)
)
}
Loading

0 comments on commit 6a6b21c

Please sign in to comment.