Skip to content

Commit

Permalink
improved do_call
Browse files Browse the repository at this point in the history
  • Loading branch information
qddyy committed Nov 14, 2024
1 parent 20c4fb7 commit 56ec984
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion R/MultipleComparison.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ MultipleComparison <- R6Class(
fixed = list(
plot = TRUE,
x = statistic_permu[k, ],
xlim = bquote(range(breaks, .(statistic[k]))),
xlim = quote(range(breaks, statistic[k])),
xlab = "Statistic",
main = paste(data_names[i], "~", data_names[j])
), ...
Expand Down
2 changes: 1 addition & 1 deletion R/PermuTest.R
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ PermuTest <- R6Class(
fixed = list(
plot = TRUE,
x = attr(private$.statistic, "permu"),
xlim = bquote(range(breaks, .(private$.statistic))),
xlim = quote(range(breaks, private$.statistic)),
xlab = "Statistic",
main = "Permutation Distribution"
), ...
Expand Down
32 changes: 19 additions & 13 deletions R/do_call.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
# Updates the formals of a function with specified arguments before calling it.
# Enables non-standard evaluation by allowing expressions in certain arguments.
# Example: `do_call(func, list(args = bquote(.(constant_here) + symbol_here)))`
do_call <- function(func, default = list(), fixed = list(), ...) {
# use `as.list()` because `formals()` returns a pairlist
formals <- as.environment(as.list(formals(func)))
# Use `as.list()` because `formals()` returns a pairlist.
args <- as.environment(as.list(formals(func)))

formal_names <- names(formals)
# Use `base::list2env()` over `utils::modifyList()` for minimal dependency.
args <- list2env(envir = args, default)
args <- list2env(envir = args, list(...))
args <- list2env(envir = args, fixed)

# use `base::list2env()` over `utils::modifyList()` for minimal dependency
formals <- list2env(envir = formals, default)
formals <- list2env(envir = formals, list(...))
formals <- list2env(envir = formals, fixed)

formals <- as.list.environment(formals, all.names = TRUE)[formal_names]

# `func` should be non-primitive
`formals<-`(func, value = formals)()
# `func` should be non-primitive.
`formals<-`(
func, value = lapply(
`names<-`(names(args), names(args)),
function(name) {
# `args[[name]]` might be a "missing symbol object". Always use the full form here.
# https://stackoverflow.com/questions/3892580
if (!is.language(args[[name]])) {
bquote(parent.frame()$args[[.(name)]])
} else args[[name]]
}
)
)()
}

0 comments on commit 56ec984

Please sign in to comment.