Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8755227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:48:35+00:00 2026-06-13T13:48:35+00:00

Possible Duplicate: S4 Classes: Multiple types per slot I am trying to make my

  • 0

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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:48:36+00:00Added an answer on June 13, 2026 at 1:48 pm

    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 that SOCKcluster contains cluster as 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, with

    setOldClass(c("SOCKcluster", "cluster"))
    A = setClass("A", representation(cluster="SOCKcluster"))
    

    and then

    > library(parallel)
    > a = A(cluster=makePSOCKcluster(2))
    > a
    An object of class "A"
    Slot "cluster":
    socket cluster with 2 nodes on host 'localhost'
    

    Trying to put an MPI cluster (requires snow and Rmpi) into your class fails

    > a = A(cluster=makeCluster(2, "MPI"))
        2 slaves are spawned successfully. 0 failed.
    Error in validObject(.Object) : 
      invalid class "A" object: 1: invalid object for slot "cluster" in class "A": got class "spawnedMPIcluster", should be or extend class "SOCKcluster"
    invalid class "A" object: 2: invalid object for slot "cluster" in class "A": got class "MPIcluster", should be or extend class "SOCKcluster"
    invalid class "A" object: 3: invalid object for slot "cluster" in class "A": got class "cluster", should be or extend class "SOCKcluster"
    

    Creating a class that supports cluster would require reading of ?setOldClass.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Multiple Inheritance in C# I have two classes Class A and Class
Possible Duplicate: extending from two classes I'm currently trying to make a simple Mod
Possible Duplicate: C++ Inherited template classes don't have access to the base class I'm
Possible Duplicate: Ternary Expression with Interfaces as a Base Class Classes: interface ISms {
Possible Duplicate: Removing multiple classes (jQuery) how can we remove multiple classes from one
Possible Duplicate: WHY an Anonymous class in Java can't implement multiple interfaces directly? Simply
Possible Duplicate: Dynamic proxy for concrete classes Once I create a InvocationHandler implementation, I
Possible Duplicate: Forward declaration of nested types/classes in C++ For simple cross-references of classes
Possible Duplicate: java class declaration <T> Sometimes I've seen that java classes are created
Possible Duplicate: Is it a bad practice to have multiple classes in the same

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.