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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:37:16+00:00 2026-05-23T00:37:16+00:00

I have a data.frame of cells, values and coordinates. It resides in the global

  • 0

I have a data.frame of cells, values and coordinates. It resides in the global environment.

> head(cont.values)
   cell value   x   y
1 11117    NA -34 322
2 11118    NA -30 322
3 11119    NA -26 322
4 11120    NA -22 322
5 11121    NA -18 322
6 11122    NA -14 322

Because my custom function takes almost a second to calculate individual cell (and I have tens of thousands of cells to calculate) I don’t want to duplicate calculations for cells that already have a value. My following solution tries to avoid that. Each cell can be calculated independently, screaming for parallel execution.

What my function actually does is check if there’s a value for a specified cell number and if it’s NA, it calculates it and inserts it in place of NA.

I can run my magic function (result is value for a corresponding cell) using apply family of functions and from within apply, I can read and write cont.values without a problem (it’s in global environment).

Now, I want to run this in parallel (using snowfall) and I’m unable to read or write from/to this variable from individual core.

Question: What solution would be able to read/write from/to a dynamic variable residing in global environment from within worker (core) when executing a function in parallel. Is there a better approach of doing this?

  • 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-23T00:37:17+00:00Added an answer on May 23, 2026 at 12:37 am

    The pattern of a central store that workers consult for values is implemented in the rredis package on CRAN. The idea is that the Redis server maintains a store of key-value pairs (your global data frame, re-implemented). Workers query the server to see if the value has been calculated (redisGet) and if not do the calculation and store it (redisSet) so that other workers can re-use it. Workers can be R scripts, so it’s easy to expand the work force. It’s a very nice alternative parallel paradigm. Here’s an example that uses the notion of ‘memoizing’ each result. We have a function that is slow (sleeps for a second)

    fun <- function(x) { Sys.sleep(1); x }
    

    We write a ‘memoizer’ that returns a variant of fun that first checks to see if the value for x has already been calculated, and if so uses that

    memoize <-
        function(FUN)
    {
        force(FUN) # circumvent lazy evaluation
        require(rredis)
        redisConnect()
        function(x)
        {
            key <- as.character(x)
            val <- redisGet(key)
            if (is.null(val)) {
                val <- FUN(x)
                redisSet(key, val)
            }
            val
        }
    }
    

    We then memoize our function

    funmem <- memoize(fun)
    

    and go

    > system.time(res <- funmem(10)); res
       user  system elapsed 
      0.003   0.000   1.082 
    [1] 10
    > system.time(res <- funmem(10)); res
       user  system elapsed 
      0.001   0.001   0.040 
    [1] 10
    

    This does require a redis server running outside R but very easy to install; see the documentation that comes with the rredis package.

    A within-R parallel version might be

    library(snow)
    cl <- makeCluster(c("localhost","localhost"), type = "SOCK")
    clusterEvalQ(cl, { require(rredis); redisConnect() })
    tasks <- sample(1:5, 100, TRUE)
    system.time(res <- parSapply(cl, tasks, funmem))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a data.frame where some cells have missing values. Instead
I have data frame with some numerical variables and some categorical factor variables. The
I have a data frame with start dates and end dates, along with the
I have a data frame like below (20,000 rows by 49 cols). Each row
I have a data frame with two qualitative variables (Q1, Q2) which are both
I have a data frame that is some 35,000 rows, by 7 columns. it
I have a data frame consisting of results from multiple runs of an experiment,
I have a data frame that's about ts1[100, 2000] in dimension as follows: >
I have a data frame of daily data: df with four columns: Date, A,
I have a data.frame of a time series of data, I would like to

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.