###################################################
### chunk number 1: 
###################################################
#line 35 "vignettes/MLInterfaces/inst/doc/xvalComputerClusters.Rnw"
options(width=60)


###################################################
### chunk number 2: init
###################################################
#line 55 "vignettes/MLInterfaces/inst/doc/xvalComputerClusters.Rnw"
library(MLInterfaces)
library(golubEsets)
smallG <- Golub_Merge[200:250,]


###################################################
### chunk number 3: xval
###################################################
#line 63 "vignettes/MLInterfaces/inst/doc/xvalComputerClusters.Rnw"
lk1 <- MLearn(ALL.AML~., smallG, knnI(k=1), xvalSpec("LOO"))
confuMat(lk1)


###################################################
### chunk number 4: clusterinit eval=FALSE
###################################################
## #line 78 "vignettes/MLInterfaces/inst/doc/xvalComputerClusters.Rnw"
## library(MLInterfaces)
## library(golubEsets)
## smallG <- Golub_Merge[200:250,]


###################################################
### chunk number 5: clusterstart eval=FALSE
###################################################
## #line 84 "vignettes/MLInterfaces/inst/doc/xvalComputerClusters.Rnw"
## library(snow)
## cl <- makeCluster(8, "MPI")
## clusterEvalQ(cl, library(MLInterfaces))


###################################################
### chunk number 6: clustercalc eval=FALSE
###################################################
## #line 91 "vignettes/MLInterfaces/inst/doc/xvalComputerClusters.Rnw"
## lk1 <- xval(smallG, "ALL.AML", knnB, xvalMethod="LOO", 
##             group=as.integer(0), cluster = cl)
## table(lk1,smallG$ALL.AML)


###################################################
### chunk number 7:  eval=FALSE
###################################################
## #line 113 "vignettes/MLInterfaces/inst/doc/xvalComputerClusters.Rnw"
## setMethod("xvalLoop", 
##           signature( cluster = "ANY" ),
##           function( cluster, ... ) lapply )


###################################################
### chunk number 8:  eval=FALSE
###################################################
## #line 133 "vignettes/MLInterfaces/inst/doc/xvalComputerClusters.Rnw"
## setOldClass( "spawnedMPIcluster" )
## setMethod("xvalLoop",
##           signature( cluster = "spawnedMPIcluster" ),
##           function( cluster, ... ) parLapply )


###################################################
### chunk number 9:  eval=FALSE
###################################################
## #line 151 "vignettes/MLInterfaces/inst/doc/xvalComputerClusters.Rnw"
## setOldClass( "spawnedMPIcluster" )
## setMethod("xvalLoop",
## signature( cluster = "spawnedMPIcluster" ),
## function( cluster, ... ) {
##     relapply <- function(X, FUN, ...) {
##         parLapply( cluster, X, FUN, ... )
##     }
##     relapply
## })


###################################################
### chunk number 10:  eval=FALSE
###################################################
## #line 179 "vignettes/MLInterfaces/inst/doc/xvalComputerClusters.Rnw"
## setMethod("xvalLoop", signature( cluster = "spawnedMPIcluster"),
## function( cluster, ... ) {
##     clusterExportEnv <- function (cl, env = .GlobalEnv)
##     {
##         unpackEnv <- function(env) {
##             for ( name in ls(env) ) assign(name, get(name, env), .GlobalEnv )
##             NULL
##         }
##         clusterCall(cl, unpackEnv, env)
##     }
##     relapply <- function(X, FUN, ...) {
##         ## send all visible variables from the parent (i.e., xval) frame
##         clusterExportEnv( cluster, parent.frame(1) )
##         parLapply( cluster, X, FUN, ... )
##     }
##     relapply
## })


###################################################
### chunk number 11: 
###################################################
#line 208 "vignettes/MLInterfaces/inst/doc/xvalComputerClusters.Rnw"
res <- c(20.04,11.34, 9.23, 8.20, 7.87)
plot(res,ylab="Time (seconds)", xlab="Nodes", ylim=c(0,max(res)))


###################################################
### chunk number 12:  eval=FALSE
###################################################
## #line 221 "vignettes/MLInterfaces/inst/doc/xvalComputerClusters.Rnw"
## harness <- function( nodes, reps, data ) {
##     if ( nodes > 1) {
##         cl <- makeCluster(nodes, "MPI")
##         clusterEvalQ(cl, library(MLInterfaces))
##     } else cl <- NULL
##     func <- function(x)
##         res <- xval(data, "ALL.AML", knnB, xvalMethod = "LOO", 0:0, cluster = cl )
##     func()                              # warm-up
##     tm <- system.time( sapply( 1:reps, func ) )[3]
##     if (nodes > 1) stopCluster(cl)
##     tm
## }
## res <- sapply( 1:5, harness, 10, smallG )


###################################################
### chunk number 13: 
###################################################
#line 279 "vignettes/MLInterfaces/inst/doc/xvalComputerClusters.Rnw"
x <- 1:10
for (i in 1:10) x[i] <- i**2
x # x has been modified by the for loop

x <- 1:10
res <- lapply(1:10, function(i) x[i] <- i**2)
x # copy of x in lapply modified, not the global x