[Rd] [R] S4 Methods with large numbers of args using memory
Seth Falcon
sfalcon at fhcrc.org
Thu Jun 8 20:12:46 CEST 2006
[This is probably more appropriate for R-devel so I'm responding
there]
Joseph Wang <joe at gnacademy.org> writes:
> Thanks all to the people on the r-help list for the backtick help.
>
> I've run into another problem. With my SWIG C++ wrapper for R, I'm finding
> that S4 generic functions with large numbers of argument (such as 10 or 11)
> seem to be taking up a lot of memory especially when a lot of the arguments
> are set to missing.
>
> This happens because I'm generating S4 methods to wrapper C++ functions with
> optional arguments. Each wrapper can take several meg of heap causing my R
> program to quickly run out of heap memory.
>
> Any ideas as to what the problem is and any possible way of working
> around it?
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
More information about the R-devel
mailing list