[R] using functions from other packages in own package

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Aug 20 17:28:47 CEST 2013


On 20/08/2013 15:51, Jannis wrote:
> Dear R community,
>
> I am creating my own package which uses functions from other packages.
> After reading several books/help pages I now would do the following to
> safely use some functions from other packages:
>
> 1. list them in Imports: (DESCRIPTION file)
> 2. put importFrom(packagename, functionname1, functionname2) in NAMESPACE
>
> Apparently this now also works without loading the external package with
> require() or library() inside the function that uses the imported
> functions.
>
> Is this the recommended way to do this (even if there may be several
> ways...)? Is it save to remove all the library() and require() calls?

Yes, and CRAN is encouraging people to move to this model.

There is one exception: 'methods' should be in Depends: (and also 
importFrom what you need).  That is because of a bug in 'methods' as to 
where it looks for its own functions, which is at least partially fixed 
in R-patched.

> Thanks a lot
> Jannis


-- 
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