Skip to content
Prev 19804 / 29559 Next

cluster error with clusterR and calc

Guillaume,

You cannot use an unevaluated variable in the clusterR "fun" function
as such variables are not exported to the nodes. It works by replacing
ENV.LAPSERATE with a number.
Also, note my different (matrix) notation in the function for calc,
but that for cases like this, where you want to refer to specific
layers, the overlay function is more convenient.

Robert

library(raster)
x <- raster()
y = setValues(x, runif(ncell(x)))
z = setValues(x, runif(ncell(x)))
x = setValues(x, runif(ncell(x)))
f= 'test.tif'
s <- stack(x, y, z)

# note, I replaced ENV.LAPSERATE with a number
fun1 <- function(x) calc(x, fun = function(d){ 10 * (d[,2] - d[,3]) /
6 + d[,1] })
fun2 <- function(x) overlay(x, fun = function(d,e,f){ 10 * (e - f) / 6 + d })

beginCluster(4)
h1 <- clusterR(s, fun1)
h2 <- clusterR(s, fun2)
# or like this
h3 <- clusterR(s, overlay, args=list(fun = function(d,e,f){ 10 * (e -
f) / 6 + d }))
endCluster()

On Wed, Nov 20, 2013 at 8:40 AM, Guillaume Drolet
<droletguillaume at gmail.com> wrote: