## Evaluate futures in parallel
plan(multisession)
## Data
x <- rnorm(100)
y <- 2 * x + 0.2 + rnorm(100)
w <- 1 + x ^ 2
## EXAMPLE: Regular assignments (evaluated sequentially)
fitA <- lm(y ~ x, weights = w) ## with offset
fitB <- lm(y ~ x - 1, weights = w) ## without offset
fitC <- {
w <- 1 + abs(x) ## Different weights
lm(y ~ x, weights = w)
}
print(fitA)
print(fitB)
print(fitC)
## EXAMPLE: Future assignments (evaluated in parallel)
fitA %<-% lm(y ~ x, weights = w) ## with offset
fitB %<-% lm(y ~ x - 1, weights = w) ## without offset
fitC %<-% {
w <- 1 + abs(x)
lm(y ~ x, weights = w)
}
print(fitA)
print(fitB)
print(fitC)
## EXAMPLE: Explicitly create futures (evaluated in parallel)
## and retrieve their values
fA <- future( lm(y ~ x, weights = w) )
fB <- future( lm(y ~ x - 1, weights = w) )
fC <- future({
w <- 1 + abs(x)
lm(y ~ x, weights = w)
})
fitA <- value(fA)
fitB <- value(fB)
fitC <- value(fC)
print(fitA)
print(fitB)
print(fitC)
\dontshow{
## Make sure to "close" an multisession workers on Windows
plan(sequential)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.