-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy paths4.R
53 lines (45 loc) · 1.22 KB
/
s4.R
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
# http://stackoverflow.com/a/39880324/946850
s4_methods <- function(env, pkg_fun = NULL) {
generics <- methods::getGenerics(env)
if (is.null(pkg_fun)) {
ok <- TRUE
} else {
ok <- pkg_fun(generics@package)
}
res <- Map(
generics@.Data[ok], generics@package[ok],
USE.NAMES = TRUE,
f = function(name, package) {
what <- methods::methodsPackageMetaName("T", paste(name, package, sep = ":"))
table <- get(what, envir = env)
mget(ls(table, all.names = TRUE), envir = table)
}
)
unlist(res, recursive = FALSE)
}
s4_real_argument_names <- function(s4_method) {
expect_s4_class(s4_method, "function")
expect_s4_class(s4_method, "MethodDefinition")
unwrapped <- s4_unwrap(s4_method)
names(formals(unwrapped))
}
s4_unwrap <- function(s4_method) {
# Only unwrap if body is of the following form:
# {
# .local <- function(x, y, z, ...) {
# ...
# }
# ...
# }
method_body <- body(s4_method)
if (inherits(method_body, "{")) {
local_def <- method_body[[2]]
if (inherits(local_def, "<-") && local_def[[2]] == quote(.local)) {
local_fun <- local_def[[3]]
if (inherits(local_fun, "function")) {
return(local_fun)
}
}
}
s4_method
}