R语言源代码查看
发布时间
阅读量:
阅读量
源代码查看R
对funciton说why时,这三个方法基本够用
edit ;getAnywhere;methods
- edit
edit(getAnywhere)
# result:
function (x)
{
if (tryCatch(!is.character(x), error = function(e) TRUE))
x <- as.character(substitute(x))
objs <- list()
where <- character()
visible <- logical()
if (length(pos <- find(x, numeric = TRUE))) {
objs <- lapply(pos, function(pos, x) get(x, pos = pos),
x = x)
where <- names(pos)
visible <- rep.int(TRUE, length(pos))
}
if (length(grep(".", x, fixed = TRUE))) {
np <- length(parts <- strsplit(x, ".", fixed = TRUE)[[1L]])
for (i in 2:np) {
gen <- paste(parts[1L:(i - 1)], collapse = ".")
cl <- paste(parts[i:np], collapse = ".")
if (gen == "" || cl == "")
next
Call <- substitute(getS3method(gen, cl, TRUE), list(gen = gen,
cl = cl))
f <- eval.parent(Call)
if (!is.null(f) && !is.null(environment(f))) {
ev <- topenv(environment(f), baseenv())
nmev <- if (isNamespace(ev))
getNamespaceName(ev)
else NULL
objs <- c(objs, list(f))
msg <- paste("registered S3 method for", gen)
if (!is.null(nmev))
msg <- paste(msg, "from namespace", nmev)
where <- c(where, msg)
visible <- c(visible, FALSE)
}
}
}
for (i in loadedNamespaces()) {
ns <- asNamespace(i)
if (exists(x, envir = ns, inherits = FALSE)) {
f <- get(x, envir = ns, inherits = FALSE)
objs <- c(objs, list(f))
where <- c(where, paste0("namespace:", i))
visible <- c(visible, FALSE)
}
}
ln <- length(objs)
dups <- rep.int(FALSE, ln)
if (ln > 1L)
for (i in 2L:ln) for (j in 1L:(i - 1L)) if (identical(objs[[i]],
objs[[j]], ignore.environment = TRUE)) {
dups[i] <- TRUE
break
}
structure(list(name = x, objs = objs, where = where, visible = visible,
dups = dups), class = "getAnywhere")
}
- getAnywhere
getAnywhere(methods)
# reulst :
A single object matching ‘methods’ was found
It was found in the following places
package:utils
namespace:utils
with value
function (generic.function, class)
{
envir <- parent.frame()
if (!missing(generic.function) && !is.character(generic.function)) {
what <- substitute(generic.function)
if (is.function(generic.function) && is.call(what) &&
(deparse(what[[1L]])[1L] %in% c("::", ":::"))) {
generic.function <- as.character(what[[3L]])
envir <- asNamespace(as.character(what[[2L]]))
}
else generic.function <- deparse(what)
}
if (!missing(class) && !is.character(class))
class <- paste(deparse(substitute(class)))
s3 <- .S3methods(generic.function, class, envir)
s4 <- if (.isMethodsDispatchOn()) {
methods::.S4methods(generic.function, class)
}
else NULL
.MethodsFunction(s3, s4, missing(generic.function))
}
<bytecode: 0x000001a5618bcdc8>
<environment: namespace:utils>
- methods –generic.function
> methods(generic.function = "print")[1:3]
[1] "print.acf" "print.anova" "print.aov"
- methods –class
> methods(class = "POSIXct")[1:10]
[1] "[.POSIXct" "[[.POSIXct"
[3] "[<-.POSIXct" "as.data.frame.POSIXct"
[5] "as.Date.POSIXct" "as.list.POSIXct"
[7] "as.POSIXlt.POSIXct" "c.POSIXct"
[9] "coerce,oldClass,S3-method" "format.POSIXct"
全部评论 (0)
还没有任何评论哟~
