Correct use of Depends, Imports and ::

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Correct use of Depends, Imports and ::

Mauricio Zambrano-Bigiarini
Dear R developers,

Taking advantage of the yesterday discussion about the use of
Depends/Import/Suggests/Enhances, I would like to add a related question.

Let's assume, in the DESCRIPTION file of my package foo0, I have:

Depends: foo1
Imports: foo2

while in the NAMESPACE file of my package I have

importFrom("foo2", f2)


and within my package I use the following two external functions:

f1, from package foo1
f2, from package foo2


If I want to use the two previous functions within a function of my
package foo0, is it correct to call them as follow ( assuming 'x' is the
only argument of 'f1' and 'f2'):

f1(x)
foo2::f2(x)

OR should I use

foo1::f1(x)
f2(x)

OR

f1(x)
f2(x)

OR

something else (the correct way of doing it in R) ?



Finally, can I use the following call in the body of a function of my
package foo0:

foo3::f3(x)

without declaring the package 'foo3' in the Imports section of my
DESCRIPTION file ?


Thanks in advance for your help,


Mauricio Zambrano-Bigiarini

--
=================================================
Water Resources Unit
Institute for Environment and Sustainability (IES)
Joint Research Centre (JRC), European Commission
TP 261, Via Enrico Fermi 2749, 21027 Ispra (VA), IT
webinfo    : http://floods.jrc.ec.europa.eu/
=================================================
DISCLAIMER:\ "The views expressed are purely those of th...{{dropped:11}}

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Correct use of Depends, Imports and ::

Simon Urbanek
On Nov 7, 2012, at 4:04 AM, Mauricio Zambrano-Bigiarini wrote:

> Dear R developers,
>
> Taking advantage of the yesterday discussion about the use of Depends/Import/Suggests/Enhances, I would like to add a related question.
>
> Let's assume, in the DESCRIPTION file of my package foo0, I have:
>
> Depends: foo1
> Imports: foo2
>
> while in the NAMESPACE file of my package I have
>
> importFrom("foo2", f2)
>
>
> and within my package I use the following two external functions:
>
> f1, from package foo1
> f2, from package foo2
>
>
> If I want to use the two previous functions within a function of my package foo0, is it correct to call them as follow ( assuming 'x' is the only argument of 'f1' and 'f2'):
>
> f1(x)
> foo2::f2(x)
>
> OR should I use
>
> foo1::f1(x)
> f2(x)
>
> OR
>
> f1(x)
> f2(x)
>

Yes, this one. You have imported f2 so the whole point of that is that it will be inserted as "f2" into your imports environment, so you want to use it that way. When you use foo2:: you are skipping over imports and addressing the namespace of foo2 directly which sort of defeats the purpose (and is slower).

As for f1, you have attached the package so it is available. I would not use foo1:: for the same reason as above, but there is a risk that it may get clobbered since the call will go through the search path so you may not get f1 from foo1 necessarily - this may or may not be a good thing depending on your intentions. You could import it via import(foo1) to make sure you don't need to go through the search path - that's what I would do if I don't intend overrides. (Whether you want to use Depends or Imports in that case is related to what you intend for the user to see as I was saying in my last comment - if I expect users to use foo1 as well, I would use Depends, otherwise Imports is good enough.)


> OR
>
> something else (the correct way of doing it in R) ?
>
>
>
> Finally, can I use the following call in the body of a function of my package foo0:
>
> foo3::f3(x)
>
> without declaring the package 'foo3' in the Imports section of my DESCRIPTION file ?
>

No, it has to be at least in Enhances or Suggests (see R-exts 1.1.1).

Cheers,
Simon


>
> Thanks in advance for your help,
>
>
> Mauricio Zambrano-Bigiarini
>
> --
> =================================================
> Water Resources Unit
> Institute for Environment and Sustainability (IES)
> Joint Research Centre (JRC), European Commission
> TP 261, Via Enrico Fermi 2749, 21027 Ispra (VA), IT
> webinfo    : http://floods.jrc.ec.europa.eu/
> =================================================
> DISCLAIMER:\ "The views expressed are purely those of th...{{dropped:11}}
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Correct use of Depends, Imports and ::

Simon Urbanek

On Nov 7, 2012, at 9:25 AM, Simon Urbanek wrote:

> On Nov 7, 2012, at 4:04 AM, Mauricio Zambrano-Bigiarini wrote:
>
>> Dear R developers,
>>
>> Taking advantage of the yesterday discussion about the use of Depends/Import/Suggests/Enhances, I would like to add a related question.
>>
>> Let's assume, in the DESCRIPTION file of my package foo0, I have:
>>
>> Depends: foo1
>> Imports: foo2
>>
>> while in the NAMESPACE file of my package I have
>>
>> importFrom("foo2", f2)
>>
>>
>> and within my package I use the following two external functions:
>>
>> f1, from package foo1
>> f2, from package foo2
>>
>>
>> If I want to use the two previous functions within a function of my package foo0, is it correct to call them as follow ( assuming 'x' is the only argument of 'f1' and 'f2'):
>>
>> f1(x)
>> foo2::f2(x)
>>
>> OR should I use
>>
>> foo1::f1(x)
>> f2(x)
>>
>> OR
>>
>> f1(x)
>> f2(x)
>>
>
> Yes, this one. You have imported f2 so the whole point of that is that it will be inserted as "f2" into your imports environment, so you want to use it that way. When you use foo2:: you are skipping over imports and addressing the namespace of foo2 directly which sort of defeats the purpose (and is slower).
>
> As for f1, you have attached the package so it is available. I would not use foo1:: for the same reason as above, but there is a risk that it may get clobbered since the call will go through the search path so you may not get f1 from foo1 necessarily - this may or may not be a good thing depending on your intentions. You could import it via import(foo1) to make sure you don't need to go through the search path - that's what I would do if I don't intend overrides. (Whether you want to use Depends or Imports in that case is related to what you intend for the user to see as I was saying in my last comment - if I expect users to use foo1 as well, I would use Depends, otherwise Imports is good enough.)
>
>
>> OR
>>
>> something else (the correct way of doing it in R) ?
>>
>>
>>
>> Finally, can I use the following call in the body of a function of my package foo0:
>>
>> foo3::f3(x)
>>
>> without declaring the package 'foo3' in the Imports section of my DESCRIPTION file ?
>>
>
> No, it has to be at least in Enhances or Suggests (see R-exts 1.1.1).
>

This should have been "Yes, but it has ..."


> Cheers,
> Simon
>
>
>>
>> Thanks in advance for your help,
>>
>>
>> Mauricio Zambrano-Bigiarini
>>
>> --
>> =================================================
>> Water Resources Unit
>> Institute for Environment and Sustainability (IES)
>> Joint Research Centre (JRC), European Commission
>> TP 261, Via Enrico Fermi 2749, 21027 Ispra (VA), IT
>> webinfo    : http://floods.jrc.ec.europa.eu/
>> =================================================
>> DISCLAIMER:\ "The views expressed are purely those of th...{{dropped:11}}
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Correct use of Depends, Imports and ::

Mauricio Zambrano-Bigiarini


On 11/07/12, Simon Urbanek  <[hidden email]> wrote:

> On Nov 7, 2012, at 9:25 AM, Simon Urbanek wrote:
>
> > On Nov 7, 2012, at 4:04 AM, Mauricio Zambrano-Bigiarini wrote:
> >
> >> Dear R developers,
> >>
> >> Taking advantage of the yesterday discussion about the use of Depends/Import/Suggests/Enhances, I would like to add a related question.
> >>
> >> Let's assume, in the DESCRIPTION file of my package foo0, I have:
> >>
> >> Depends: foo1
> >> Imports: foo2
> >>
> >> while in the NAMESPACE file of my package I have
> >>
> >> importFrom("foo2", f2)
> >>
> >>
> >> and within my package I use the following two external functions:
> >>
> >> f1, from package foo1
> >> f2, from package foo2
> >>
> >>
> >> If I want to use the two previous functions within a function of my package foo0, is it correct to call them as follow ( assuming 'x' is the only argument of 'f1' and 'f2'):
> >>
> >> f1(x)
> >> foo2::f2(x)
> >>
> >> OR should I use
> >>
> >> foo1::f1(x)
> >> f2(x)
> >>
> >> OR
> >>
> >> f1(x)
> >> f2(x)
> >>
> >
> > Yes, this one. You have imported f2 so the whole point of that is that it will be inserted as "f2" into your imports environment, so you want to use it that way. When you use foo2:: you are skipping over imports and addressing the namespace of foo2 directly which sort of defeats the purpose (and is slower).

Thanks Simon for making the point related to f2 clearer for me.

> >
> > As for f1, you have attached the package so it is available. I would not use foo1:: for the same reason as above, but there is a risk that it may get clobbered since the call will go through the search path so you may not get f1 from foo1 necessarily - this may or may not be a good thing depending on your intentions. You could import it via import(foo1) to make sure you don't need to go through the search path - that's what I would do if I don't intend overrides. (Whether you want to use Depends or Imports in that case is related to what you intend for the user to see as I was saying in my last comment - if I expect users to use foo1 as well, I would use Depends, otherwise Imports is good enough.)

So, if I'm only using a few functions of foo1, I shouldn't declare them in Depends, but only in Imports, which also will allow me to be sure that the functions come from foo1 and not from another package that have functions with the same, right ?

> >
> >
> >> OR
> >>
> >> something else (the correct way of doing it in R) ?
> >>
> >>
> >>
> >> Finally, can I use the following call in the body of a function of my package foo0:
> >>
> >> foo3::f3(x)
> >>
> >> without declaring the package 'foo3' in the Imports section of my DESCRIPTION file ?
> >>
> >
> > No, it has to be at least in Enhances or Suggests (see R-exts 1.1.1).
> >
>
> This should have been "Yes, but it has ..."

Thanks again,

Mauricio

=====================================
Linux user #454569 -- Ubuntu user #17469
=====================================
"If you torture any data set long enough,
it will confess anything!" (Murray Lark)

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Correct use of Depends, Imports and ::

Prof Brian Ripley
On 07/11/2012 17:52, mauricio zambrano wrote:

>
>
> On 11/07/12, Simon Urbanek  <[hidden email]> wrote:
>
>> On Nov 7, 2012, at 9:25 AM, Simon Urbanek wrote:
>>
>>> On Nov 7, 2012, at 4:04 AM, Mauricio Zambrano-Bigiarini wrote:
>>>
>>>> Dear R developers,
>>>>
>>>> Taking advantage of the yesterday discussion about the use of
>>>> Depends/Import/Suggests/Enhances, I would like to add a related
>>>> question.
>>>>
>>>> Let's assume, in the DESCRIPTION file of my package foo0, I
>>>> have:
>>>>
>>>> Depends: foo1 Imports: foo2
>>>>
>>>> while in the NAMESPACE file of my package I have
>>>>
>>>> importFrom("foo2", f2)
>>>>
>>>>
>>>> and within my package I use the following two external
>>>> functions:
>>>>
>>>> f1, from package foo1 f2, from package foo2
>>>>
>>>>
>>>> If I want to use the two previous functions within a function
>>>> of my package foo0, is it correct to call them as follow (
>>>> assuming 'x' is the only argument of 'f1' and 'f2'):
>>>>
>>>> f1(x) foo2::f2(x)
>>>>
>>>> OR should I use
>>>>
>>>> foo1::f1(x) f2(x)
>>>>
>>>> OR
>>>>
>>>> f1(x) f2(x)
>>>>
>>>
>>> Yes, this one. You have imported f2 so the whole point of that is
>>> that it will be inserted as "f2" into your imports environment,
>>> so you want to use it that way. When you use foo2:: you are
>>> skipping over imports and addressing the namespace of foo2
>>> directly which sort of defeats the purpose (and is slower).
>
> Thanks Simon for making the point related to f2 clearer for me.
>
>>>
>>> As for f1, you have attached the package so it is available. I
>>> would not use foo1:: for the same reason as above, but there is a
>>> risk that it may get clobbered since the call will go through the
>>> search path so you may not get f1 from foo1 necessarily - this
>>> may or may not be a good thing depending on your intentions. You
>>> could import it via import(foo1) to make sure you don't need to
>>> go through the search path - that's what I would do if I don't
>>> intend overrides. (Whether you want to use Depends or Imports in
>>> that case is related to what you intend for the user to see as I
>>> was saying in my last comment - if I expect users to use foo1 as
>>> well, I would use Depends, otherwise Imports is good enough.)

Or declare in Depends but also import in the NAMESPACE.

> So, if I'm only using a few functions of foo1, I shouldn't declare
> them in Depends, but only in Imports, which also will allow me to be
> sure that the functions come from foo1 and not from another package
> that have functions with the same, right ?

Yes, but it would be better to use importsFrom() with just the functions
you need (the list of exported functions from foo1 could change over time).

I've just been through this advising an author of a package who needs to
use intersect() from rgeos, not intersect() from base.


--
Brian D. Ripley,                  [hidden email]
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
diy
Reply | Threaded
Open this post in threaded view
|

alternative to .Fortran("dqrls", ...)

diy
Hi all,

In a package I maintain, I use the "dqrls" from the base package to fit
regression models. However, I was informed that directly calling functions
from the base package is no longer allowed:

"CRAN packages should use only the public API. Hence they should not use
entry points not declared as API in installed headers nor .Internal()
nor .Call() etc calls to base packages. Such usages can cause packages
to break at any time, even in patched versions of R."

Prof. Ripley suggested that one can replace such calls with lm.wfit, which
will work.

However, in my package, the call will need to be implemented hundreds of
thousands times. Using lm.wfit is much slower. I am wondering whether there
is alternative ways that are more efficient. I can program in C if needed.

Many thanks.

Best regards,
Yanming

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: alternative to .Fortran("dqrls", ...)

Prof Brian Ripley
On 07/11/2012 20:26, Yanming Di wrote:

> Hi all,
>
> In a package I maintain, I use the "dqrls" from the base package to fit
> regression models. However, I was informed that directly calling
> functions from the base package is no longer allowed:
>
> "CRAN packages should use only the public API. Hence they should not use
> entry points not declared as API in installed headers nor .Internal()
> nor .Call() etc calls to base packages. Such usages can cause packages
> to break at any time, even in patched versions of R."

And dqrls is no longer available via .Fortran in R-devel.

> Prof. Ripley suggested that one can replace such calls with lm.wfit,
> which will work.
>
> However, in my package, the call will need to be implemented hundreds of
> thousands times. Using lm.wfit is much slower. I am wondering whether
> there is alternative ways that are more efficient. I can program in C if
> needed.

If the licence of your package allows it, you can copy the Fortran code
into your package and write your own interface via .Fortran, .C or .Call.

The .Fortran interface is itself quite costly, especially in R < 2.15.1:
in some cases lm.wfit is actually faster as it uses .Call.

>
> Many thanks.
>
> Best regards,
> Yanming
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


--
Brian D. Ripley,                  [hidden email]
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Correct use of Depends, Imports and ::

Stephanie M. Gogarten
In reply to this post by Prof Brian Ripley


On 11/7/12 12:00 PM, Prof Brian Ripley wrote:

> On 07/11/2012 17:52, mauricio zambrano wrote:
>>
>>
>> On 11/07/12, Simon Urbanek  <[hidden email]> wrote:
>>
>>> On Nov 7, 2012, at 9:25 AM, Simon Urbanek wrote:
>>>
>>>> On Nov 7, 2012, at 4:04 AM, Mauricio Zambrano-Bigiarini wrote:
>>>>
>>>>> Dear R developers,
>>>>>
>>>>> Taking advantage of the yesterday discussion about the use of
>>>>> Depends/Import/Suggests/Enhances, I would like to add a related
>>>>> question.
>>>>>
>>>>> Let's assume, in the DESCRIPTION file of my package foo0, I
>>>>> have:
>>>>>
>>>>> Depends: foo1 Imports: foo2
>>>>>
>>>>> while in the NAMESPACE file of my package I have
>>>>>
>>>>> importFrom("foo2", f2)
>>>>>
>>>>>
>>>>> and within my package I use the following two external
>>>>> functions:
>>>>>
>>>>> f1, from package foo1 f2, from package foo2
>>>>>
>>>>>
>>>>> If I want to use the two previous functions within a function
>>>>> of my package foo0, is it correct to call them as follow (
>>>>> assuming 'x' is the only argument of 'f1' and 'f2'):
>>>>>
>>>>> f1(x) foo2::f2(x)
>>>>>
>>>>> OR should I use
>>>>>
>>>>> foo1::f1(x) f2(x)
>>>>>
>>>>> OR
>>>>>
>>>>> f1(x) f2(x)
>>>>>
>>>>
>>>> Yes, this one. You have imported f2 so the whole point of that is
>>>> that it will be inserted as "f2" into your imports environment,
>>>> so you want to use it that way. When you use foo2:: you are
>>>> skipping over imports and addressing the namespace of foo2
>>>> directly which sort of defeats the purpose (and is slower).
>>
>> Thanks Simon for making the point related to f2 clearer for me.
>>
>>>>
>>>> As for f1, you have attached the package so it is available. I
>>>> would not use foo1:: for the same reason as above, but there is a
>>>> risk that it may get clobbered since the call will go through the
>>>> search path so you may not get f1 from foo1 necessarily - this
>>>> may or may not be a good thing depending on your intentions. You
>>>> could import it via import(foo1) to make sure you don't need to
>>>> go through the search path - that's what I would do if I don't
>>>> intend overrides. (Whether you want to use Depends or Imports in
>>>> that case is related to what you intend for the user to see as I
>>>> was saying in my last comment - if I expect users to use foo1 as
>>>> well, I would use Depends, otherwise Imports is good enough.)
>
> Or declare in Depends but also import in the NAMESPACE.

This is especially a good idea if there is any chance that anyone else
will ever want to import your package.  If a package is in Depends but
the functions you use are not explicitly imported in the NAMESPACE (with
imports or importsFrom), it can cause problems as described in this thread:

https://stat.ethz.ch/pipermail/r-devel/2011-September/062043.html


>
>> So, if I'm only using a few functions of foo1, I shouldn't declare
>> them in Depends, but only in Imports, which also will allow me to be
>> sure that the functions come from foo1 and not from another package
>> that have functions with the same, right ?
>
> Yes, but it would be better to use importsFrom() with just the functions
> you need (the list of exported functions from foo1 could change over time).
>
> I've just been through this advising an author of a package who needs to
> use intersect() from rgeos, not intersect() from base.
>
>

Stephanie Gogarten
Research Scientist, Biostatistics
University of Washington

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Correct use of Depends, Imports and ::

Mauricio Zambrano-Bigiarini-2
In reply to this post by Prof Brian Ripley
2012/11/7 Prof Brian Ripley <[hidden email]>:

> On 07/11/2012 17:52, mauricio zambrano wrote:
>>
>>
>>
>> On 11/07/12, Simon Urbanek  <[hidden email]> wrote:
>>
>>> On Nov 7, 2012, at 9:25 AM, Simon Urbanek wrote:
>>>
>>>> On Nov 7, 2012, at 4:04 AM, Mauricio Zambrano-Bigiarini wrote:
>>>>
>>>>> Dear R developers,
>>>>>
>>>>> Taking advantage of the yesterday discussion about the use of
>>>>> Depends/Import/Suggests/Enhances, I would like to add a related
>>>>> question.
>>>>>
>>>>> Let's assume, in the DESCRIPTION file of my package foo0, I
>>>>> have:
>>>>>
>>>>> Depends: foo1 Imports: foo2
>>>>>
>>>>> while in the NAMESPACE file of my package I have
>>>>>
>>>>> importFrom("foo2", f2)
>>>>>
>>>>>
>>>>> and within my package I use the following two external
>>>>> functions:
>>>>>
>>>>> f1, from package foo1 f2, from package foo2
>>>>>
>>>>>
>>>>> If I want to use the two previous functions within a function
>>>>> of my package foo0, is it correct to call them as follow (
>>>>> assuming 'x' is the only argument of 'f1' and 'f2'):
>>>>>
>>>>> f1(x) foo2::f2(x)
>>>>>
>>>>> OR should I use
>>>>>
>>>>> foo1::f1(x) f2(x)
>>>>>
>>>>> OR
>>>>>
>>>>> f1(x) f2(x)
>>>>>
>>>>
>>>> Yes, this one. You have imported f2 so the whole point of that is
>>>> that it will be inserted as "f2" into your imports environment,
>>>> so you want to use it that way. When you use foo2:: you are
>>>> skipping over imports and addressing the namespace of foo2
>>>> directly which sort of defeats the purpose (and is slower).
>>
>>
>> Thanks Simon for making the point related to f2 clearer for me.
>>
>>>>
>>>> As for f1, you have attached the package so it is available. I
>>>> would not use foo1:: for the same reason as above, but there is a
>>>> risk that it may get clobbered since the call will go through the
>>>> search path so you may not get f1 from foo1 necessarily - this
>>>> may or may not be a good thing depending on your intentions. You
>>>> could import it via import(foo1) to make sure you don't need to
>>>> go through the search path - that's what I would do if I don't
>>>> intend overrides. (Whether you want to use Depends or Imports in
>>>> that case is related to what you intend for the user to see as I
>>>> was saying in my last comment - if I expect users to use foo1 as
>>>> well, I would use Depends, otherwise Imports is good enough.)
>
>
> Or declare in Depends but also import in the NAMESPACE.
>
>
>> So, if I'm only using a few functions of foo1, I shouldn't declare
>> them in Depends, but only in Imports, which also will allow me to be
>> sure that the functions come from foo1 and not from another package
>> that have functions with the same, right ?
>
>
> Yes, but it would be better to use importsFrom() with just the functions you
> need (the list of exported functions from foo1 could change over time).

That is what I wanted to say (although I was not explicit):
to declare the dependency of package foo1 in the Imports section of
the DESCRIPTION file, and then import the specific functions in the
NAMESPACE file by using importFrom("foo1", f11,...,f1n)

Thank you very much for this useful advise.


Mauricio Zambrano-Bigiarini
--
=====================================
Water Resources Unit
Institute for Environment and Sustainability
Joint Research Centre, European Commission
webinfo    : http://floods.jrc.ec.europa.eu/
=====================================
DISCLAIMER:\ "The views expressed are purely those of th...{{dropped:15}}

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Correct use of Depends, Imports and ::

Mauricio Zambrano-Bigiarini-2
In reply to this post by Stephanie M. Gogarten
2012/11/7 Stephanie M. Gogarten <[hidden email]>:

>
>
> On 11/7/12 12:00 PM, Prof Brian Ripley wrote:
>>
>> On 07/11/2012 17:52, mauricio zambrano wrote:
>>>
>>>
>>>
>>> On 11/07/12, Simon Urbanek  <[hidden email]> wrote:
>>>
>>>> On Nov 7, 2012, at 9:25 AM, Simon Urbanek wrote:
>>>>
>>>>> On Nov 7, 2012, at 4:04 AM, Mauricio Zambrano-Bigiarini wrote:
>>>>>
>>>>>> Dear R developers,
>>>>>>
>>>>>> Taking advantage of the yesterday discussion about the use of
>>>>>> Depends/Import/Suggests/Enhances, I would like to add a related
>>>>>> question.
>>>>>>
>>>>>> Let's assume, in the DESCRIPTION file of my package foo0, I
>>>>>> have:
>>>>>>
>>>>>> Depends: foo1 Imports: foo2
>>>>>>
>>>>>> while in the NAMESPACE file of my package I have
>>>>>>
>>>>>> importFrom("foo2", f2)
>>>>>>
>>>>>>
>>>>>> and within my package I use the following two external
>>>>>> functions:
>>>>>>
>>>>>> f1, from package foo1 f2, from package foo2
>>>>>>
>>>>>>
>>>>>> If I want to use the two previous functions within a function
>>>>>> of my package foo0, is it correct to call them as follow (
>>>>>> assuming 'x' is the only argument of 'f1' and 'f2'):
>>>>>>
>>>>>> f1(x) foo2::f2(x)
>>>>>>
>>>>>> OR should I use
>>>>>>
>>>>>> foo1::f1(x) f2(x)
>>>>>>
>>>>>> OR
>>>>>>
>>>>>> f1(x) f2(x)
>>>>>>
>>>>>
>>>>> Yes, this one. You have imported f2 so the whole point of that is
>>>>> that it will be inserted as "f2" into your imports environment,
>>>>> so you want to use it that way. When you use foo2:: you are
>>>>> skipping over imports and addressing the namespace of foo2
>>>>> directly which sort of defeats the purpose (and is slower).
>>>
>>>
>>> Thanks Simon for making the point related to f2 clearer for me.
>>>
>>>>>
>>>>> As for f1, you have attached the package so it is available. I
>>>>> would not use foo1:: for the same reason as above, but there is a
>>>>> risk that it may get clobbered since the call will go through the
>>>>> search path so you may not get f1 from foo1 necessarily - this
>>>>> may or may not be a good thing depending on your intentions. You
>>>>> could import it via import(foo1) to make sure you don't need to
>>>>> go through the search path - that's what I would do if I don't
>>>>> intend overrides. (Whether you want to use Depends or Imports in
>>>>> that case is related to what you intend for the user to see as I
>>>>> was saying in my last comment - if I expect users to use foo1 as
>>>>> well, I would use Depends, otherwise Imports is good enough.)
>>
>>
>> Or declare in Depends but also import in the NAMESPACE.
>
>
> This is especially a good idea if there is any chance that anyone else will
> ever want to import your package.  If a package is in Depends but the
> functions you use are not explicitly imported in the NAMESPACE (with imports
> or importsFrom), it can cause problems as described in this thread:
>
> https://stat.ethz.ch/pipermail/r-devel/2011-September/062043.html

So, no matter if the package providing the functions I'm using within
my package (e.g., foo1) are declared in the Depends or Imports section
of the DESCRIPTION file, it is always a "good practice" to specify all
the imported functions with import(foo1) or importFrom("foo1",
f11,...,f1n) in the NAMESPACE file of my package, in order to avoid
problems as the one described in the previous link.

Thanks Prof. Ripley and Stephanie for the advice,

All the best,

Mauricio
--
=====================================
Water Resources Unit
Institute for Environment and Sustainability
Joint Research Centre, European Commission
webinfo    : http://floods.jrc.ec.europa.eu/
=====================================
DISCLAIMER:\ "The views expressed are purely those of th...{{dropped:11}}

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel