方法一:直接写函数名称,如在R中查看回归分析代码:
lm
直接可以查看到
function (formula, data, subset, weights, na.action, method = "qr",
model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE,
contrasts = NULL, offset, ...)
{
ret.x
ret.y
cl
mf
m
"offset"), names(mf), 0L)
mf
mf$drop.unused.levels
mf[[1L]]
mf
if (method == "model.frame")
return(mf)
else if (method != "qr")
warning(gettextf("method = '%s' is not supported. Using 'qr'",
method), domain = NA)
mt
y
w
if (!is.null(w) && !is.numeric(w))
stop("'weights' must be a numeric vector")
offset
if (!is.null(offset)) {
if (length(offset) != NROW(y))
stop(gettextf("number of offsets is %d, should equal %d (number of observations)",
length(offset), NROW(y)), domain = NA)
}
if (is.empty.model(mt)) {
x
z
3) else numeric(), residuals = y, fitted.values = 0 *
y, weights = w, rank = 0L, df.residual = if (!is.null(w)) sum(w !=
0) else if (is.matrix(y)) nrow(y) else length(y))
if (!is.null(offset)) {
z$fitted.values
z$residuals
}
}
else {
x
z
lm.fit(x, y, offset = offset, singular.ok = singular.ok,
...)
else lm.wfit(x, y, w, offset = offset, singular.ok = singular.ok,
...)
}
class(z)
z$na.action
z$offset
z$contrasts
z$xlevels
z$call
z$terms
if (model)
z$model
if (ret.x)
z$x
if (ret.y)
z$y
if (!qr)
z$qr
z
}
优点:直接简单。
缺点:并非所有的函数都能通过此方法得到。
原因:R是面向对象设计的程序语言。
方法二:与方法一类似,用函数page(),不过,结果在另一个窗口显示。
方法三:与方法二类似,用函数edit()。
方法四:对于计算方法不同的函数,要用methods()来定义具体的查看对象,如查看函数mean代码,用方法一只能查到
function (x, ...)
UseMethod("mean")
无法得到具体的代码。此时要有methods()来查找mean具体的对象
methods(mean)
此时,结果是
[1] mean.Date mean.default mean.difftime mean.POSIXct mean.POSIXlt
要查看具体名称,如mean.default的代码,直接用代码
mean.default
可以看到mean.default的源代码
function (x, trim = 0, na.rm = FALSE, ...)
{
if (!is.numeric(x) && !is.complex(x) && !is.logical(x)) {
warning("argument is not numeric or logical: returning NA")
return(NA_real_)
}
if (na.rm)
x
if (!is.numeric(trim) || length(trim) != 1L)
stop("'trim' must be numeric of length one")
n
if (trim > 0 && n) {
if (is.complex(x))
stop("trimmed means are not defined for complex data")
if (anyNA(x))
return(NA_real_)
if (trim >= 0.5)
return(stats::median(x, na.rm = FALSE))
lo
hi
x
}
.Internal(mean(x))
}
注意:对于程序包里的函数,需要先调用函数所在的包。
对于methods()得出的类函数中带星号标注的源代码是看不到的。
对于非类函数,不能用此方法。
方法五:对于方法四中methods()得出的类函数中带星号标注的源代码,用函数getAnywhere(),如查找predict函数的源代码。
methods(predict)
结果显示:
[1] predict.ar* predict.Arima* predict.arima0* predict.glm predict.HoltWinters* predict.lm
[7] predict.loess* predict.mlm* predict.nls* predict.poly* predict.ppr* predict.prcomp*
[13] predict.princomp* predict.smooth.spline* predict.smooth.spline.fit* predict.StructTS*
若用命令predict.Arima查看predict.Arima源代码。结果显示:
错误: 找不到对象'predict.Arima'
此时,用
getAnywhere(predict.Arima)
这样就可以查看到predict.Arima的源代码。
function (object, n.ahead = 1L, newxreg = NULL, se.fit = TRUE,
...)
{
myNCOL
0
else NCOL(x)
rsd
xr
xreg
eval.parent(xr)
else NULL
ncxreg
if (myNCOL(newxreg) != ncxreg)
stop("'xreg' and 'newxreg' have different numbers of columns")
class(xreg)
xtsp
n
arma
coefs
narma
if (length(coefs) > narma) {
if (names(coefs)[narma + 1L] == "intercept") {
xreg
newxreg
ncxreg
}
xm
drop(as.matrix(newxreg) %*% coefs)
else drop(as.matrix(newxreg) %*% coefs[-(1L:narma)])
}
else xm
if (arma[2L] > 0L) {
ma
if (any(Mod(polyroot(c(1, ma))) < 1))
warning("MA part of model is not invertible")
}
if (arma[4L] > 0L) {
ma
if (any(Mod(polyroot(c(1, ma))) < 1))
warning("seasonal MA part of model is not invertible")
}
z
pred
frequency = xtsp[3L])
if (se.fit) {
se
deltat(rsd), frequency = xtsp[3L])
return(list(pred = pred, se = se))
}
else return(pred)
}
方法六:直接上CRAN 下载源代码包
流程如下:登入R主页 http://www.r-project.org/ ,点击 Download 下的CRAN;
选择一个镜像;
里面的Source Code for all Platforms下有各种源码了,对于程序包,点packages;
点选择项Table of available packages, sorted by name;
找到你你想要的包,点击看Package source这一项,用tar.gz封装的,下载解压后就能看见源代码了。
很多函数的核心是用C或FORTRAN等写的,利用.C(),.FORTRAN()等函数调用。这种做法是出于计算效率的考虑。
最后,如果真的想阅读组成R系统本身的源代码,在各个CRAN中均有下载。都是精心挑选过的算法,是学习的好材料。同时,你可以看到R系统内部是如何构成的,对于高效使用R有至关重要的作用。