[R] How could I see the source code of functions in an R package?

David Winsemius dwinsemius at comcast.net
Fri May 17 06:01:13 CEST 2013


Do this search with your favorite search tool

Accessing the sources ligges

(Uwe Ligges is the author of a comprehensive article on the topic in R News.)

-- 
David.
On May 16, 2013, at 6:58 PM, jpm miao wrote:

> Hi,
> 
>   How could I see the source code of functions in an R package?
>   If we type ?function_name , we will see documentations of the
> function_name.
>   If we type function_name, is what returns just the source code? Could we
> just save it in an .R file and modify as we want? However, it seems that
> sometimes the source code is hidden (or stored elsewhere?) As an example,
> could we see the source code of "xyplot" in the following example?
> 
>   Thanks,
> 
> 
>> library(lattice)
>> xyplot
> function (x, data, ...)
> UseMethod("xyplot")
> <environment: namespace:lattice>
> 
>> library(plm)
> Loading required package: bdsmatrix
> 
> Attaching package: Œbdsmatrix‚
> 
> The following object(s) are masked from Œpackage:base‚:
> 
>    backsolve
> 
> Loading required package: nlme
> Loading required package: Formula
> Loading required package: MASS
> Loading required package: sandwich
> Loading required package: zoo
> 
> Attaching package: Œzoo‚
> 
> The following object(s) are masked from Œpackage:base‚:
> 
>    as.Date, as.Date.numeric
> 
>> ?plm
>> plm
> function (formula, data, subset, na.action, effect = c("individual",
>    "time", "twoways"), model = c("within", "random", "ht", "between",
>    "pooling", "fd"), random.method = c("swar", "walhus", "amemiya",
>    "nerlove", "kinla"), inst.method = c("bvk", "baltagi"), index = NULL,
>    ...)
> {
>    nframe <- length(sys.calls())
>    is.a.list <- class(formula)[1] == "list"
>    if (is.a.list) {
>        plmlist <- match.call(expand.dots = FALSE)
>        plmlist[[1]] <- as.name("plm.list")
>        plmlist <- eval(plmlist, sys.frame(which = nframe))
>        return(plmlist)
>    }
>    dots <- list(...)
>    effect <- match.arg(effect)
>    if (!any(is.na(model)))
>        model <- match.arg(model)
>    random.method <- match.arg(random.method)
>    inst.method <- match.arg(inst.method)
>    if (!is.na(model) && model == "ht") {
>        ht <- match.call(expand.dots = FALSE)
>        m <- match(c("formula", "data", "subset", "na.action",
>            "index"), names(ht), 0)
>        ht <- ht[c(1, m)]
>        ht[[1]] <- as.name("pht")
>        ht <- eval(ht, parent.frame())
>        return(ht)
>    }
>    if (!is.null(dots$instruments)) {
>        as.Formula(formula, dots$instruments)
>        deprec.instruments <- paste("the use of the instruments argument is
> deprecated,",
>            "use two-part formulas instead")
>        warning(deprec.instruments)
>    }
>    if (inherits(data, "pdata.frame") && !is.null(index))
>        warning("the index argument is ignored because data is a
> pdata.frame")
>    if (!inherits(data, "pdata.frame"))
>        data <- pdata.frame(data, index)
>    if (!inherits(formula, "pFormula"))
>        formula <- pFormula(formula)
>    if (length(formula)[2] == 2)
>        formula <- expand.formula(formula)
>    cl <- match.call()
>    mf <- match.call(expand.dots = FALSE)
>    m <- match(c("formula", "data", "subset", "na.action"), names(mf),
>        0)
>    mf <- mf[c(1, m)]
>    mf$drop.unused.levels <- TRUE
>    mf[[1]] <- as.name("model.frame")
>    mf$formula <- formula
>    mf$data <- data
>    data <- eval(mf, parent.frame())
>    if (is.na(model)) {
>        attr(data, "formula") <- formula
>        return(data)
>    }
>    args <- list(model = model, effect = effect, random.method =
> random.method,
>        inst.method = inst.method)
>    result <- plm.fit(formula, data, model, effect, random.method,
>        inst.method)
>    result$call <- cl
>    result$args <- args
>    result
> }
> <environment: namespace:plm>
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list