Introduction
Parallel computing speeds up computations by using multiple cores. R provides several packages for this.
Parallel Package
library(parallel)
# Detect cores
detectCores()
# Apply in parallel
cl <- makeCluster(2)
parLapply(cl, data, function)
stopCluster(cl)
Future Package
library(future)
library(future.apply)
plan(multisession)
# Run in parallel
future_lapply(data, function)
Parallel Examples
# mclapply (Mac/Linux)
library(parallel)
result <- mclapply(data, function, mc.cores = 2)
Performance
# Benchmark
library(microbenchmark)
microbenchmark(
serial = lapply(x, fun),
parallel = parLapply(cl, x, fun)
)
Summary
Parallel computing speeds up heavy computations. Use it for large-scale data processing.