Soldalma
Soldalma

Reputation: 4758

Why does the future package does not find a function?

I am implementing parallelization using function parLapply from package parallel. This package requires exporting objects using the function clusterExport(). After watching a video about the package future I thought I could avoid the export step by using it. It has not worked, as the example below will demonstrate.

Here is the code:

library("future")

source("C:\\Dropbox\\Code\\R\\TestFuncs.R")

plan(sequential)
plan(multisession, workers=4)
bas <- function(x) {sapply(1:10, bas2(x))}

bar <- function(y) {bas(y)}

foo <- function(x) {bar(x)}

test <- lapply(1:100, function(x) future({ foo(x) }))
testvals <- lapply(test, value)

The sourced file TestFuncs.R contains the following code:

bas2 <- function(y) { y+1 }

When I try to run the program above I get the following message in RStudio:

> testvals <- lapply(test, value)
Error in match.fun(FUN) : 
    'bas2(x)' is not a function, character or symbol

Called from: signalConditions(future, exclude = getOption("future.relay.immediate", 
    "immediateCondition"), resignal = TRUE)

Browse[1]> 

I checked that the function bas2 is in the global enviroment. I also checked that the same problem happens in an RGui session, with a simpler error message.

Why is this happening and how do I deal with this issue?

Any help will be greatly appreciated.

Upvotes: 1

Views: 482

Answers (1)

MrFlick
MrFlick

Reputation: 206197

This doesn't seem have anything to do with future. YOur bas function doesn't seem to work. In it's current state

bas <- function(x) {sapply(1:10, bas2(x))}

Running it on its own gives the same error

bas(1)
#  Error in match.fun(FUN) : 
#   'bas2(x)' is not a function, character or symbol 

The second parameter to sapply needs to be a function, not an expression. It should look like

bas <- function(x) {sapply(1:10, function(y) bas2(x))}

(Note I used y in the inner function because I wasn't sure what x you wanted to use when calling bas2).

Upvotes: 3

Related Questions