I don't have time to look into how the ts class works, but I am assuming
you want to overload the get and set functionalities of the dollar, the
single bracket and double bracket operators/functions?! If so, this can
be done as follows:
"$.MyClass" <- function(object, name) {
cat("The method $() of class MyClass was called with the
arguments:");
print(object);
print(name);
}
"$<-.MyClass" <- function(object, name, value) {
cat("The method $<-() of class MyClass was called with the
arguments:");
print(object);
print(name);
print(value);
# IMPORTANT: Remember to return the same object otherwise the changes
will
# not be stored (this far they have only been applied to a local copy
# of the object).
object;
}
Note that the name of the second argument should 'name' and the third
should be 'value'. The first one could be anything.
Given and object, say 'a', of class MyClass, e.g. data.class(a)
="MyClass", and you do 'print(a$myField)' this will call the
function
"$.MyClass"() with the arguments object=a and
name="myField". a$myField
<- 42 will call "$<-.MyClass"() with object=a,
name="myField" and
value=42.
To change "[[.MyClass"(object, name)" and
"[[<-.MyClass"(object, name,
value)" do the same thing. The single bracket operators/functions are a
little bit trickier since they can take any number of indices and
optional arguments. A simple matrix (two indices) could be done as
"[.MyClass"(object, i=NULL, j=NULL, drop=FALSE) {
...
}
and
"[<-.MyClass"(object, i=NULL, j=NULL, value) {
...
object; # Always return!
}
The value to the set/assignment function is always passed as the last
argument and it should be named 'value'. You can have any number of
arguments to these two functions, e.g. "[.MyClass"(object, i=NULL,
j=NULL, k=NULL, drop=FALSE) or even more general "[.MyClass"(object,
..., drop=FALSE).
Hope this helps a bit.
Henrik Bengtsson
Mathematical Statistics,
Lund University, Sweden
> -----Original Message-----
> From: r-help-admin at stat.math.ethz.ch
> [mailto:r-help-admin at stat.math.ethz.ch] On Behalf Of Pijus Virketis
> Sent: den 12 december 2002 11:55
> To: r-help at stat.math.ethz.ch
> Subject: [R] improving ts object
>
>
> Dear all,
>
> Currently, a ts object behaves like an array, and it would be
> very useful to have a similar object, which would behave like
> a data.frame, i.e. it could be indexed, named, etc. like a
> data.frame. What would be the most efficient way to construct
> such an object? I have tried to make one on my own following
> the directions of class design from "S Programming" (2000) as
> a combination of POSIXt vector and a data.frame, but I can't
> get it right. Any tips much appreciated ...
>
> Cheers,
>
> Pijus
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/> r-help
>
>