2014-01-23 36 views
1

我正在使用程序包中的函数。这个函数返回一些值。例如:是否有可能看到函数的值的源代码

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

,我可以得到这个值:

k$costMatrix 

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

UPDATE 这是函数的源代码:

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我不明白这个功能

+0

我认为'#here some code'会告诉你它是如何为globalCostMatrix创建数据,然后是这个'costMatrix'。你不能直接访问'globalCostMatrix',因为它被封装在其他函数定义中。 – Llopis

+0

我已经复制完整的代码。我不能在这个函数中看到'globalCostMatrix'的实现 – Kaja

+3

你可以到CRAN的网站找到'dtw'包,找到函数的定义,下载源tar文件夹并转到'/ R'文件夹中的'globalCostMatrix.R'文件。你在R中没有看到这个函数,因为它没有被导出到'NAMESPACE'文件中。 –

回答

2

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

在你的情况,你有一个功能dtw来自同一个名称包。该功能使用称为globalCostMatrix的功能。如果您将该名称输入到R中,您将收到一个错误,指出该对象未找到。发生这种情况是因为创建包时未导出函数,可能是因为作者认为这不是普通用户可以使用(但看不到!)或防止与可能使用相同函数名称的其他包发生冲突。

但是,对于感兴趣的读者,至少有两种方法可以访问此函数中的代码。一种方法是前往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

我想你想看到什么的源代码功能dtw()会处理您的数据。我似乎创建了一个data.frame,其中包含一个名为costMatrix的列。

要了解如何生成列costMatrix中的数据,只需键入并执行dtw(不含括号!)。 R会告诉你之后的功能dtw()的来源。

+0

我已更新我的文章 – Kaja

相关问题