find all functions (including private) in a package
Asked Answered
D

2

34

I know ls("package:grid") and find.funs("package:grid") in mvbutils but apparently neither of them can find non-exported functions and methods that are only accessible internally or with ::: or getAnywhere.

I've had to source the files in the /R directory of the source package and use ls() on a clean global environment, but there must be a better way, no?

Demasculinize answered 1/1, 2012 at 22:59 Comment(3)
get and getFromNamespace seem to call to an .internal, and they need to know a particular name, which doesn't help much. That your read?Nano
What is a particular symbol in package:grid that you don't see listed in ls(package:grid) but expect to?Nano
481 of them, according to the solutions below setdiff(unclass(lsf.str(envir = asNamespace("grid"), all = T)), ls('package:grid'))Demasculinize
A
34

you can use asNamespace:

> methods(cbind)
[1] cbind.data.frame cbind.grobGrid   cbind.ts*       

   Non-visible functions are asterisked
> r <- unclass(lsf.str(envir = asNamespace("stats"), all = T))
> r[grep("cbind.ts", r)]
[1] ".cbind.ts" "cbind.ts" 

cbind.ts in stats package is invisible but can find in envir = asNamespace("stats").

Arrester answered 2/1, 2012 at 0:2 Comment(2)
Neat, thanks. Both answers work fine, but since only one can be accepted I'll opt for the one-liner.Demasculinize
ls(asNamespace("stats")) can also give what you want, in a vector.Aunt
E
27

This appears to be something of a perennial here.

If it's this one-liners you're after then this should be a contender (credit @Joshua):

ls(getNamespace("grid"), all.names=TRUE)

(Link is to a question that was asked after the above, but closely related).

As grid is a base package and I haven't yet moved up to R 3... I'm getting 756 functions with Version 2.15.1. vs. 503 from the unclass solution.

Euchromatin answered 25/6, 2013 at 7:4 Comment(2)
707 vs 778 in recent R; most of the setdiff seems to be internal C routines (grid:::L_textBounds) or constants (grid:::Mb).Demasculinize
Thanks. To have only private functions : setdiff( ls(getNamespace("ThePackage"), all.names=TRUE), ls("package:ThePackage", all.names=TRUE))Carvalho

© 2022 - 2024 — McMap. All rights reserved.