[R] An example of "overloading" [

Roger Bivand Roger.Bivand at nhh.no
Tue Mar 13 08:42:43 CET 2007


On Mon, 12 Mar 2007, Sender wrote:

> Any insight as to why this is printing twice?

Try:

m <- NA
class(m) <- "mydist"
m[]
res <- m[]
res

(hint - print.default() is returning what it was asked to print, which 
then gets printed again automatically)

> 
> "[.mydist" <- function(x,...){
>     print("I called my function")
> }
> 
> m <- a_mydist_obj
> m[]
> 
> I called my function
> I called my function
> 
> 
> 
> 
> On 3/12/07, Sender <exonintron at gmail.com> wrote:
> >
> > Super. Thanks for the leads.
> >
> > On 3/12/07, Marc Schwartz <marc_schwartz at comcast.net> wrote:
> > >
> > > On Mon, 2007-03-12 at 21:28 -0700, Sender wrote:
> > > > Hello:
> > > >
> > > > Could anyone point me to a nice example where someone has created
> > > methods
> > > > for "[" on a user defined Class?
> > > >
> > > > I looked at the package Matrix but that was a little daunting. I'm
> > > looking
> > > > for someone a little more introductory. I've tried to search the help
> > > > section and the web but its difficult since "[" isn't searchable.
> > > >
> > > > Thanks in advance!
> > > >
> > > > Greg
> > >
> > > You might want to look at this thread from last year:
> > >
> > >   http://finzi.psych.upenn.edu/R/Rhelp02a/archive/77057.html
> > >
> > >    http://finzi.psych.upenn.edu/R/Rhelp02a/archive/77060.html
> > >
> > >   http://finzi.psych.upenn.edu/R/Rhelp02a/archive/77059.html
> > >
> > >    http://finzi.psych.upenn.edu/R/Rhelp02a/archive/77102.html
> > >
> > >
> > > You can also review the existing methods defined for '[' in your current
> > > installation by using:
> > >
> > >   methods("[")
> > >
> > > It may be easiest to then review the code for a given method by using
> > > something like the following as an example:
> > >
> > >   getAnywhere("[.data.frame")
> > >
> > >
> > > BTW, for searching the R help files, REGEX's are used, so for "[", you
> > > would need:
> > >
> > >   help.search ("\\[")
> > >
> > > since '[' is a special character in regular expressions and you need to
> > > escape it to search on the literal character. In R, you need to double
> > > the '\' to be interpreted properly.
> > >
> > > HTH,
> > >
> > > Marc Schwartz
> > >
> > >
> > >
> >
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-help mailing list