Skip to content

S4 dispatch for .DollarNames (utils)

8 messages · Romain Francois, Deepayan Sarkar, John Chambers +1 more

#
Hello,

I'm trying to make .DollarNames generic and implement a method for it in 
a package. .DollarNames is the function that is now called to get 
completion possibilities.

My R code looks like this:

setGeneric( ".DollarNames" )

setClass("track",
          representation(x="numeric", y="numeric"))
## A class extending the previous, adding one more slot
setClass("trackCurve",
     representation(smooth = "numeric"),
     contains = "track")

setMethod( ".DollarNames", signature( x = "track", pattern = "character" 
), function(x, pattern){
	grep( pattern, c("foo", "bar"), value = TRUE )	
} )


and the NAMESPACE :

import( utils )
exportMethods( .DollarNames )
exportClasses( track, trackCurve )


When I load the package, I can call .DollarNames explicitely :

 > require( foo )
 > x <- new( "trackCurve", x = 1:10, y = 1:10, smooth = 1:10 )
 > .DollarNames( x, "f" )
[1] "foo"

but completion does not work :

 > x$f<TAB>
x$


What do I miss ?

I've uploaded foo here : http://addictedtor.free.fr/misc/rcpp/foo_1.0.tar.gz

Romain
#
This seems to do the trick, but it does not feel right:

.onLoad <- function( libname, pkgname ){	
	utils <- asNamespace( "utils" )
	unlockBinding( ".DollarNames", utils )
	assignInNamespace( ".DollarNames", .DollarNames, utils )
	lockBinding( ".DollarNames", utils )
}

Any better idea ?

Romain

Le 29/05/10 13:21, Romain Francois a ?crit :

  
    
#
On Sat, May 29, 2010 at 4:21 AM, Romain Francois
<romain at r-enthusiasts.com> wrote:
I guess because
character(0)

so the S4 generic is not being seen within the utils namespace. I
don't know what the right fix is...

-Deepayan
#
Le 29/05/10 20:23, Deepayan Sarkar a ?crit :
yes. hence the hack I used when replying which is probably not a good 
idea, specially if two packages want it.
Perhaps something like the attached ?

defining a generic in methods and use this one when methods dispatch is on.

  
    
#
Dear all,

thank your for this discussion - I had been wondering how to get the completion 
with my S4 class, but it didn't bother me enough to dig into things.
My question here is: what is the dummy class needed for?

I have a few S3 methods for my S4 class (rbind.hyperSpec, cbind.hyperSpec, and 
now .DollarNames.hyperSpec). They seem to work just fine without first setting 
up a dummy S3 class that is then used in the contains of the S4 definition.
Any pitfalls with that?

Claudia
#
Claudia,

Your S3 methods will be selected for objects from "hyperSpec", but not 
for S4 classes that inherit from that class (that have 
contains="hyperSpec" in their definition).

The S3 method dispatch has been fixed to recognize S4 classes that 
inherit from an S3 class, "foo3" in my example.

The plan is to fix that for the next release, and have S3 dispatch 
behave for all S4 objects, not just those that inherit from an S3 
class.  Meanwhile, the dummy class is needed.
On 5/30/10 8:08 AM, Claudia Beleites wrote:
#
John,

Thank your for the explanation!

Claudia