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,