By Joshua Ulrich

2013-10-07 13:58:00 8 Comments

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) 
<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, ...) 
<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), 
<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?


@Arthur Yip 2019-02-22 14:54:00

In RStudio, there are (at least) 3 ways:

  1. Press the F2 key while cursor is on any function.
  2. Click on the function name while holding Ctrl or Command
  3. View(function_name) (as stated above)

A new pane will open with the source code. If you reach .Primitive or .C you'll need another method, sorry.

@strboul 2017-12-26 11:45:54

You can also try to use print.function(), which is S3 generic, to get the function write in the console.

@Joshua Ulrich 2017-12-27 21:48:10

print.function() is a S3 method. The generic is print(). And it's generally not a good idea to call methods directly. That defeats the entire purpose of generic functions and method dispatch.

@MCH 2017-01-24 16:42:33

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":

> functionBody(functionName)

@Joshua Ulrich 2017-01-24 18:26:36

This is the same as body. identical(functionBody, body) is TRUE.

@Moody_Mudskipper 2017-11-10 22:03:50

base::body and methods::functionBody, though they're unlikey to be detahed. body could be overridden too:

@Koo 2017-01-11 02:45:22

View([function_name]) - eg. View(mean) Make sure to use uppercase [V]. The read-only code will open in the editor.

@Axeman 2017-01-11 21:27:41

This only works in Rstudio.

@Geoffrey Poole 2016-12-30 21:41:25

For non-primitive functions, R base includes a function called body() that returns the body of function. For instance the source of the print.Date() function can be viewed:


will produce this:

    if (is.null(max)) 
        max <- getOption("max.print", 9999L)
    if (max < length(x)) {
        print(format(x[seq_len(max)]), max = max, ...)
        cat(" [ reached getOption(\"max.print\") -- omitted", 
            length(x) - max, "entries ]\n")
    else print(format(x), max = max, ...)

If you are working in a script and want the function code as a character vector, you can get it.


will get you:

[1] "{"                                                                   
[2] "    if (is.null(max)) "                                              
[3] "        max <- getOption(\"max.print\", 9999L)"                      
[4] "    if (max < length(x)) {"                                          
[5] "        print(format(x[seq_len(max)]), max = max, ...)"              
[6] "        cat(\" [ reached getOption(\\\"max.print\\\") -- omitted\", "
[7] "            length(x) - max, \"entries ]\\n\")"                      
[8] "    }"                                                               
[9] "    else print(format(x), max = max, ...)"                           
[10] "    invisible(x)"                                                    
[11] "}"     

Why would I want to do such a thing? I was creating a custom S3 object (x, where class(x) = "foo") based on a list. One of the list members (named "fun") was a function and I wanted to display the function source code, indented. So I ended up with the following snippet in

sourceVector = capture.output(print(body(x[["fun"]])))
cat(paste0("      ", sourceVector, "\n"))

which indents and displays the code associated with x[["fun"]].

@MichaelChirico 2015-12-01 20:18:10

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%), use getAnywhere, e.g.:

# A single object matching ‘%%’ was found
# It was found in the following places
#   package:base
#   namespace:base
#  with value
# function (e1, e2)  .Primitive("%%")

The main answer covers how to then use mirrors to dig deeper.

@Joshua Ulrich 2015-12-01 20:32:03

smci's answer recommended getAnywhere. Or you could just use backticks if you already know the name of the operator: `%in%`.

@MichaelChirico 2015-12-01 20:37:40

@JoshuaUlrich didn't know you could use backticks! Thanks. getAnywhere is mentioned in your answer as well, but I think a specific reference to infix is useful for future reference to this answer -- I've read this page many a time and was still a bit perplexed trying to find code for such functions for a while -- and I didn't think it fit into the flow of either other answer (which are both using getAnywhere for another purpose).

@Joshua Ulrich 2013-10-07 13:58:00

UseMethod("t") is telling you that t() is a (S3) generic function that has methods for different object classes.

The S3 method dispatch system

For S3 classes, you can use the methods function to list the methods for a particular generic function or class.

> methods(t)
[1] t.default    t.ts*       

   Non-visible functions are asterisked
> methods(class="ts")
 [1] aggregate.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 are asterisked

"Non-visible functions are asterisked" means the function is not exported from its package's namespace. You can still view its source code via the ::: function (i.e. stats:::t.ts), or by using getAnywhere(). getAnywhere() is useful because you don't have to know which package the function came from.

> getAnywhere(t.ts)
A single object matching ‘t.ts’ was found
It was found in the following places
  registered S3 method for t from namespace stats
with value

function (x) 
    cl <- oldClass(x)
    other <- !(cl %in% c("ts", "mts"))
    class(x) <- if (any(other)) 
    attr(x, "tsp") <- NULL
<bytecode: 0x294e410>
<environment: namespace:stats>

The S4 method dispatch system

The S4 system is a newer method dispatch system and is an alternative to the S3 system. Here is an example of an S4 function:

> library(Matrix)
Loading required package: lattice
> chol2inv
standardGeneric for "chol2inv" defined from package "base"

function (x, ...) 
<bytecode: 0x000000000eafd790>
<environment: 0x000000000eb06f10>
Methods may be defined for arguments: x
Use  showMethods("chol2inv")  for currently available ones.

The output already offers a lot of information. standardGeneric is an indicator of an S4 function. The method to see defined S4 methods is offered helpfully:

> showMethods(chol2inv)
Function: chol2inv (package base)

getMethod can be used to see the source code of one of the methods:

> getMethod("chol2inv", "diagonalMatrix")
Method Definition:

function (x, ...) 
<bytecode: 0x000000000ea2cc70>
<environment: namespace:Matrix>

target  "diagonalMatrix"
defined "diagonalMatrix"

There are also methods with more complex signatures for each method, for example

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 the source code for one of these methods the entire signature must be supplied, e.g.

getMethod("extract" , signature = c( x = "Raster" , y = "SpatialPolygons") )

It will not suffice to supply the partial signature

#Error in getMethod("extract", signature = "SpatialPolygons") : 
#  No method found for function "extract" and signature SpatialPolygons

Functions that call unexported functions

In the case of ts.union, .cbindts and .makeNamesTs are unexported functions from the stats namespace. You can view the source code of unexported functions by using the ::: operator or getAnywhere.

> stats:::.makeNamesTs
function (...) 
    l <- as.list(substitute(list(...)))[-1L]
    nm <- names(l)
    fixup <- if (is.null(nm)) 
    else nm == ""
    dep <- sapply(l[fixup], function(x) deparse(x)[1L])
    if (is.null(nm)) 
    if (any(fixup)) 
        nm[fixup] <- dep
<bytecode: 0x38140d0>
<environment: namespace:stats>

Functions that call compiled code

Note that "compiled" does not refer to byte-compiled R code as created by the compiler package. The <bytecode: 0x294e410> line in the above output indicates that the function is byte-compiled, and you can still view the source from the R command line.

Functions that call .C, .Call, .Fortran, .External, .Internal, or .Primitive are calling entry points in compiled code, so you will have to look at sources of the compiled code if you want to fully understand the function. This GitHub mirror of the R source code is a decent place to start. The function pryr::show_c_source can be a useful tool as it will take you directly to a GitHub page for .Internal and .Primitive calls. Packages may use .C, .Call, .Fortran, and .External; but not .Internal or .Primitive, because these are used to call functions built into the R interpreter.

Calls to some of the above functions may use an object instead of a character string to reference the compiled function. In those cases, the object is of class "NativeSymbolInfo", "RegisteredNativeSymbol", or "NativeSymbol"; and printing the object yields useful information. For example, optim calls .External2(C_optimhess, res$par, fn1, gr1, con) (note that's C_optimhess, not "C_optimhess"). optim is in the stats package, so you can type stats:::C_optimhess to see information about the compiled function being called.

Compiled code in a package

If you want to view compiled code in a package, you will need to download/unpack the package source. The installed binaries are not sufficient. A package's source code is available from the same CRAN (or CRAN compatible) repository that the package was originally installed from. The download.packages() function can get the package source for you.

download.packages(pkgs = "Matrix", 
                  destdir = ".",
                  type = "source")

This will download the source version of the Matrix package and save the corresponding .tar.gz file in the current directory. Source code for compiled functions can be found in the src directory of the uncompressed and untared file. The uncompressing and untaring step can be done outside of R, or from within R using the untar() function. It is possible to combine the download and expansion step into a single call (note that only one package at a time can be downloaded and unpacked in this way):

untar(download.packages(pkgs = "Matrix",
                        destdir = ".",
                        type = "source")[,2])

Alternatively, if the package development is hosted publicly (e.g. via GitHub, R-Forge, or, you can probably browse the source code online.

Compiled code in a base package

Certain packages are considered "base" packages. These packages ship with R and their version is locked to the version of R. Examples include base, compiler, stats, and utils. As such, they are not available as separate downloadable packages on CRAN as described above. Rather, they are part of the R source tree in individual package directories under /src/library/. How to access the R source is described in the next section.

Compiled code built into the R interpreter

If you want to view the code built-in to the R interpreter, you will need to download/unpack the R sources; or you can view the sources online via the R Subversion repository or Winston Chang's github mirror.

Uwe Ligges's R news article (PDF) (p. 43) is a good general reference of how to view the source code for .Internal and .Primitive functions. The basic steps are to first look for the function name in src/main/names.c and then search for the "C-entry" name in the files in src/main/*.

@Ari B. Friedman 2013-10-26 19:37:49

If you use RStudio, it will attempt to pull the source for the function your text cursor is over if you press the F2 key.

@Sunny 2014-02-24 15:12:06

@Ari B. Friedman Sorry for this late question. Will RStudio also pull the C source code for the function or just for the functions written in R? Thanks

@Ari B. Friedman 2014-02-25 00:55:49

@Samir I believe it's just the R source.

@Tomasz Gandor 2017-05-12 08:36:11

@AriB.Friedman - thanks Ari, this is handy. In my case I still needed the knowledge shown in the answer (scale, which is S3 - I got UseMethod("scale") and then used getAnywhere(scale.default)). But plain functions work just fine.

@JimLohse 2018-03-27 14:42:55

Imitation is the sincerest form of flattery I assume this answer / wiki came first :) Before this

@yuqli 2018-08-14 16:59:51

How can we find the compiled code in this github folder? I want to find the source code for function stl and it calls a Fortran function z <- .Fortran(C_stl, x, n, as.integer(period).... But I can't find C_stl in that folder. Would it be somewhere else?

@Joshua Ulrich 2018-08-17 11:57:17

@yuqli: Note that it's C_stl, not "C_stl". Use print(stats:::C_stl) to see the NativeSymbolInfo for the C_stl object. That shows that the Fortran function name is "stl", which is in the stl.f file.

@yuqli 2018-08-17 15:04:23

@JoshuaUlrich Thanks for the response! I later searched "stl" and did also find the stl.f file. But thanks so much for teaching me the trick of printing and the difference between an object and a file.

@smci 2014-05-22 22:46:52

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:

edit(getAnywhere('rfcv'), file='source_rfcv.r')

To redirect to a separate file:

capture.output(getAnywhere('rfcv'), file='source_rfcv.r')

@smci 2014-09-12 19:10:01

@ the downvoters: this is actually valuable and took effort to find out. There are lots of reasons why you might prefer to send a long function with lots of arg-parsing logic to a file rather than multiple screens on the console. I didn't know how so I got off my ass and figured it out. Then posted it here. Shame on you for downvoting.

@smci 2015-08-21 19:48:57

Admittedly, getAnywhere is another wacky R choice of name for something which should have been called findOnSearchPath or similar.

@Sigfried 2019-02-28 11:35:48

I'll upvote this answer because it got me close to what I wanted. What I actually wanted, in RStudio, was View(foo); where foo was a function from an already loaded package.

@smci 2019-03-04 05:22:09

@Sigfried: edit() opens a text editor (of user's choice), whereas View() opens an Excel-type spreadsheet viewer for data, the latter is good for browsing (multi-columnar) data, but usually terrible for code of anything other than toy length. For example as I hint at, generally the first thing I want to do when browsing a function is skip/collapse/dummy out all the arg-parsing and default-action logic, to see what the function actually does.

@Eric 2014-12-04 23:39:33

There is a very handy function in R edit

new_optim <- edit(optim)

It will open the source code of optim using the editor specified in R's options, and then you can edit it and assign the modified function to new_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 use debug).

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. Function de 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.

@Brian Diggs 2014-12-05 06:17:57

This approach only brings up the same function source that printing the function gives (that is, the same as in the question). Getting further/deeper than that is what this question is about.

@Eric 2014-12-05 16:23:57

@BrianDiggs Yes, you are right. I did not mean to give an answer to the question, since Joshua has given a quite complete answer. I just try to add something related to the topic, interesting and may be useful to know about.

@Selva 2014-08-12 07:01:47

It gets revealed when you debug using the debug() function. Suppose you want to see the underlying code in t() transpose function. Just typing 't', doesn't reveal much.

function (x) 
<bytecode: 0x000000003085c010>
<environment: namespace:base>

But, Using the 'debug(functionName)', it reveals the underlying code, sans the internals.

> debug(t)
> t(co2)
debugging in: t(co2)
debug: UseMethod("t")
debugging in: t.ts(co2)
debug: {
    cl <- oldClass(x)
    other <- !(cl %in% c("ts", "mts"))
    class(x) <- if (any(other)) 
    attr(x, "tsp") <- NULL
debug: cl <- oldClass(x)
debug: other <- !(cl %in% c("ts", "mts"))
debug: class(x) <- if (any(other)) cl[other]
debug: attr(x, "tsp") <- NULL
debug: t(x)

EDIT: debugonce() accomplishes the same without having to use undebug()

@Brian Diggs 2014-08-12 15:33:38

The downsides of this method compared to the ones given in the accepted answer are that you need a working function call (all necessary parameters specified, acceptably); and that, in addition to the initial block of code, you also get each block at the time it is run. This is great for debugging, but not optimal for just getting the source.

@Selva 2014-08-13 16:21:29

Yes, its not optimal. But if you are clever, you can get the source quick and dirty, esp for in-built functions.

@Joshua Ulrich 2014-08-18 20:20:07

I'd also recommend using debugonce instead of debug in this instance.

Related Questions

Sponsored Content

86 Answered Questions

[SOLVED] How do JavaScript closures work?

13 Answered Questions

[SOLVED] How to join (merge) data frames (inner, outer, left, right)

22 Answered Questions

[SOLVED] Set a default parameter value for a JavaScript function

37 Answered Questions

[SOLVED] var functionName = function() {} vs function functionName() {}

19 Answered Questions

[SOLVED] How to sort a dataframe by multiple column(s)

23 Answered Questions

[SOLVED] How to make a great R reproducible example

  • 2011-05-11 11:12:02
  • Andrie
  • 253469 View
  • 2476 Score
  • 23 Answer
  • Tags:   r r-faq

10 Answered Questions

[SOLVED] What does the exclamation mark do before the function?

  • 2010-09-20 21:21:51
  • Sebastian Otto
  • 144828 View
  • 1140 Score
  • 10 Answer
  • Tags:   javascript function

31 Answered Questions

[SOLVED] What's the difference between a method and a function?

9 Answered Questions

[SOLVED] Grouping functions (tapply, by, aggregate) and the *apply family

3 Answered Questions

[SOLVED] non-numeric matrix extent error when plotting in R

  • 2013-02-10 08:56:23
  • nopeva
  • 9299 View
  • 1 Score
  • 3 Answer
  • Tags:   r

Sponsored Content