[R] determining if a function exists in a particular package

Sarah Goslee sarah.goslee at gmail.com
Fri Apr 27 23:21:06 CEST 2012


You can also use exists():
> library(MASS)
> exists("loglm", where=search()[grepl("MASS", search())])
[1] TRUE
> exists("loglmx", where=search()[grepl("MASS", search())])
[1] FALSE

As for lsf.str(), this only works for loaded packages. You didn't
specify whether you needed to be able to search loaded packages,
installed packages, or packages that exist on CRAN.

Package sos might be helpful for the last of those; you can use
help.search and grep to search for functions within installed but
unloaded packages.

Since you didn't tell us anything about your objectives, it's
impossible to know what you are really looking for.

Sarah


On Fri, Apr 27, 2012 at 5:13 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
> I found the solution in an old post:
>
> It's
> lsf.str("package:ts")
>
> for functions.
>
> Cool!
>
> On Fri, Apr 27, 2012 at 4:12 PM, Mark Leeds <markleeds2 at gmail.com> wrote:
>> Hi Erin: I don't know how to do it programatically but it might be quicker
>> to just
>> go to the vignette and click on it. All the functions should be in the right
>> column of
>> the pdf. Of course, you may have reason for needing it programatically in
>> which
>> case, my apologies for the noise !!!!!
>>
>> On Fri, Apr 27, 2012 at 5:07 PM, Erin Hodgess <erinm.hodgess at gmail.com>
>> wrote:
>>>
>>> Hello R People:
>>>
>>> Is there a way to determine if a function exists in a particular
>>> package, please?
>>>
>>> I looked at exists and objects, but they seem to refer to an
>>> environment rather than a package.
>>>
>>> I was thinking of something like:
>>>
>>> ifelse(exists(functiona) in MASS, print(1:10), print(5))
>>>
>>> Thanks,
>>> Erin
>>>

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list