jpm miao
2013-May-17 01:58 UTC
[R] How could I see the source code of functions in an R package?
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) > xyplotfunction (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 > plmfunction (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]]
arun
2013-May-17 02:30 UTC
[R] How could I see the source code of functions in an R package?
HI, methods(xyplot) #[1] xyplot.formula* xyplot.ts*???? # ?#? Non-visible functions are asterisked lattice:::xyplot.formula function (x, data = NULL, allow.multiple = is.null(groups) || ??? outer, outer = !is.null(groups), auto.key = FALSE, aspect = "fill", ??? panel = lattice.getOption("panel.xyplot"), prepanel = NULL, ??? scales = list(), strip = TRUE, groups = NULL, xlab, xlim, ??? ylab, ylim, drop.unused.levels = lattice.getOption("drop.unused.levels"), ??? ..., lattice.options = NULL, default.scales = list(), default.prepanel = lattice.getOption("prepanel.default.xyplot"), ??? subscripts = !is.null(groups), subset = TRUE) -------------------------------------------------------- ------------------------------------------------------- ?????????? names(foo$legend) <- foo$legend[[1]]$args$space ??? } ??? class(foo) <- "trellis" ??? foo } lattice:::xyplot.ts A.K. 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) > xyplotfunction (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 > plmfunction (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>
David Winsemius
2013-May-17 04:01 UTC
[R] How could I see the source code of functions in an R package?
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