NAMESPACE Q: does import as exist?

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

NAMESPACE Q: does import as exist?

Seth Falcon-2
Is there a way to rename a function when importing it?  I want to say,
"import yourFunc from Foo as myFunc" in the NAMESPACE file.  

Does this exist and I've missed it?  If it doesn't exist, would others
think it useful (and possible)?


Best,

+ seth

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

Re: NAMESPACE Q: does import as exist?

Duncan Murdoch
On 2/7/2006 8:48 PM, Seth Falcon wrote:
> Is there a way to rename a function when importing it?  I want to say,
> "import yourFunc from Foo as myFunc" in the NAMESPACE file.  
>
> Does this exist and I've missed it?  If it doesn't exist, would others
> think it useful (and possible)?

I don't know if that exists, but importing it and then copying it should
work.  That is, in your namespace import foo, and export bar, and in one
of your R source files have

bar <- foo

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

Re: NAMESPACE Q: does import as exist?

MarkBravington
In reply to this post by Seth Falcon-2
> On 2/7/2006 8:48 PM, Seth Falcon wrote:
> > Is there a way to rename a function when importing it?  I
> want to say,
> > "import yourFunc from Foo as myFunc" in the NAMESPACE file.
>

I don't think Seth's facility exists yet, but it has occurred to me
previously that it would be useful when you need to import two different
functions with the same name from two different packages. (Rare but does
happen.) Duncan's solution won't get around that. So it would be nice to
be able to do this

ImportFromAs( 'pack1', 'funca', 'pack1.funca')
ImportFromAs( 'pack2', 'funca', 'different.name.altogether')

in a NAMESPACE file. Useful-- yes. Possible-- I don't know!

Mark Bravington
CSIRO Mathematical & Information Sciences
Marine Laboratory
Castray Esplanade
Hobart 7001
TAS

ph (+61) 3 6232 5118
fax (+61) 3 6232 5012
mob (+61) 438 315 623
 

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Duncan Murdoch
> Sent: Wednesday, 8 February 2006 1:11 PM
> To: Seth Falcon
> Cc: [hidden email]
> Subject: Re: [Rd] NAMESPACE Q: does import as exist?
>
> On 2/7/2006 8:48 PM, Seth Falcon wrote:
> > Is there a way to rename a function when importing it?  I
> want to say,
> > "import yourFunc from Foo as myFunc" in the NAMESPACE file.
> >
> > Does this exist and I've missed it?  If it doesn't exist,
> would others
> > think it useful (and possible)?
>
> I don't know if that exists, but importing it and then
> copying it should work.  That is, in your namespace import
> foo, and export bar, and in one of your R source files have
>
> bar <- foo
>
> ______________________________________________
> [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: NAMESPACE Q: does import as exist?

Seth Falcon-2
On  7 Feb 2006, [hidden email] wrote:

>> On 2/7/2006 8:48 PM, Seth Falcon wrote:
>>> Is there a way to rename a function when importing it?  I
>> want to say,
>>> "import yourFunc from Foo as myFunc" in the NAMESPACE file.
>>
>
> I don't think Seth's facility exists yet, but it has occurred to me
> previously that it would be useful when you need to import two
> different functions with the same name from two different
> packages. (Rare but does happen.) Duncan's solution won't get around
> that. So it would be nice to be able to do this
>
> ImportFromAs( 'pack1', 'funca', 'pack1.funca')
> ImportFromAs( 'pack2', 'funca', 'different.name.altogether')
>
> in a NAMESPACE file. Useful-- yes. Possible-- I don't know!

Yes, this is along the lines of what I was thinking.  

An unpleasant work around would be to create a translation package
that does something along the lines of Duncan M.'s suggestion,
importing, renaming, exporting.

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

Re: NAMESPACE Q: does import as exist?

Romain François
Le 08.02.2006 04:26, Seth Falcon a écrit :

>On  7 Feb 2006, [hidden email] wrote:
>
>  
>
>>>On 2/7/2006 8:48 PM, Seth Falcon wrote:
>>>      
>>>
>>>>Is there a way to rename a function when importing it?  I
>>>>        
>>>>
>>>want to say,
>>>      
>>>
>>>>"import yourFunc from Foo as myFunc" in the NAMESPACE file.
>>>>        
>>>>
>>I don't think Seth's facility exists yet, but it has occurred to me
>>previously that it would be useful when you need to import two
>>different functions with the same name from two different
>>packages. (Rare but does happen.) Duncan's solution won't get around
>>that. So it would be nice to be able to do this
>>
>>ImportFromAs( 'pack1', 'funca', 'pack1.funca')
>>ImportFromAs( 'pack2', 'funca', 'different.name.altogether')
>>
>>in a NAMESPACE file. Useful-- yes. Possible-- I don't know!
>>    
>>
>
>Yes, this is along the lines of what I was thinking.  
>
>An unpleasant work around would be to create a translation package
>that does something along the lines of Duncan M.'s suggestion,
>importing, renaming, exporting.
>  
>
What about :

bar <- pack::foo
kok <- otherpack::foo


Romain

--
visit the R Graph Gallery : http://addictedtor.free.fr/graphiques
mixmod 1.7 is released : http://www-math.univ-fcomte.fr/mixmod/index.php
+---------------------------------------------------------------+
| Romain FRANCOIS - http://francoisromain.free.fr               |
| Doctorant INRIA Futurs / EDF                                  |
+---------------------------------------------------------------+

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

Re: NAMESPACE Q: does import as exist?

Duncan Murdoch
In reply to this post by Duncan Murdoch
On 2/7/2006 9:10 PM, Duncan Murdoch wrote:

> On 2/7/2006 8:48 PM, Seth Falcon wrote:
>> Is there a way to rename a function when importing it?  I want to say,
>> "import yourFunc from Foo as myFunc" in the NAMESPACE file.  
>>
>> Does this exist and I've missed it?  If it doesn't exist, would others
>> think it useful (and possible)?
>
> I don't know if that exists, but importing it and then copying it should
> work.  That is, in your namespace import foo, and export bar, and in one
> of your R source files have
>
> bar <- foo

There may be a disadvantage to this if you have a saved image of the
function -- the current one will be saved, not the one that gets a bug
fixed next week.  Or maybe it's okay, I don't know.  I think a way
around that problem would be

delayedAssign("bar", foo)

which won't actually do the copying until the first use of bar.

As Romain said, if there are multiple foo functions around, just qualify
the name before importing.

Duncan Murdoch

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

Re: NAMESPACE Q: does import as exist?

Duncan Murdoch
In reply to this post by Seth Falcon-2
On 2/7/2006 10:26 PM, Seth Falcon wrote:

> On  7 Feb 2006, [hidden email] wrote:
>
>>> On 2/7/2006 8:48 PM, Seth Falcon wrote:
>>>> Is there a way to rename a function when importing it?  I
>>> want to say,
>>>> "import yourFunc from Foo as myFunc" in the NAMESPACE file.
>> I don't think Seth's facility exists yet, but it has occurred to me
>> previously that it would be useful when you need to import two
>> different functions with the same name from two different
>> packages. (Rare but does happen.) Duncan's solution won't get around
>> that. So it would be nice to be able to do this
>>
>> ImportFromAs( 'pack1', 'funca', 'pack1.funca')
>> ImportFromAs( 'pack2', 'funca', 'different.name.altogether')
>>
>> in a NAMESPACE file. Useful-- yes. Possible-- I don't know!
>
> Yes, this is along the lines of what I was thinking.  
>
> An unpleasant work around would be to create a translation package
> that does something along the lines of Duncan M.'s suggestion,
> importing, renaming, exporting.

Why do you call it unpleasant?  With the current mechanisms in R, that's
probably what your ImportFromAs function would have to do.  There's no
way to have two names referring to the same function.

Duncan Murdoch

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

Re: NAMESPACE Q: does import as exist?

Seth Falcon-2
In reply to this post by Romain François
On  8 Feb 2006, [hidden email] wrote:
> What about :
>
> bar <- pack::foo
> kok <- otherpack::foo

This is a solution, thanks for the suggestion.  The downside is that
it doesn't let one be particularly specific about what one is using in
the NAMESPACE file.  In fact, if one only wanted to use pack::foo and
otherpack::foo, then I believe one would have to _not_ mention them at
all in NAMESPACE.

+ seth

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

Re: NAMESPACE Q: does import as exist?

Seth Falcon-2
In reply to this post by Duncan Murdoch
On  8 Feb 2006, [hidden email] wrote:
>>> in a NAMESPACE file. Useful-- yes. Possible-- I don't know!
>> Yes, this is along the lines of what I was thinking.  An unpleasant
>> work around would be to create a translation package
>> that does something along the lines of Duncan M.'s suggestion,
>> importing, renaming, exporting.
>
> Why do you call it unpleasant?  

Because other languages do this without defining an extra "package".
I don't think users should have to go to that length to resolve such
name issues.

> With the current mechanisms in R, that's probably what your
> ImportFromAs function would have to do.  There's no way to have two
> names referring to the same function.

Do you mean: there's no way to have one name referring to two
different functions?  

After looking over some of the namespace code, I think what I'm asking
for is possible (without creating extra packages).  E.g., attaching a
package with a NAMESPACE file looks like it will call
attachNamespace(), which creates an empty env on the search path and
then calls importIntoEnv() to fill it.  ImportIntoEnv() ends up in
do_importIntoEnv in envir.c.  The comment there is encouraging:

    /* This function copies values of variables from one environment
       to another environment, possibly with different names.
       Promises are not forced and active bindings are preserved. */

Am I on track here that this implies that it is possible to have an
importFromAs directive without change to the current underlying
mechanism and that only higher level additions would be needed (not to
say it would be easy).

Best,

+ seth

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

Re: NAMESPACE Q: does import as exist?

Duncan Murdoch
On 2/8/2006 10:18 AM, Seth Falcon wrote:

> On  8 Feb 2006, [hidden email] wrote:
>>>> in a NAMESPACE file. Useful-- yes. Possible-- I don't know!
>>> Yes, this is along the lines of what I was thinking.  An unpleasant
>>> work around would be to create a translation package
>>> that does something along the lines of Duncan M.'s suggestion,
>>> importing, renaming, exporting.
>>
>> Why do you call it unpleasant?  
>
> Because other languages do this without defining an extra "package".
> I don't think users should have to go to that length to resolve such
> name issues.

I don't see a need for an additional package here -- it could all be
done in the package doing the importing.

>
>> With the current mechanisms in R, that's probably what your
>> ImportFromAs function would have to do.  There's no way to have two
>> names referring to the same function.
>
> Do you mean: there's no way to have one name referring to two
> different functions?  

No, but that's true too, if you're talking within an environment.  It's
certainly possible to use the same name in different environments to
refer to different things.

What I meant was that I can't define "foo" to refer to function "bar".
I can make a copy of "bar", but not a reference.

Duncan Murdoch

>
> After looking over some of the namespace code, I think what I'm asking
> for is possible (without creating extra packages).  E.g., attaching a
> package with a NAMESPACE file looks like it will call
> attachNamespace(), which creates an empty env on the search path and
> then calls importIntoEnv() to fill it.  ImportIntoEnv() ends up in
> do_importIntoEnv in envir.c.  The comment there is encouraging:
>
>     /* This function copies values of variables from one environment
>        to another environment, possibly with different names.
>        Promises are not forced and active bindings are preserved. */
>
> Am I on track here that this implies that it is possible to have an
> importFromAs directive without change to the current underlying
> mechanism and that only higher level additions would be needed (not to
> say it would be easy).
>
> Best,
>
> + seth
>
> ______________________________________________
> [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
|

How to change names when importing with a NAMESPACE (was: NAMESPACE Q: does import as exist?)

Seth Falcon-2
In reply to this post by Seth Falcon-2
Well, it seems that the following does what I want.  In the NAMESPACE
file, one can do:

  importFrom("pkgA", foo=bar)

Which will allow you to use the symbol 'foo' to refer to pkgA::bar.
This is exactly what I wanted.

Despite the lack of documentation, this seems an intentional feature.
Is there a reason not to document it and allow it as part of the
NAMESPACE feature set?

Best,

+ seth

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

Re: NAMESPACE Q: does import as exist?

Luke Tierney
In reply to this post by Seth Falcon-2
We anticipated this issue when the name space mechanism was designed,
and the design allows for this, but we deliberately did not formally
document this at the time.  As with most things like this there are
trade-offs.  Allowing for renaming complicates the code; it also may
prevent, or at least significantly complicate, changes in the
implementation we might want to make.  On the other hand this might be
quite useful in some settings.  We had some disagrements about how
useful, so the compromise was to leave the facility in but not
officially document it, and wait and see what the need looks like.
This is the first request I recall in almost three years since the
mechanism was introduced, so it is not a major issue.

The support for renaming on import allows you to use the directive

  importFrom(bar, hh = g)

to import bar::g as hh in your package.

Since this mechanism is not officially documented and has not been
used or tested, changes that have occurred in the code over time may
have broken things, or things may have been added that did not take
renaming into account.  For example, the parallel facility for
renaming on export seems to have been broken somewhere along the way.
So if you want to use this do so with caution.  If we see substantial
usage we may need to think about formally documenting and testing
this; but only after carefully considering whether it does impose
limits on other things we would like to do.

Some of the throughts on this are described in
http://www.stat.uiowa.edu/~luke/R/namespaces/morenames.html, also
available off the developer page developer.r-project.org.

Best,

luke




On Wed, 8 Feb 2006, Seth Falcon wrote:

> On  8 Feb 2006, [hidden email] wrote:
>>>> in a NAMESPACE file. Useful-- yes. Possible-- I don't know!
>>> Yes, this is along the lines of what I was thinking.  An unpleasant
>>> work around would be to create a translation package
>>> that does something along the lines of Duncan M.'s suggestion,
>>> importing, renaming, exporting.
>>
>> Why do you call it unpleasant?
>
> Because other languages do this without defining an extra "package".
> I don't think users should have to go to that length to resolve such
> name issues.
>
>> With the current mechanisms in R, that's probably what your
>> ImportFromAs function would have to do.  There's no way to have two
>> names referring to the same function.
>
> Do you mean: there's no way to have one name referring to two
> different functions?
>
> After looking over some of the namespace code, I think what I'm asking
> for is possible (without creating extra packages).  E.g., attaching a
> package with a NAMESPACE file looks like it will call
> attachNamespace(), which creates an empty env on the search path and
> then calls importIntoEnv() to fill it.  ImportIntoEnv() ends up in
> do_importIntoEnv in envir.c.  The comment there is encouraging:
>
>    /* This function copies values of variables from one environment
>       to another environment, possibly with different names.
>       Promises are not forced and active bindings are preserved. */
>
> Am I on track here that this implies that it is possible to have an
> importFromAs directive without change to the current underlying
> mechanism and that only higher level additions would be needed (not to
> say it would be easy).
>
> Best,
>
> + seth
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

--
Luke Tierney
Chair, 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-devel
Reply | Threaded
Open this post in threaded view
|

Re: How to change names when importing with a NAMESPACE (was: NAMESPACE Q: does import as exist?)

Prof Brian Ripley
In reply to this post by Seth Falcon-2
This is indeed described in

http://www.stat.uiowa.edu/~luke/R/namespaces/morenames.html

linked from developer.r-project.org.

with the comment

   The purpose of baz is to import some of the exports of foo and bar and
   re-export them, using renaming in one case: bar's export g is imported
   under the internal name hh, and the internal variable hh is exported
   under the name gg. [Renaming seems like a useful option, but it may
   turn out to create too many complications and need to be dropped.]

It's been there since 1.7.0 and not yet been dropped.  (I would also
suggest checking the R-News article on namespaces, which I have not done.
But checking the developer site is always worthwhile.)

It would be nice to have more end-user documentation on namespaces, and I
have suggested it in the past.  A project for you, perhaps?


On Wed, 8 Feb 2006, Seth Falcon wrote:

> Well, it seems that the following does what I want.  In the NAMESPACE
> file, one can do:
>
>  importFrom("pkgA", foo=bar)
>
> Which will allow you to use the symbol 'foo' to refer to pkgA::bar.
> This is exactly what I wanted.
>
> Despite the lack of documentation, this seems an intentional feature.
> Is there a reason not to document it and allow it as part of the
> NAMESPACE feature set?
>
> Best,
>
> + seth

--
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: NAMESPACE Q: does import as exist?

Seth Falcon-2
In reply to this post by Luke Tierney
Hi Luke,

Thanks very much for the explanation.

On  8 Feb 2006, [hidden email] wrote:
> Allowing for renaming complicates the code; it also may prevent, or
> at least significantly complicate, changes in the implementation we
> might want to make.

Makes sense that this feature is not of highest priority.  Renaming
will always be available as newFoo <- somePkg::foo.  The ability to do
the renaming in the NAMESPACE file has the added advantage of being
declarative.  That has value, but certainly one can get by without
it.  

Given that newFoo <- somePkg::foo will always work, I would think that
getting it to work in the NAMESPACE file shouldn't add too much
complexity, but I recognize this is a naive view.

> On the other hand this might be quite useful in some settings.  We
> had some disagrements about how useful, so the compromise was to
> leave the facility in but not officially document it, and wait and
> see what the need looks like.  This is the first request I recall in
> almost three years since the mechanism was introduced, so it is not
> a major issue.

My bias is obvious from this thread; I think this is a valuable
feature and I'm not sure the count of requests in the past 3 yrs is a
fair measure.  There is, perhaps, a feedback loop:

  more doc (plus beating with sticks)  => more use of namespaces
                                       => more feature requests

In the end, I would like to see more packages use namespaces and I
think this would be helped by more doc and examples.

> So if you want to use this do so with caution.  If we see
> substantial usage we may need to think about formally documenting
> and testing this; but only after carefully considering whether it
> does impose limits on other things we would like to do.

That is clear.  One last thought that I didn't see mentioned in the
namespace notes on your website:

Is there a way to say in the NAMESPACE file:

  I'm going to use pkgFoo, please load the package, but don't import
  anything because I will access via pkgFoo::

I realize that '::' will load pkgFoo on demand.  My reservation about
using :: is that it is easy to lose track of the fact that my package
needs pkgFoo.  I like the idea that all of my package's dependencies
are easily parsable by the system.

Having written that, I realize that the Imports field in DESCRIPTION
gives me a way to do this --- but in an ideal world I would need to
duplicate info from NAMESPACE in DESCRIPTION.

Best Wishes,

+ seth

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

Re: How to change names when importing with a NAMESPACE

Seth Falcon-2
In reply to this post by Prof Brian Ripley
On  8 Feb 2006, [hidden email] wrote:

> This is indeed described in
>
> http://www.stat.uiowa.edu/~luke/R/namespaces/morenames.html
>
> linked from developer.r-project.org.
>
> with the comment
>
> The purpose of baz is to import some of the exports of foo and bar
> and re-export them, using renaming in one case: bar's export g is
> imported under the internal name hh, and the internal variable hh is
> exported under the name gg. [Renaming seems like a useful option,
> but it may turn out to create too many complications and need to be
> dropped.]
>
> It's been there since 1.7.0 and not yet been dropped.

I'd like to interpret that as an endorsement that this feature is
likely to stay around.  Luke's comments indicates that at least in his
opinion the provisional nature is just as much provisional as now.

> It would be nice to have more end-user documentation on namespaces,
> and I have suggested it in the past.  A project for you, perhaps?

:-)

I will very likely write something up, at least as a summary of
lessons learned from this email exchange.  

+ seth

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