Skip to content

[R-pkg-devel] Force namespace prefix for a loaded package function

7 messages · Duncan Murdoch, Tim Keitt

#
http://www.keittlab.org/
On Mon, Jun 27, 2016 at 3:22 AM, Joris Meys <jorismeys at gmail.com> wrote:

            
I think you missed the point (and stated the obvious).

A well-designed namespace feature would give control of imports to the code
user, not the code writer.

Right now, I have to avoid all the function names in base because I will
cause a collision. If I want to have an "options" function in my package, I
have to make it "pkgname_options" rather than pkgname::options, which is
greatly preferable and would allow the user to decide whether they want to
import it and then simply use "options" and "base::options".

I've always considered this all-or-nothing approach to imports a bug in the
implementation of namespaces in R. I was trying to suggest that it be
fixed. (Probably should have sent this to r-devel actually.)

THK

  
  
#
On 27/06/2016 11:08 AM, Tim Keitt wrote:
The base package is special, but for all other packages there's no 
"all-or-nothing" approach to imports, so your statement about a function 
named "options" doesn't really make sense.  If you want to do that, just 
do it, and other packages that prefer your implementation to the base 
one can import just that one function, or do the import at run time by 
calling it as pkgname::options().

Duncan Murdoch
#
http://www.keittlab.org/

On Mon, Jun 27, 2016 at 10:19 AM, Duncan Murdoch <murdoch.duncan at gmail.com>
wrote:
I know that. I mean for someone writing a script, not a package.

Its all good for package writers. Its quite simple to control imports
there. But not so much for someone using the package in R to write a
script. You either go with package_name::object for everything or you call
"library" and you get everything the packager exported.

It would be nice to 1) be able to hold back some functions from being fully
exported in a package and (maybe or) 2) extend the functionality of the
NAMESPACE file to the user session via a set of functions.

Does that make any more sense?

THK

  
  
#
http://www.keittlab.org/
On Mon, Jun 27, 2016 at 4:46 PM, Tim Keitt <tkeitt at utexas.edu> wrote:

            
Actually, now I see that those functions are available to the user in base
(although discouraged). I'll have to study them a bit.

THK

  
  
#
On 27/06/2016 5:46 PM, Tim Keitt wrote:
It makes a little more sense, but it's still not correct.  If you want 
to do the equivalent of importing foo::options, just add the line

options <- foo::options

at the start of your script.  This "imports" that one function, and 
nothing else from the foo namespace.

It has the side effect of leaving the options object in the current 
workspace afterwards.  If that concerns you, use local():

local( {
   options <- foo::options
   # Lots of calculations, computing result
   result
})

Duncan Murdoch
#
http://www.keittlab.org/

On Mon, Jun 27, 2016 at 5:18 PM, Duncan Murdoch <murdoch.duncan at gmail.com>
wrote:
Good one. Yes, that is more of what I had in mind.

I suppose I really want C++-like namespaces because I tend to think that
way.

THK

  
  
#
http://www.keittlab.org/
On Mon, Jun 27, 2016 at 7:04 PM, Tim Keitt <tkeitt at utexas.edu> wrote:

            
Here's a prototype package implementing some ideas:
https://github.com/thk686/using

THK