r - How can I view the source code for a function? -
i want @ source code function see how works. know can print function typing name @ prompt:
> t function (x) usemethod("t") <bytecode: 0x2332948> <environment: namespace:base>
in case, usemethod("t")
mean? how find source code that's being used by, example: t(1:10)
?
in other cases, can see r functions being called, can't find source code functions.
> ts.union function (..., dframe = false) .cbind.ts(list(...), .makenamests(...), dframe = dframe, union = true) <bytecode: 0x36fbf88> <environment: namespace:stats> > .cbindts error: object '.cbindts' not found > .makenamests error: object '.makenamests' not found
how find functions .cbindts
, .makenamests
?
in still other cases, there's bit of r code, of work seems done somewhere else.
> matrix function (data = na, nrow = 1, ncol = 1, byrow = false, dimnames = null) { if (is.object(data) || !is.atomic(data)) data <- as.vector(data) .internal(matrix(data, nrow, ncol, byrow, dimnames, missing(nrow), missing(ncol))) } <bytecode: 0x134bd10> <environment: namespace:base> > .internal function (call) .primitive(".internal") > .primitive function (name) .primitive(".primitive")
how find out .primitive
function does? similarly, functions call .c
, .call
, .fortran
, .external
, or .internal
. how can find source code those?
usemethod("t")
telling t()
(s3) generic function has methods different object classes.
the s3 method dispatch system
for s3 classes, can use methods
function list methods particular generic function or class.
> methods(t) [1] t.data.frame t.default t.ts* non-visible functions asterisked > methods(class="ts") [1] aggregate.ts as.data.frame.ts cbind.ts* cycle.ts* [5] diffinv.ts* diff.ts kernapply.ts* lines.ts [9] monthplot.ts* na.omit.ts* ops.ts* plot.ts [13] print.ts time.ts* [<-.ts* [.ts* [17] t.ts* window<-.ts* window.ts* non-visible functions asterisked
"non-visible functions asterisked" means function not exported package's namespace. can still view source code via :::
function (i.e. stats:::t.ts
), or using getanywhere()
. getanywhere()
useful because don't have know package function came from.
> getanywhere(t.ts) single object matching ‘t.ts’ found found in following places registered s3 method t namespace stats namespace:stats value function (x) { cl <- oldclass(x) other <- !(cl %in% c("ts", "mts")) class(x) <- if (any(other)) cl[other] attr(x, "tsp") <- null t(x) } <bytecode: 0x294e410> <environment: namespace:stats>
the s4 method dispatch system
the s4 system newer method dispatch system , alternative s3 system. here example of s4 function:
> library(matrix) loading required package: lattice > chol2inv standardgeneric "chol2inv" defined package "base" function (x, ...) standardgeneric("chol2inv") <bytecode: 0x000000000eafd790> <environment: 0x000000000eb06f10> methods may defined arguments: x use showmethods("chol2inv") available ones.
the output offers lot of information. standardgeneric
indicator of s4 function. method see defined s4 methods offered helpfully:
> showmethods(chol2inv) function: chol2inv (package base) x="any" x="chmfactor" x="densematrix" x="diagonalmatrix" x="dtrmatrix" x="sparsematrix"
getmethod
can used see source code of 1 of methods:
> getmethod("chol2inv", "diagonalmatrix") method definition: function (x, ...) { chk.s(...) tcrossprod(solve(x)) } <bytecode: 0x000000000ea2cc70> <environment: namespace:matrix> signatures: x target "diagonalmatrix" defined "diagonalmatrix"
there methods more complex signatures each method, example
require(raster) showmethods(extract) function: extract (package raster) x="raster", y="data.frame" x="raster", y="extent" x="raster", y="matrix" x="raster", y="spatiallines" x="raster", y="spatialpoints" x="raster", y="spatialpolygons" x="raster", y="vector"
to see source code 1 of these methods entire signature must supplied, e.g.
getmethod("extract" , signature = c( x = "raster" , y = "spatialpolygons") )
it not suffice supply partial signature
getmethod("extract",signature="spatialpolygons") #error in getmethod("extract", signature = "spatialpolygons") : # no method found function "extract" , signature spatialpolygons
functions call unexported functions
in case of ts.union
, .cbindts
, .makenamests
unexported functions stats
namespace. can view source code of unexported functions using :::
operator or getanywhere
.
> stats:::.makenamests function (...) { l <- as.list(substitute(list(...)))[-1l] nm <- names(l) fixup <- if (is.null(nm)) seq_along(l) else nm == "" dep <- sapply(l[fixup], function(x) deparse(x)[1l]) if (is.null(nm)) return(dep) if (any(fixup)) nm[fixup] <- dep nm } <bytecode: 0x38140d0> <environment: namespace:stats>
functions call compiled code
note "compiled" not refer byte-compiled r code created compiler package. <bytecode: 0x294e410>
line in above output indicates function byte-compiled, , can still view source r command line.
functions call .c
, .call
, .fortran
, .external
, .internal
, or .primitive
calling entry points in compiled code, have @ sources of compiled code if want understand function. this github mirror of r source code decent place start. function pryr::show_c_source
can useful tool take directly github page .internal
, .primitive
calls. packages may use .c
, .call
, .fortran
, , .external
; not .internal
or .primitive
, because these used call functions built r interpreter.
calls of above functions may use object instead of character string reference compiled function. in cases, object of class "nativesymbolinfo"
, "registerednativesymbol"
, or "nativesymbol"
; , printing object yields useful information. example, optim
calls .external2(c_optimhess, res$par, fn1, gr1, con)
(note that's c_optimhess
, not "c_optimhess"
). optim
in stats package, can type stats:::c_optimhess
see information compiled function being called.
compiled code in package
if want view compiled code in package, need download/unpack package source. installed binaries not sufficient. package's source code available same cran (or cran compatible) repository package installed from. download.packages()
function can package source you.
download.packages(pkgs = "matrix", destdir = ".", type = "source")
this download source version of matrix package , save corresponding .tar.gz
file in current directory. source code compiled functions can found in src
directory of uncompressed , untared file. uncompressing , untaring step can done outside of r
, or within r
using untar()
function. possible combine download , expansion step single call (note 1 package @ time can downloaded , unpacked in way):
untar(download.packages(pkgs = "matrix", destdir = ".", type = "source")[,2])
alternatively, if package development hosted publicly (e.g. via github, r-forge, or rforge.net), can browse source code online.
compiled code in base package
certain packages considered "base" packages. these packages ship r , version locked version of r. examples include base
, compiler
, stats
, , utils
. such, not available separate downloadable packages on cran described above. rather, part of r source tree in individual package directories under /src/library/
. how access r source described in next section.
compiled code built r interpreter
if want view code built-in r interpreter, need download/unpack r sources; or can view sources online via r subversion repository or winston chang's github mirror.
uwe ligges's r news article (pdf) (p. 43) general reference of how view source code .internal
, .primitive
functions. basic steps first function name in src/main/names.c
, search "c-entry" name in files in src/main/*
.
Comments
Post a Comment