Thanks.  Used getGeneric("[") to figure out the general format for the
setMethod, but am having some problem with how to set up the actual
function:

> getGeneric("[")
standardGeneric for "[" defined from package "base"

function (x, i, j, ..., drop = TRUE)
standardGeneric("[", .Primitive("["))
<environment: 0x116513c30>
Methods may be defined for arguments: x, i, j, drop
Use  showMethods("[")  for currently available ones.

Based on this, I set up the following code:

setClass("A", representation(a="numeric", b="numeric"))
data = new("A", a=1:10, b=1:10)
setMethod("[", "A",
        function(x, i, j, ..., drop) {
            slotnames <- slotNames(x)[j]
            new_ = new("A")
            for(slot in slotnames) new_d...@slot = x...@slot[i]
            new_data
        })
data[5,c("a")]

The problem is that I cannot access S4 object slots using @ and a character
variable.  I also cannot access a slot using the typical brackets since that
is what I am trying to define here.  Kind of stuck.  Thanks for any advice
you might have.
Best,
Markus

On Mon, Feb 8, 2010 at 4:54 PM, Martin Morgan <mtmor...@fhcrc.org> wrote:

> On 02/08/2010 01:22 PM, Markus Weisner wrote:
> > Worked like a charm!!  Thank you so much.  I just plugged the following
> into
> > my code ...
> >
> > setMethod("$", "CADresponses", function(x, name) slot(x, name))
> >
> > ... and it worked perfect.  If you don't mind, I have a quick follow up
> > question, using your example
> >
> > setClass("A", representation(a="numeric", b="numeric"))
> > setMethod("$", "A", function(x, name) slot(x, name))
> > data = new("A", a=1:10, b=1:10)
> > data$a[5] #now works thanks to your code
> > data$a[5] <- 200 #assignments do not work -- any ideas?
>
> same idea, but for "$<-"
>
> > setClass("A", representation(a="numeric"))
> [1] "A"
> > getGeneric("$<-")
> standardGeneric for "$<-" defined from package "base"
>
> function (x, name, value)
> standardGeneric("$<-", .Primitive("$<-"))
> <environment: 0x14c33a8>
> Methods may be defined for arguments: x, value
> Use  showMethods("$<-")  for currently available ones.
> > setReplaceMethod("$", "A", function(x, name, value) {
> +     slot(x, name) <- value
> +     x
> + })
> [1] "$<-"
> > a <- new("A", a=1:10)
> > a$a <- 10:1
> > a
> An object of class "A"
> Slot "a":
>  [1] 10  9  8  7  6  5  4  3  2  1
>
> > data[5,c("a")] = 200 #would also like this to work -- any ideas?
> >
> > Do you have any suggestions for getting assignments and brackets to work
> as
> > they would for data frames?  Thanks so much for your help.
>
> same approach, but using getGeneric("[") and getGeneric("[<-") to guide
> you.
>
> Martin
>
> > Best,
> > Markus
> >
> >
> >
> > On Mon, Feb 8, 2010 at 2:44 PM, Martin Morgan <mtmor...@fhcrc.org>
> wrote:
> >
> >> On 02/07/2010 08:31 PM, Markus Weisner wrote:
> >>> I created some S4 objects that are essentially data frame objects.  The
> >> S4
> >>> object definitions were necessary to verify data integrity and force a
> >>> standardized data format.  I am, however, finding myself redefining all
> >> the
> >>> typical generic functions so that I can still manipulate my S4 objects
> as
> >> if
> >>> they were data frames ... I have used setMethod to set methods for
> >> "subset",
> >>> "head", and "tail".  I would like to use setMethod or setGeneric to
> >> enable
> >>> me to use object$slotname to access obj...@slotname for my S4 objects.
> >>  Any
> >>> advice is appreciated.  Thanks.
> >>
> >> Hi Markus --
> >>
> >>> setClass("A", representation(a="numeric"))
> >> [1] "A"
> >>> new("A")$a
> >> Error in new("A")$a : $ operator not defined for this S4 class
> >>> getGeneric("$")
> >> standardGeneric for "$" defined from package "base"
> >>
> >> function (x, name)
> >> standardGeneric("$", .Primitive("$"))
> >> <environment: 0xa62028>
> >> Methods may be defined for arguments: x
> >> Use  showMethods("$")  for currently available ones.
> >>> setMethod("$", "A", function(x, name) slot(x, name))
> >> [1] "$"
> >>> new("A", a=1:10)$a
> >>  [1]  1  2  3  4  5  6  7  8  9 10
> >>> new("A", a=1:10)$b
> >> Error in slot(x, name) : no slot of name "b" for this object of class
> "A"
> >>
> >> does that help?
> >>
> >> Martin
> >>
> >>> --Markus
> >>>
> >>>       [[alternative HTML version deleted]]
> >>>
> >>> ______________________________________________
> >>> R-help@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.
> >>
> >>
> >> --
> >> Martin Morgan
> >> Computational Biology / Fred Hutchinson Cancer Research Center
> >> 1100 Fairview Ave. N.
> >> PO Box 19024 Seattle, WA 98109
> >>
> >> Location: Arnold Building M1 B861
> >> Phone: (206) 667-2793
> >>
> >
>
>
> --
> Martin Morgan
> Computational Biology / Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N.
> PO Box 19024 Seattle, WA 98109
>
> Location: Arnold Building M1 B861
> Phone: (206) 667-2793
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to