-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfunction_args.R
More file actions
59 lines (50 loc) · 2 KB
/
function_args.R
File metadata and controls
59 lines (50 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#' get matched argument list for a given function
#'
#' extracts and names user-specified arguments according to function defaults
#'
#' @param package the package to use to get the function from
#' @param name the function name
#' @param object the first argument, which may have a class to match functions to
#' @param \dots user arguments to be used for the list
#' @param use.default use different function name other than <function>.default (optional)
#' @param drop boolean for dropping all non-formal args passed in with \dots
#'
#' @keywords internal
function_args <- function(package, name, object, ..., use.default=paste0(name,'.default'), drop=FALSE){
params <- list(...)
if (!missing(object)) {
if (!is.null(names(object)))
params <- append(object, params)
else {
params <- append(list(object), params)
}
} else {
object = c() # replace w/ empty
}
if (length(params) == 0)
return(list())
# // is there a method for this class?
defFun <- getS3method(name,class(object),optional=TRUE) # will be NULL when object is missing
if (is.null(defFun)){
defFun <- getFromNamespace(ifelse(existsFunction(use.default), use.default, name), package)
}
arg.names = names(formals(defFun))[which(!names(formals(defFun)) %in% names(params))]
if (is.null(names(params))){
# // all are unnamed
if (arg.names[seq_len(length(params))][1] == "..."){
# // special case where unnamed args go to ..., and should remain as characters (such as par("usr"))
return(params)
}
names(params) <- arg.names[seq_len(length(params))]
} else {
names(params)[which(names(params) == "")] <- arg.names[seq_len(sum(names(params) == ""))]
}
# // re-order
sort.i <- seq_len(length(params))
match.i <- match(names(params), names(formals(defFun)))
sort.i[!is.na(match.i)] <- match.i[!is.na(match.i)]
params <- params[sort(sort.i, index.return = TRUE)$ix]
if (drop)
params = params[names(params) %in% names(formals(defFun))]
return(params)
}