Possible Duplicate:
S4 Classes: Multiple types per slot
I am trying to make my first R package.
I plan to create a S4 class “test” that contains data and some methods to process data.
In my case, the processing of data can be improved by multi-threading.
I have tested parLapply() and it increases performance.
The problem is that I do not want to call:
cl <- makeCluster(N)
parLapply(cl, x, FUN, ...)
stopCluster(cl)
in each method I want to make parallel. This is because it is not elegant and, I presume, the repeated creation (and destruction) of the teams of thread costs.
Therefore, I was thinking about (simply) having a cluster object within my class “test”.
Then I could, for instance, make a “test” object “o” and call a method of “test” setNumbrOfThreads(o) <- 4.
However, I have trouble with the implementation. Since ?makeCluster() states the return value is ”’An object of class ‘c(“SOCKcluster”, “cluster”)”’, I have tried :
setClass("test",
representation(
data = "list",
nThreads = "numeric",
cluster = c("SOCKcluster", "cluster") #This seems incorrect
),
prototype(
data = NULL,
nThreads = 1,
cluster = makeCluster(1) # "cluster = NULL" does not help
)
)
R complained that element 3 of the representation was not a single character string.
So I tried without more success: cluster = "cluster" or cluster = "SOCKcluster" (in representation).
My question is:
How can I can I create an S4 class with a member object of class c(“SOCKcluster”, “cluster”) ?
Thank you,
Brandon asked about putting different types of objects (‘apple’, ‘orange’) into a single slot; you’re asking about using an S3 class in an S4 object. The notation
c("SOCKcluster", "cluster")is S3’s way of saying thatSOCKclustercontainsclusteras a parent class. This is a duplicate of Example of Using an S3 Class in a S4 Object, not of S4 Classes: Multiple types per slot, and with a twist — you have a (short) S3 class hierarchy, not just a single S3 class. The solution is in the same spirit, though, withand then
Trying to put an MPI cluster (requires
snowandRmpi) into your class failsCreating a class that supports
clusterwould require reading of?setOldClass.