packagename:::functionname vs. importFrom

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

packagename:::functionname vs. importFrom

Frank Harrell
In my rms package I use the packagename:::functionname construct in a number of places.  If I instead use the importFrom declaration in the NAMESPACE file would that require the package to be available, and does it load the package when my package loads?  If so I would keep using packagename::: to avoid up-front loading of other packages that are not always used.

Thanks
Frank
Frank Harrell
Department of Biostatistics, Vanderbilt University
Reply | Threaded
Open this post in threaded view
|

Re: packagename:::functionname vs. importFrom

Hadley Wickham-2
Hi Frank,

I think you mean packagename::functionname?  The three colon form is
for accessing non-exported objects.  Otherwise, I think using :: vs
importFrom is functionally identical - either approach delays package
loading until necessary.

Hadley

On Mon, Jan 3, 2011 at 9:45 PM, Frank Harrell <[hidden email]> wrote:

>
> In my rms package I use the packagename:::functionname construct in a number
> of places.  If I instead use the importFrom declaration in the NAMESPACE
> file would that require the package to be available, and does it load the
> package when my package loads?  If so I would keep using packagename::: to
> avoid up-front loading of other packages that are not always used.
>
> Thanks
> Frank
>
>
> -----
> Frank Harrell
> Department of Biostatistics, Vanderbilt University
> --
> View this message in context: http://r.789695.n4.nabble.com/packagename-functionname-vs-importFrom-tp3172684p3172684.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: packagename:::functionname vs. importFrom

plangfelder
On Mon, Jan 3, 2011 at 3:48 PM, Hadley Wickham <[hidden email]> wrote:
> Hi Frank,
>
> I think you mean packagename::functionname?  The three colon form is
> for accessing non-exported objects.

Normally two colons suffice, but within a package you need three to
access exported but un-imported objects :)

Peter

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: packagename:::functionname vs. importFrom

Hadley Wickham-2
>> I think you mean packagename::functionname?  The three colon form is
>> for accessing non-exported objects.
>
> Normally two colons suffice, but within a package you need three to
> access exported but un-imported objects :)

Are you sure?

     Note that it is typically a design mistake to use ‘:::’ in your
     code since the corresponding object has probably been kept
     internal for a good reason.  Consider contacting the package
     maintainer if you feel the need to access the object for anything
     but mere inspection.

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: packagename:::functionname vs. importFrom

plangfelder
Well, I'm pretty sure that, inside package A, calling B::functionName
will not work if B has not been imported. That's why I use ::: (after
spending some time trying to figure out why :: didn't work). At least
that was the state of affairs as of R 2.9 or so, perhaps things have
changed since then.

Peter

On Mon, Jan 3, 2011 at 4:48 PM, Hadley Wickham <[hidden email]> wrote:

>>> I think you mean packagename::functionname?  The three colon form is
>>> for accessing non-exported objects.
>>
>> Normally two colons suffice, but within a package you need three to
>> access exported but un-imported objects :)
>
> Are you sure?
>
>     Note that it is typically a design mistake to use ‘:::’ in your
>     code since the corresponding object has probably been kept
>     internal for a good reason.  Consider contacting the package
>     maintainer if you feel the need to access the object for anything
>     but mere inspection.
>
> Hadley
>
> --
> Assistant Professor / Dobelman Family Junior Chair
> Department of Statistics / Rice University
> http://had.co.nz/
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: packagename:::functionname vs. importFrom

Frank Harrell
Correct.  I'm doing this because of non-exported functions in other packages, so I need :::

I'd still appreciate any insight about whether importFrom in NAMESPACE defers package loading so that if the package is not actually used (and is not installed) there will be no problem.

Thanks
Frank
Frank Harrell
Department of Biostatistics, Vanderbilt University
Reply | Threaded
Open this post in threaded view
|

Re: packagename:::functionname vs. importFrom

Tierney, Luke
On Mon, 3 Jan 2011, Frank Harrell wrote:

>
> Correct.  I'm doing this because of non-exported functions in other packages,
> so I need :::
>
> I'd still appreciate any insight about whether importFrom in NAMESPACE
> defers package loading so that if the package is not actually used (and is
> not installed) there will be no problem.

It does not -- the namespace from which you import is loaded when your
package is. (Also you can't import unexported variables.)

Best,

luke

>
> Thanks
> Frank
>
>
> -----
> Frank Harrell
> Department of Biostatistics, Vanderbilt University
>

--
Luke Tierney
Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:      [hidden email]
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: packagename:::functionname vs. importFrom

Hadley Wickham-2
In reply to this post by Frank Harrell
> Correct.  I'm doing this because of non-exported functions in other packages,
> so I need :::

But you really really shouldn't be doing that.  Is there a reason that
the package authors won't export the functions?

> I'd still appreciate any insight about whether importFrom in NAMESPACE
> defers package loading so that if the package is not actually used (and is
> not installed) there will be no problem.

Imported packages need to be installed - but it's the import vs.
suggests vs. depends statement in DESCRIPTION that controls this
behaviour, not the namespace.

Hadley


--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: packagename:::functionname vs. importFrom

Bill.Venables
If you use ::: to access non-exported functions, as Frank confesses he does, then you can't complain if in the next release of the package involved the non-exported objects are missing and things are being done another way entirely.  That's the deal.

On the other hand, sometimes package authors do not envisage all the ways their package will be used and neglecting to export some object is mostly because the author simply did not anticipate that anyone would ever need to use it. But sometimes they do.  A common case is when you need to do some operations very efficiently and there are simplifications in the input of which you can take advantage to cut down on the overheads.  In that case you usually need the cut-down (non-exported) workhorse rather than the (exported) show-pony front end.

The documentation suggests that if you ever need to use ::: perhaps you should be contacting the package maintainer to have the article in question exported.  This makes a lot of sense, but it can also creates quite a bit of work for the maintainers, too, if they agree to do it.

It's a very grey area, in my experience.

Bill Venables.

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Hadley Wickham
Sent: Tuesday, 4 January 2011 3:06 PM
To: Frank Harrell
Cc: [hidden email]
Subject: Re: [R] packagename:::functionname vs. importFrom

> Correct.  I'm doing this because of non-exported functions in other packages,
> so I need :::

But you really really shouldn't be doing that.  Is there a reason that
the package authors won't export the functions?

> I'd still appreciate any insight about whether importFrom in NAMESPACE
> defers package loading so that if the package is not actually used (and is
> not installed) there will be no problem.

Imported packages need to be installed - but it's the import vs.
suggests vs. depends statement in DESCRIPTION that controls this
behaviour, not the namespace.

Hadley


--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: packagename:::functionname vs. importFrom

Martin Morgan
In reply to this post by Hadley Wickham-2
On 01/03/2011 09:06 PM, Hadley Wickham wrote:
>> Correct.  I'm doing this because of non-exported functions in other packages,
>> so I need :::
>
> But you really really shouldn't be doing that.  Is there a reason that
> the package authors won't export the functions?
>
>> I'd still appreciate any insight about whether importFrom in NAMESPACE
>> defers package loading so that if the package is not actually used (and is
>> not installed) there will be no problem.

I think that with importFrom(packagename, functioname), the package will
fail to INSTALL with message "object 'functioname' is not exported by
namespace:packagename'".

If the function is exported from packagname, then R CMD check will
complain that 'Namespace dependency not required: packagename', which is
to say that Imports: packagename is needed in the DESCRIPTION file.
Packages that are listed in the Imports field of DESCRIPTION must be
available at install time, so this implies that the user has packagename
installed.

I think this is trying to corral you to good programming practice: use
Imports: packagename in the DESCRIPTION, use importsFrom(packagename,
functioname) in the NAMESPACE, and only use functions that are exported
from packagename.

Martin

>
> Imported packages need to be installed - but it's the import vs.
> suggests vs. depends statement in DESCRIPTION that controls this
> behaviour, not the namespace.
>
> Hadley
>
>


--
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: packagename:::functionname vs. importFrom

Frank Harrell
In reply to this post by Tierney, Luke
Thanks Luke.  By "the namespace from which you import is loaded when your
package is" I take it that you are saying that all such referenced packages are loaded up front, which is not what I hoped.  And it's too bad you can't import unexported objects, as that rather defeats the purpose of importFrom.

Frank
Frank Harrell
Department of Biostatistics, Vanderbilt University
Reply | Threaded
Open this post in threaded view
|

Re: packagename:::functionname vs. importFrom

Frank Harrell
In reply to this post by Martin Morgan
Thanks Hadley, Luke, Martin, and Bill.  Bill captured the essence of my reasons for needing an unexported function.  Other reasons include occasional overrides of dispatching rules, and providing parallel versions of some functions that exist in other packages to make them easier to use in some sense.  I could ask the package creator to export those functions but it would not make sense to him to do so.

Frank
Frank Harrell
Department of Biostatistics, Vanderbilt University
Reply | Threaded
Open this post in threaded view
|

Re: packagename:::functionname vs. importFrom

Tierney, Luke
In reply to this post by Frank Harrell
On Tue, 4 Jan 2011, Frank Harrell wrote:

>
> Thanks Luke.  By "the namespace from which you import is loaded when your
> package is" I take it that you are saying that all such referenced packages
> are loaded up front, which is not what I hoped.

That is what happens. You can use conditional imports in the NAMESPACE
but that isn't appropriate for all setting.  Using :: is reasonable if
you want to use a fuction only if its package is available.  Using :::
is code is a really bad idea for reasons already explained inthis
thread.

>  And it's too bad you can't
> import unexported objects, as that rather defeats the purpose of importFrom.

The purpose of importFrom is to avoid a full import.  So in no sense
does this "defeat the purpose of importFrom".

Best,

luke

>
> Frank
>
>
> -----
> Frank Harrell
> Department of Biostatistics, Vanderbilt University
>

--
Luke Tierney
Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:      [hidden email]
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: packagename:::functionname vs. importFrom

Frank Harrell
Thanks very much Luke for clarifying.
Frank
Frank Harrell
Department of Biostatistics, Vanderbilt University