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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:09:32+00:00 2026-05-23T08:09:32+00:00

I am trying to speed up my code with a ‘foreach’ loop using the

  • 0

I am trying to speed up my code with a ‘foreach’ loop using the doSMP package.

Here is a simplified version of my issue: I am running a file called main.R

file: main.R:

require(doSMP)
dropbox_path = "/home/ruser/Dropbox"
workers <- startWorkers(4)
registerDoSMP(workers)
foreach(jj=1:4 ) %dopar% source("test.R")
stopWorkers(workers)

file: test.R:

message(dropbox_path)

This returns the following error:
“Error in source(“test.R”) :
task 1 failed – “object ‘dropbox_path’ not found”

If I modify main.R to be :

require(doSMP)
dropbox_path = "/home/ruser/Dropbox"
workers <- startWorkers(4)
registerDoSMP(workers)
foreach(jj=1:4 ) %dopar% message(dropbox_path)
stopWorkers(workers)

It then works very well.
It also used to work well with sequencial code (‘for’ instead of ‘foreach’).

So R child instances can access the dropbox_path variable, but not when it is parsed through the source function. I tried to play around with the source() function arguments ‘local’ and ‘chdir’ with no sucess.

Would you know a way for the code to work? I would like to keep using the source() function.

  • 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-05-23T08:09:33+00:00Added an answer on May 23, 2026 at 8:09 am

    I apologize in advance that my little workaround doesn’t use your tools, but here’s how I would do it. I use package snowfall because I can easily extend my apply functions to work on multiple cores. The code is not tested because all of my cores are currently occupied. Should work, though.

    tiny_script.R contents:

    date()

    R code to fire up multiple cores:

    library(snowfall)
    sfInit(parallel = TRUE, cpus = 4, type = "SOCK") #power up
    my.list <- vector("list", 10)
    
    sfLapply(x = my.list, fun = function(x) source("./Odpad/tiny_script.R")) 
    

    Running on a single core using lapply only:

    > lapply(X = my.list, FUN = function(x) source("./Odpad/tiny_script.R")) #notice the difference in argument names between `lapply` and `sfLapply`.
    [[1]]
    [[1]]$value
    [1] "Wed Jun 22 13:02:11 2011"
    
    [[1]]$visible
    [1] TRUE
    
    
    [[2]]
    [[2]]$value
    [1] "Wed Jun 22 13:02:11 2011"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing some code and trying to speed it up using SIMD intrinsics
i'm trying to cave-man-profile the speed of my code from code using the date
I'm trying to refactor some slow running code which writes XML using nested loops
I'm trying to use doSMP / foreach to parallelize some code in R. I
I'm trying to speed up my code and the bottleneck seems to be the
I was trying to speed up some code, and then I tried compiling a
I'm trying to speed up my drawing code. Instead of creating a graphics context,
I was trying to speed up multiple FTP downloads by using threaded FTP connections.
I am trying to speed up my Numpy code and decided that I wanted
I'm trying to improve the speed of some code I've written. I was wondering

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.