Skip to content

S4 Methods with large numbers of args using memory

1 message · Seth Falcon

#
[This is probably more appropriate for R-devel so I'm responding
there]

Joseph Wang <joe at gnacademy.org> writes:
I think the issue is related to the overhead of storing strings and
possibly language objects in R.  The memory used by a couple of large
setMethod calls is striking.

As for work arounds, I think you can make things better if it is the
case that you don't actually want to do dispatch on the possibly
missing arguments.  For example, if the signature is:

f(a, b, o1, o2, o3)

Where you want to dispatch on 'a' and 'b' and where o1, ..., o3 are
optional.  In that case you can

* Use a parameter object.  For the example above, you'd have slots o1,
  ..., o3 and the signature would become f(a, b, params).  Default
  values can be specified in the prototype for the param object class
  definition.

* Put args after '...'.  So you would have something like:

    setGeneric("f", function(a, b, ..., o1=1, o2=2, o3=3) standardGeneric("f"))

  Partial matching won't work for these args if you do this, but it
  can be a nice way to provide optional named args common to all
  methods of a generic function, but for which you do not ever want to
  do dispatch.

+ seth