[R] names of functions in a library
Henrik Bengtsson
hb at maths.lth.se
Tue Jun 7 12:02:10 CEST 2005
For a generalization of this, see ll() in the R.oo package;
> library(R.oo)
> ll(mode="function", envir="base")
member data.class dimension object.size
1 - function NULL 28
2 -.Date function NULL 5996
3 -.POSIXt function NULL 6864
<snip></snip>
1008 zapsmall function NULL 3068
1009 zip.file.extract function NULL 5232
> help(ll)
Usage:
ll(pattern=".*", ..., private=FALSE, properties=c("data.class",
"dimension", "object.size"), sortBy=NULL, envir=parent.frame())
Arguments:
pattern: Regular expression pattern specifying which members to
return. If '".*"', all names are matched.
...: A named 'vector' of format 'functionName=value', where
'functionName()' will be called on each member found. If the
result matches the 'value', the member is returned, otherwise
not.
properties: Names of properties to be returned. There must exist a
'function' with the same name, because it will be called.
This way one can extract any type of property by defining new
methods.
sortBy: Name or index of column (property) to be sorted by. If
'NULL', the objects are listed in the order they are found.
private: If 'TRUE', also private members, i.e. members with a name
starting with a '.' (period), will be listed, otherwise not.
envir: An 'environment', a search path index or a name of a package
to be scanned.
Thus, stratification on 'mode' in the example abov is taken care of by
the '...' argument.
Cheers
/Henrik
Prof Brian Ripley wrote:
> On Mon, 6 Jun 2005, Liaw, Andy wrote:
>
>>> From: Duncan Murdoch
>>>
>>> On 6/6/2005 4:43 PM, Omar Lakkis wrote:
>>>
>>>> How can I get a list of the names of all exported functions
>>>
>>> in a library?
>>>
>>>> I load my library using library() and then want to
>>>
>>> dynamically get all
>>>
>>>> functions that start with "test." to dynamically execute them.
>>>
>>>
>>> Use search() to see all the _packages_ that have been loaded.
>>> If yours
>>> is second in the list (the typical spot just after calling the
>>> unfortunately named library() function), then ls(2) will list
>>> all of its
>>> exports.
>>
>>
>> Just to nitpick a bit: That lists all _objects_ in position 2, which may
>> include objects that are not functions, although it's rare, if at all,
>> that
>> a package database would contain non-functions...
>
>
> It's actually common: lazy-loaded datasets are there too, and 'base' has
>
> [1] "F" "LETTERS" "R.version" "R.version.string"
> [5] "T" "letters" "month.abb" "month.name"
> [9] "pi" "version"
>
> 'stats' has "p.adjust.methods" ....
>
> Here's a simple function to find only the functions
>
> lsf <- function(n=2)
> {
> tmp <- ls(n, all=TRUE)
> isf <- sapply(tmp, function(x) is.function(get(x, pos=n)))
> tmp[isf]
> }
>
> and one could use the 'pattern' arg of ls() to restrict the set.
>
More information about the R-help
mailing list