Re: [Rd] Correct use of Depends, Imports and ::

From: Mauricio Zambrano-Bigiarini <hzambran.newsgroups_at_gmail.com>
Date: Thu, 08 Nov 2012 00:10:58 +0100

2012/11/7 Prof Brian Ripley <ripley_at_stats.ox.ac.uk>:

> On 07/11/2012 17:52, mauricio zambrano wrote:

>>
>>
>>
>> On 11/07/12, Simon Urbanek <simon.urbanek_at_r-project.org> 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}}

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Wed 07 Nov 2012 - 23:16:43 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 08 Nov 2012 - 20:30:53 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive