是否可以查看函数值的源代码

时间:2014-01-23 10:50:26

标签: r

我正在使用包中的函数。此函数返回一些值。例如:

k<-dtw(v1,v2, keep.internals=TRUE)

我可以得到这个值:

k$costMatrix

是否可以查看costMatrix的源代码?如果是的话我该怎么做?

更新 这是函数的源代码:

   function (x, y = NULL, dist.method = "Euclidean", step.pattern = symmetric2, 
    window.type = "none", keep.internals = FALSE, distance.only = FALSE, 
    open.end = FALSE, open.begin = FALSE, ...) 
{
    lm <- NULL
    if (is.null(y)) {
        if (!is.matrix(x)) 
            stop("Single argument requires a global cost matrix")
        lm <- x
    }
    else if (is.character(dist.method)) {
        x <- as.matrix(x)
        y <- as.matrix(y)
        lm <- proxy::dist(x, y, method = dist.method)
    }
    else if (is.function(dist.method)) {
        stop("Unimplemented")
    }
    else {
        stop("dist.method should be a character method supported by proxy::dist()")
    }
    wfun <- .canonicalizeWindowFunction(window.type)
    dir <- step.pattern
    norm <- attr(dir, "norm")
    if (!is.null(list(...)$partial)) {
        warning("Argument `partial' is obsolete. Use `open.end' instead")
        open.end <- TRUE
    }
    n <- nrow(lm)
    m <- ncol(lm)
    if (open.begin) {
        if (is.na(norm) || norm != "N") {
            stop("Open-begin requires step patterns with 'N' normalization (e.g. asymmetric, or R-J types (c)). See papers in citation().")
        }
        lm <- rbind(0, lm)
        np <- n + 1
        precm <- matrix(NA, nrow = np, ncol = m)
        precm[1, ] <- 0
    }
    else {
        precm <- NULL
        np <- n
    }
    gcm <- globalCostMatrix(lm, step.matrix = dir, window.function = wfun, 
        seed = precm, ...)
    gcm$N <- n
    gcm$M <- m
    gcm$call <- match.call()
    gcm$openEnd <- open.end
    gcm$openBegin <- open.begin
    gcm$windowFunction <- wfun
    lastcol <- gcm$costMatrix[np, ]
    if (is.na(norm)) {
    }
    else if (norm == "N+M") {
        lastcol <- lastcol/(n + (1:m))
    }
    else if (norm == "N") {
        lastcol <- lastcol/n
    }
    else if (norm == "M") {
        lastcol <- lastcol/(1:m)
    }
    gcm$jmin <- m
    if (open.end) {
        if (is.na(norm)) {
            stop("Open-end alignments require normalizable step patterns")
        }
        gcm$jmin <- which.min(lastcol)
    }
    gcm$distance <- gcm$costMatrix[np, gcm$jmin]
    if (is.na(gcm$distance)) {
        stop("No warping path exists that is allowed by costraints")
    }
    if (!is.na(norm)) {
        gcm$normalizedDistance <- lastcol[gcm$jmin]
    }
    else {
        gcm$normalizedDistance <- NA
    }
    if (!distance.only) {
        mapping <- backtrack(gcm)
        gcm <- c(gcm, mapping)
    }
    if (open.begin) {
        gcm$index1 <- gcm$index1[-1] - 1
        gcm$index2 <- gcm$index2[-1]
        lm <- lm[-1, ]
        gcm$costMatrix <- gcm$costMatrix[-1, ]
        gcm$directionMatrix <- gcm$directionMatrix[-1, ]
    }
    if (!keep.internals) {
        gcm$costMatrix <- NULL
        gcm$directionMatrix <- NULL
    }
    else {
        gcm$localCostMatrix <- lm
        if (!is.null(y)) {
            gcm$query <- x
            gcm$reference <- y
        }
    }
    class(gcm) <- "dtw"
    return(gcm)
}

但是如果我写globalCostMatrix我没有得到这个函数的源代码

2 个答案:

答案 0 :(得分:2)

查找函数的最简单方法是查看源代码。您很有可能通过在R控制台中键入函数名称,您将获得函数定义(尽管并不总是具有良好的布局,因此寻找存在括号的源代码是可行的选择)。

在您的情况下,您使用同一名称包中的函数dtw。此函数使用名为globalCostMatrix的函数。如果在R中键入该名称,则会收到未找到对象的错误。发生这种情况是因为在创建包时未导出该函数,可能是因为作者认为这不是普通用户会使用的(但看不到!),或者是为了防止与可能使用相同函数名的其他包冲突。 / p>

但是,对于感兴趣的读者,至少有两种方法可以访问此函数中的代码。一种方法是转到CRAN,下载源代码tarball并在tar球的R文件夹中找到该函数。另一个更容易,就是使用getAnywhere函数。这将为您提供函数的定义,就像您用于其他用户可访问的函数一样,如dtw

> library(dtw)
> getAnywhere("globalCostMatrix")
A single object matching ‘globalCostMatrix’ was found
It was found in the following places
  namespace:dtw
with value

function (lm, step.matrix = symmetric1, window.function = noWindow, 
    native = TRUE, seed = NULL, ...) 
{
    if (!is.stepPattern(step.matrix)) 
        stop("step.matrix is no stepMatrix object")
    n <- nrow(lm)
... omitted for brevity

答案 1 :(得分:1)

我想您想看看dtw()函数对您的数据的作用。我似乎创建了一个包含名为costMatrix的列的data.frame。

要了解如何生成列costMatrix中的数据,只需键入并执行dtw(不带括号!)。 R将在之后显示函数dtw()的来源。

相关问题