显示R函数的源代码 [英] show source code for function in R

查看:192
本文介绍了显示R函数的源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用lmclass::knn来查看源代码,但是我无法显示princomp的代码.是否用R或其他字节码编写了此函数(或其他函数). 使用如何在程序包中显示S4函数的源代码?.感谢您的帮助.

I can use lm or class::knn to view the source code, but I failed to show the code for princomp. Was this function(or something else) written in R or some other bytecode used. I also could not find the source code using advises from How do I show the source code of an S4 function in a package?. Thanks for any help.

> princomp
function (x, ...) 
UseMethod("princomp")
<bytecode: 0x9490010>
<environment: namespace:stats>

推荐答案

您必须询问使用该函数使用的相应方法.试试这个:

You have to ask using the corresponding method used by the function. Try this:

princomp # this is what you did without having a good enough answer
methods(princomp) # Next step, ask for the method: 'princomp.default'
getAnywhere('princomp.default') # this will show you the code

您要查找的代码是:

function (x, cor = FALSE, scores = TRUE, covmat = NULL, subset = rep(TRUE, 
    nrow(as.matrix(x))), ...) 
{
    cl <- match.call()
    cl[[1L]] <- as.name("princomp")
    if (!missing(x) && !missing(covmat)) 
        warning("both 'x' and 'covmat' were supplied: 'x' will be ignored")
    z <- if (!missing(x)) 
        as.matrix(x)[subset, , drop = FALSE]
    if (is.list(covmat)) {
        if (any(is.na(match(c("cov", "n.obs"), names(covmat))))) 
            stop("'covmat' is not a valid covariance list")
        cv <- covmat$cov
        n.obs <- covmat$n.obs
        cen <- covmat$center
    }
    else if (is.matrix(covmat)) {
        cv <- covmat
        n.obs <- NA
        cen <- NULL
    }
    else if (is.null(covmat)) {
        dn <- dim(z)
        if (dn[1L] < dn[2L]) 
            stop("'princomp' can only be used with more units than variables")
        covmat <- cov.wt(z)
        n.obs <- covmat$n.obs
        cv <- covmat$cov * (1 - 1/n.obs)
        cen <- covmat$center
    }
    else stop("'covmat' is of unknown type")
    if (!is.numeric(cv)) 
        stop("PCA applies only to numerical variables")
    if (cor) {
        sds <- sqrt(diag(cv))
        if (any(sds == 0)) 
            stop("cannot use cor=TRUE with a constant variable")
        cv <- cv/(sds %o% sds)
    }
    edc <- eigen(cv, symmetric = TRUE)
    ev <- edc$values
    if (any(neg <- ev < 0)) {
        if (any(ev[neg] < -9 * .Machine$double.eps * ev[1L])) 
            stop("covariance matrix is not non-negative definite")
        else ev[neg] <- 0
    }
    cn <- paste("Comp.", 1L:ncol(cv), sep = "")
    names(ev) <- cn
    dimnames(edc$vectors) <- if (missing(x)) 
        list(dimnames(cv)[[2L]], cn)
    else list(dimnames(x)[[2L]], cn)
    sdev <- sqrt(ev)
    sc <- if (cor) 
        sds
    else rep(1, ncol(cv))
    names(sc) <- colnames(cv)
    scr <- if (scores && !missing(x) && !is.null(cen)) 
        scale(z, center = cen, scale = sc) %*% edc$vectors
    if (is.null(cen)) 
        cen <- rep(NA_real_, nrow(cv))
    edc <- list(sdev = sdev, loadings = structure(edc$vectors, 
        class = "loadings"), center = cen, scale = sc, n.obs = n.obs, 
        scores = scr, call = cl)
    class(edc) <- "princomp"
    edc
}
<environment: namespace:stats>

我认为这是您要的.

这篇关于显示R函数的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆