mcparallelDo

Share:

Description

This function creates a fork, sets the variable named targetValue in the targetEnvironment to NULL, evaluates a segment of code evaluated in the fork, and the result of the fork returned in a variable named targetValue in the targetEnvironment after the next top-level command completes. If there is an error in the code, the returned variable will be a try-error. These effects are accomplished via the automatic creation and destruction of a taskCallback and other functions inside the mcparallelDoManager. If job results have to be collected before you return to the top level, use mcparallelDoCheck.

%mdpDo% Is an alternate form of calling the function, as if it were an assignment operator. See examples.

Usage

1
2
3
4
mcparallelDo(code, targetValue, verbose = TRUE,
  targetEnvironment = .GlobalEnv)

targetValue %mcpDo% code

Arguments

code

The code to evaluate within a fork wrapped in

targetValue

A character element indicating the variable that the result of that job should be assigned to targetEnvironment

verbose

A boolean element; if TRUE the completion of the fork expr will be accompanied by a message

targetEnvironment

The environment in which you want targetValue to be created

Value

If verbose is set to TRUE, then the character variable name of the job. This can be manually collected via mccollect or, if on Windows, an empty string. If verbose is set to FALSE, then NULL.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
## Create data
data(ToothGrowth)
## Trigger mcparallelDo to perform analysis on a fork
mcparallelDo({glm(len ~ supp * dose, data=ToothGrowth)},"interactionPredictorModel")
## Do other things
binaryPredictorModel <- glm(len ~ supp, data=ToothGrowth)
gaussianPredictorModel <- glm(len ~ dose, data=ToothGrowth)
## The result from mcparallelDo returns in your targetEnvironment, 
## e.g. .GlobalEnv, when it is complete with a message (by default)
summary(interactionPredictorModel)

# Example of not returning a value until we return to the top level
for (i in 1:10) {
  if (i == 1) {
    mcparallelDo({2+2}, targetValue = "output")
  }
  if (exists("output")) print(i)
}

# Example of getting a value without returning to the top level
for (i in 1:10) {
  if (i == 1) {
    mcparallelDo({2+2}, targetValue = "output")
  }
  mcparallelDoCheck()
  if (exists("output")) print(i)
}

# Example of dispatching as assignment
targetValueWithoutQuotes %mcpDo% sample(LETTERS, 10)