I want to look at the source code for a function to see how it works. I know I can print a function by typing its name at the prompt:
> t
function (x)
UseMethod("t")
<bytecode: 0x2332948>
<environment: namespace:base>
In this case, what does UseMethod("t")
mean? How do I find the source code that's actually being used by, for example: t(1:10)
?
Is there a difference between when I see UseMethod
and when I see standardGeneric
and showMethods
, as with with
?
> with
standardGeneric for "with" defined from package "base"
function (data, expr, ...)
standardGeneric("with")
<bytecode: 0x102fb3fc0>
<environment: 0x102fab988>
Methods may be defined for arguments: data
Use showMethods("with") for currently available ones.
In other cases, I can see that R functions are being called, but I can't find the source code for those 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 do I find functions like .cbindts
and .makeNamesTs
?
In still other cases, there's a bit of R code, but most of work seems to be 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 do I find out what the .Primitive
function does? Similarly, some functions call .C
, .Call
, .Fortran
, .External
, or .Internal
. How can I find the source code for those?
View([function_name])
- eg.View(mean)
Make sure to use uppercase [V]. The read-only code will open in the editor.Didn't see how this fit into the flow of the main answer but it stumped me for a while so I'm adding it here:
Infix Operators
To see the source code of some base infix operators (e.g.,
%%
,%*%
,%in%
), usegetAnywhere
, e.g.:The main answer covers how to then use mirrors to dig deeper.
For non-primitive functions, R base includes a function called
body()
that returns the body of function. For instance the source of theprint.Date()
function can be viewed:will produce this:
If you are working in a script and want the function code as a character vector, you can get it.
will get you:
Why would I want to do such a thing? I was creating a custom S3 object (
x
, whereclass(x) = "foo"
) based on a list. One of the list members (named "fun") was a function and I wantedprint.foo()
to display the function source code, indented. So I ended up with the following snippet inprint.foo()
:which indents and displays the code associated with
x[["fun"]]
.There is a very handy function in R
edit
It will open the source code of
optim
using the editor specified in R'soptions
, and then you can edit it and assign the modified function tonew_optim
. I like this function very much to view code or to debug the code, e.g, print some messages or variables or even assign them to a global variables for further investigation (of course you can usedebug
).If you just want to view the source code and don't want the annoying long source code printed on your console, you can use
Clearly, this cannot be used to view C/C++ or Fortran source code.
BTW,
edit
can open other objects like list, matrix, etc, which then shows the data structure with attributes as well. Functionde
can be used to open an excel like editor (if GUI supports it) to modify matrix or data frame and return the new one. This is handy sometimes, but should be avoided in usual case, especially when you matrix is big.As long as the function is written in pure R not C/C++/Fortran, one may use the following. Otherwise the best way is debugging and using "jump into":
In addition to the other answers on this question and its duplicates, here's a good way to get source code for a package function without needing to know which package it's in. e.g. if we want the source for
randomForest::rfcv()
:To view/edit it in a pop-up window:
To redirect to a separate file: