```
if (FALSE) {
# set up the cluster object for parallel computing
beginCluster()
r <- raster()
values(r) <- 1:ncell(r)
x <- clusterR(r, sqrt, verbose=T)
f1 <- function(x) calc(x, sqrt)
y <- clusterR(r, f1)
s <- stack(r, r*2, r*3)
f2 <- function(d,e,f) (d + e) / (f * param)
param <- 122
ov <- clusterR(s, overlay, args=list(fun=f2), export='param')
pts <- matrix(c(0,0, 45,45), ncol=2, byrow=T)
d <- clusterR(r, distanceFromPoints, args=list(xy=pts))
values(r) <- runif(ncell(r))
m <- c(0, 0.25, 1, 0.25, 0.5, 2, 0.5, 1, 3)
m <- matrix(m, ncol=3, byrow=TRUE)
rc1 <- clusterR(r, reclassify, args=list(rcl=m, right=FALSE),
filename=rasterTmpFile(), datatype='INT2S', overwrite=TRUE)
# equivalent to:
rc2 <- reclassify(r, rcl=m, right=FALSE, filename=rasterTmpFile(), datatype='INT2S', overwrite=TRUE)
# example with the calc function
a <- 10
f3 <- function(x) sum(x)+a
z1 <- clusterR(s, calc, args=list(fun=f3), export='a')
# for some raster functions that use another function as an argument
# you can write your own parallel function instead of using clusterR
# get cluster object created with beginCluster
cl <- getCluster()
library(parallel)
clusterExport(cl, "a")
z2 <- calc(s, fun=function(x){ parApply(cl, x, 1, f3)} )
# set flag that cluster is available again
returnCluster()
#
# done with cluster object
endCluster()
}
```

Run the code above in your browser using DataCamp Workspace