kjo kjo - 2 months ago 6x
R Question

How to find the file where a function is defined?

The question's title pretty much says it all.

The solution given here doesn't always work. E.g.,

> class(facet_grid)
[1] "function"
> attr(facet_grid, "srcref")
> attributes(facet_grid)

42- 42-

The environment()-function will return the package in which a function is "located" after it is loaded.

> environment(facet_grid)
<environment: namespace:ggplot2>

After downloading ggplot2_version_whatever.tag.gz from CRAN (or perhaps github) and expanding it, you can find (using your system text search facilities) a file named facet-grid.r that has this definiton starting at line 125:

facet_grid <- function(facets, margins = FALSE, scales = "fixed", space = "fixed", shrink = TRUE, labeller = "label_value", as.table = TRUE, switch = NULL, drop = TRUE) {

You should find more comments. Comments are dropped during compiling unless you make special efforts to retain them.