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 8004245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:51:46+00:00 2026-06-04T16:51:46+00:00

I’m having a problem getting some code to work with the parallel package in

  • 0

I’m having a problem getting some code to work with the parallel package in R. I’m using R 2.15.

Here’s a simplified example… I have a file ‘animal.R’ which contains the following:

# animal.R
setClass("Animal", representation(species = "character", legs = "numeric"))

##Define some Animal methods
setGeneric("count",function(x) standardGeneric("count"))
setMethod("count", "Animal", function(x) { x@legs})

setGeneric("countAfterChopping",function(x) standardGeneric("countAfterChopping"))
setMethod("countAfterChopping", "Animal", function(x) { x@legs <- x@legs-1; x@legs})

Then, in my R terminal, I run:

library(parallel)
source('animal.R')

Start a local cluster of two nodes:

cl <- makeCluster(rep('localhost', 2))

Tell the cluster nodes about the Animal class:

clusterEvalQ(cl, parse('animal.R'))

Then run some code on the cluster:

# This works
parSapply(cl, list(daisy, fred), count)

# This doesn't...
parSapply(cl, list(daisy, fred), countAfterChopping)

Stop the cluster:

stopCluster(cl)

The first call to parSapply works as expected, but the second produces this error:

Error in checkForRemoteErrors(val) : 
  2 nodes produced errors; first error: "Animal" is not a defined class

Any ideas what’s going on? Why doesn’t the second call to parSapply work?

  • 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-04T16:51:47+00:00Added an answer on June 4, 2026 at 4:51 pm

    So here’s what’s going on:

    For S4 objects of class “Animal”, the count function simply extracts the legs slot. If this were all that you were doing, you wouldn’t need to evaluate or source the file animal.R on your cluster nodes. All necessary information would be passed by parSapply.

    However, the countAfterChopping function assigns a new value to the legs slot, and this is where the fun begins. The slot assignment function `@<-` contains a call to `slot<-` with the argument check = TRUE. This triggers an evaluation of the function checkSlotAssignment, which checks “that the value provided is allowed for this slot, by consulting the definition of the class” (from ?checkSlotAssignment).

    Therefore, the class definition must be known when assigning to a slot in this way, and the S4 class “Animal” is not known on the cluster nodes. This is why evaluating the parsed file animal.R or sourcing it works. However, you would be fine with just evaluating the first line of the file, i.e., defining the class “Animal”, on each node.

    Here’s a reduced, reproducible example:

    animal.R<-"
      setClass('Animal', representation(species = 'character', legs = 'numeric'))
    
      ##Define some Animal methods
      setGeneric('count',function(x) standardGeneric('count'))
      setMethod('count', signature(x='Animal'), function(x) { x@legs})
    
      setGeneric('countAfterChopping',function(x) standardGeneric('countAfterChopping'))
      setMethod('countAfterChopping', signature(x='Animal'),
        function(x) { x@legs <- x@legs-1; x@legs})
    "
    library(parallel)
    
    source(textConnection(animal.R))
    
    cl <- makeCluster(rep('localhost', 2))
    
    daisy<-new("Animal",legs=2,species="H.sapiens")
    fred<-new("Animal",legs=4,species="C.lupus")
    
    parSapply(cl, list(daisy, fred), count)
    # [1] 2 4
    
    clusterExport(cl,"animal.R") # 
    clusterEvalQ(cl,eval(parse(textConnection(animal.R),n=1)))
    
    parSapply(cl, list(daisy, fred), countAfterChopping)
    # [1] 1 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a reasonable size flat file database of text documents mostly saved in
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.