On 2018-09-26 15:34, Duncan Murdoch wrote:
On 26/09/2018 4:16 PM, Spencer Graves wrote:
        Is there anything comparable to "with" for S4 objects?


EXAMPLE:


        A "Wave" object in the tuneR package has slots "left" and
"right", plus others.  I'd like to be able to do something like the
following:


library(tuneR)
x <- seq(0, 2*pi, length = 6)
all.equal(x, rev(x))
channel <- round(32000 * sin(440 * x))
Wobj <- Wave(left = channel, right=rev(channel))

with(Wobj, quantile(left-right))


        ** This last statement throws "Error ... object 'left' not found".


        Is there something comparable to "with" that can do this?

I don't know of anything that is "officially sanctioned".  A couple of ideas:

1.  Slots in S4 are stored in attributes.  So

  with(attributes(Wobj), quantile(left - right))

works.  BUT:  as far as I recall, this is an undocumented implementation detail, and you aren't supposed to count on it.

2.  You could write an as.list() method for the Wave class, then

  with(as.list(Wobj),

would work.  This may be the "right" way to do this.


      Thanks.  I'd prefer to have as.list.default convert every S4 object to a list.  And have with(S4_object, ...) interpret it equivalent to with(as.list(S4_object), ...).


      I think I'll do it other ways for the time being.


      Best Wishes,
      Spencer


Duncan Murdoch

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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