-
Notifications
You must be signed in to change notification settings - Fork 11
/
methods.R
83 lines (68 loc) · 2.13 KB
/
methods.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#' List methods for a S3 or S4 generic (or class)
#'
#' Returns information about all methods belong to a generic or a class.
#' In S3 and S4, methods belong to a generic, but it is often useful to see what
#' generics have been provided methods for a given class. These are
#' wrappers around [utils::methods()], which returns a lot of useful information
#' in an attribute.
#'
#' @param x Name of class or generic
#' @return A tibble with columns `generic`, `visible`, `class`, `visible`,
#' and `source`.
#' @export
#' @examples
#' s3_methods_class("Date")
#' s3_methods_generic("anova")
#'
#' s4_methods_class("Date")
#' s4_methods_generic("anova")
s3_methods_class <- function(x) {
methods <- methods_class(x)
methods <- methods[!methods$isS4, ]
methods$isS4 <- NULL
methods
}
#' @export
#' @rdname s3_methods_class
s3_methods_generic <- function(x) {
methods <- methods_generic(x)
methods <- methods[!methods$isS4, ]
methods$isS4 <- NULL
methods
}
#' @export
#' @rdname s3_methods_class
s4_methods_class <- function(x) {
methods <- methods_class(x)
methods <- methods[methods$isS4, ]
methods$isS4 <- NULL
methods
}
#' @export
#' @rdname s3_methods_class
s4_methods_generic <- function(x) {
methods <- methods_generic(x)
methods <- methods[methods$isS4, ]
methods$isS4 <- NULL
methods
}
methods_class <- function(x) {
info <- attr(utils::methods(class = x), "info")
info <- tibble::as_tibble(info)
info <- tibble::rownames_to_column(info, "method")
info$class <- rep(x, length = nrow(info))
info$source <- as.character(info$from)
info[c("generic", "class", "visible", "source", "isS4")]
}
methods_generic <- function(x) {
info <- attr(utils::methods(x), "info")
info <- tibble::as_tibble(info, rownames = "method")
generic_esc <- gsub("([.\\[])", "\\\\\\1", x)
info$class <- gsub(paste0("^", generic_esc, "[.,]"), "", info$method)
info$class <- gsub("-method$", "", info$class)
info$source <- gsub(paste0(" for ", generic_esc), "", info$from)
info[c("generic", "class", "visible", "source", "isS4")]
}
# Needed for testing
sloop_foo <- function(x) UseMethod("sloop_foo")
sloop_foo.blah <- function(x) 1