[R] help with oop in R - class structure and syntex

Gabor Grothendieck ggrothendieck at gmail.com
Wed Feb 6 19:47:51 CET 2008


zoo has a NAMESPACE which restricts access to the
internals of the package except for explicitly exported items.
zoo exports plot.zoo as an S3 method so that the plot generic
can find it.  It is also possible to simply export a function
not specifically as an S3 method in which case it will be
visible more generally.  For example see seq.dates in
chron.  seq.dates is a seq method but can be called directly
too since it was exported directly.

# plot.zoo exported as S3 method
library(zoo)
z <- zoo(1:10)
plot(z) # plot.zoo has been exported as S3 method
zoo:::plot.zoo(z) # same output - not recommended
# plot.zoo(z) # won't work

# seq.dates exported
library(chron)
x <- chron(1:10)
seq(x[1], x[10])
seq.dates(x[1], x[10]) # direct access - same output

If an object is not exported at all then it cannot be accessed
(other than via ::: notation).

The other type of package is one with no NAMESPACE.
In that case everything in the package is visible.

On Feb 6, 2008 1:16 PM, tom soyer <tom.soyer at gmail.com> wrote:
> Gabor, maybe I am not understanding it right. I was thinking for example
> something like how a call to plot actually calls plot.zoo when zoo is
> loaded. And a specific call to plot.zoo would not work, etc. One would think
> that plot and plot.zoo are separate and that one could call the parent as
> well as the offsprings independently. But if this kind of setup is by
> design, than it's fine. Does that make sense?
>
>
> On 2/6/08, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
>
> > If you believe that certain changes intended only to affect the local
> > environment nevertheless affect the global environment please give a
> > code example.
> >
> > On Feb 6, 2008 12:11 PM, tom soyer <tom.soyer at gmail.com> wrote:
> > > Thanks Hardley. I see what you mean. You are right, I am not an expert
> in
> > > oop AND I don't really know how R oo works, so certainly I shouldn't be
> > > making any sweeping statement. I was just thinking about the issue of
> local
> > > vs. global, i.e. changes intended for the local environment shouldn't
> affect
> > > the global environment. That was all I meant.
> > >
> > >
> > > On 2/6/08, hadley wickham <h.wickham at gmail.com> wrote:
> > > >
> > > > On Feb 6, 2008 10:13 AM, tom soyer <tom.soyer at gmail.com> wrote:
> > > > > Thanks Gabor. I guess true oo encapsulation is not possible in R.
> > > >
> > > > Before making such a claim, I would encourage you to actually learn
> > > > what oo means.  A couple of good readings are:
> > > >
> > > > Structure and Interpretation of Computer Programs.
> > > > http://mitpress.mit.edu/sicp/
> > > >
> > > > Concepts, Techniques, and Models of Computer Programming.
> > > > http://www.info.ucl.ac.be/~pvr/book.html
> > > >
> > > > Java style (message passing) oo is not the entirety of oo-based
> > > > programming!
> > > >
> > > > Hadley
> > > >
> > > > --
> > > > http://had.co.nz/
> > > >
> > >
> > >
> > >
> > > --
> > > Tom
> > >
> > >        [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > >
> > > R-help at r-project.org 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.
> > >
> >
>
>
>
> --
> Tom



More information about the R-help mailing list