[R-sig-Geo] cluster error with clusterR and calc

Robert J. Hijmans r.hijmans at gmail.com
Wed Nov 20 18:41:37 CET 2013


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:
> Hi,I have the piece of code below which returns this error: Error in
> clusterR(s, fun1, filename = f) : cluster error# Piece of code# x, y, z are
> rasters# f is a filenamefun1 <- function(x) {      calc(x, fun = function(x)
> {        10 * (x[2] - x[3]) / ENV.LAPSERATE + x[1]      })}s <- stack(x, y,
> z)beginCluster(nodes)  h <- clusterR(s, fun1, filename = f)endCluster()# End
> piece of codeYesterday I got the code to run without error by first
> detaching package "parallel" detach("package:parallel", unload = TRUE) [It
> seems that package "parallel" is masking functions from package "snow" which
> is used by clusterR]. Today I just can't detach "parallel" (Error in
> detach("package:parallel") : argument 'name' incorrect). This may be due to
> the fact that I updated a buch of packages yesterday.If you have any idea
> about what is causing this error, please let me know. I'll be very
> happy!Thanks,Guillaume
>
>
>
> --
> View this message in context: http://r-sig-geo.2731867.n2.nabble.com/cluster-error-with-clusterR-and-calc-tp7585148.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list