[R-pkg-devel] Initialize Method
Georgi Boshnakov
georgi.boshnakov at manchester.ac.uk
Sun Jan 8 20:39:08 CET 2017
Hi,
The short answer is that initialize() dispatches only on its first argument but the required effect can be achieved in a number of ways:
(1) Write a method for initialize() and put suitable checks at its start, e.g. if(!is(data1, "numeric"), stop()) .
Sometimes you may wish to say ' data1 <- as(data1, "numeric")' for greater flexibility. Note that if you simply assign the argument to a slot, R will raise an error if the value is not suitable anyway. Package "methods" provides a lot of power here, see the documentation as to how to use callNextMethod(), for example, if your needs are complicated.
(2) Declare your constructor as a generic function and write as many methods as you need. Each of these methods will do some preparatory work and call new("TestClass",...) to create the objects. Your constructor may have any name, although a user-friendly convention is for it to have the name of the class. Note that in your example:
setMethod("initialize", "TestClass", ... })
"TestClass" is the name of the class, not the function TestClass() that you created.
You can do both: write a method for initialize() which is invoked whenever new("TestClass", ...) is called and also write a constructor TestClass() for users' convenience. In my experience it is best to keep methods for initialize() relatively simple and do "heavy lifting" in a separate constructor.
Note that in any case it is a good idea to take care that your method for initialize() does not invalidate calls to new() without further arguments, e.g. new("TestClass").
Cheers,
Georgi
-----Original Message-----
From: R-package-devel [mailto:r-package-devel-bounces at r-project.org] On Behalf Of Glenn Schultz
Sent: 08 January 2017 18:32
To: R Package Development
Subject: [R-pkg-devel] Initialize Method
All,
My package is S4 an I would like to strongly type the input to the constructor function. I understand from reading help etc that the call to new should be in a function and the method initialize is only used when one would like to customize the call to new or when the inputs are different from the slots.
I cannot seem to get the method initialize to work. Below is a minimal example. My class works, my getter works, my constructor function works but I cannot get the initialize to strongly type the inputs. Any help to understand this would be greatly appreciated
Best,
Glenn
setClass("TestClass",
slots = c(
FirstNum = "numeric",
SeconNum = "numeric",
))
setGeneric("FirstNum", function(object)
standardGeneric("FirstNum"))
setMethod("FirstNum", signature("TestClass"),
function(object){object at FirstNum})
TestClass <- function(data1, data2, data3){ FirstNum = data1 * data2 SeconNum = data1/data2 * data3 new("TestClass", FirstNum = FirstNum, SeconNum = SeconNum) }
setMethod("initialize", "TestClass",
"Can I use initialize to type the inputs data1, data2, and data3.
If the function TestClass does heavy lifting can I use TestClass in the method initialize ? If so, how?"
})
______________________________________________
R-package-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel
More information about the R-package-devel
mailing list