[R] names of functions in a library
Prof Brian Ripley
ripley at stats.ox.ac.uk
Tue Jun 7 09:09:14 CEST 2005
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.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list