[R] Calling any method within default method!

Abby Spurdle @purd|e@@ @end|ng |rom gm@||@com
Fri May 31 03:10:44 CEST 2019


> For years I have a problem that I have avoided with the use of the switch
> function, but now I want to solve by following the good practices of
object
> orientation (OOP).
> My function was created to generate experiments according to some input
> parameters. Therefore, the first argument does not have a class defined as
> "data.frame", "matrix", etc., so that from the generic, specific methods
can
> be called.
> I made a simple example to show my problem.
> If anyone has any light I thank them immensely.

Welcome to the world of OOP.
Possibly my favorite topic.
:)

However, I don't understand your question.

> foo(a = 1)

This is not how method dispatch is designed to work.
Lets say you have a function to create a brazil (Brazil) object.
(Which sets or prepends "brazil" to the class attribute).
> brazil = function (...some.arguments...)
> {    x = ...some.thing...
>      class (x) = c ("brazil", class (x) )
>      ...some.other.constructor.code....
>      ...return.x...
> }

Then you would write:
> b = brazil ()
> foo (b, a=1) # not foo (a=1)

And the brazil method would be called for generic foo and object b.

Note that there are many variations of this approach.
And someone will probably correct me, if I don't also mention that R has
two main object oriented systems.
This one relates to S3, the other is S4 partly implemented in the methods
package.

	[[alternative HTML version deleted]]



More information about the R-help mailing list