###########################################################
# prepare a GDS file
# cteate a GDS file
f <- createfn.gds("test1.gds")
(n <- add.gdsn(f, "matrix", val=matrix(1:(10*6), nrow=10)))
read.gdsn(index.gdsn(f, "matrix"))
closefn.gds(f)
# cteate the GDS file "test2.gds"
(f <- createfn.gds("test2.gds"))
X <- matrix(1:50, nrow=10)
Y <- matrix((1:50)/100, nrow=10)
Z1 <- factor(c(rep(c("ABC", "DEF", "ETD"), 3), "TTT"))
Z2 <- c(TRUE, FALSE, TRUE, FALSE, TRUE)
node.X <- add.gdsn(f, "X", X)
node.Y <- add.gdsn(f, "Y", Y)
node.Z1 <- add.gdsn(f, "Z1", Z1)
node.Z2 <- add.gdsn(f, "Z2", Z2)
f
closefn.gds(f)
###########################################################
# apply in parallel
library(parallel)
# Use option cl.core to choose an appropriate cluster size.
cl <- makeCluster(getOption("cl.cores", 2L))
# Apply functions over rows or columns of matrix
clusterApply.gdsn(cl, "test1.gds", "matrix", margin=1, FUN=function(x) x)
clusterApply.gdsn(cl, "test1.gds", "matrix", margin=2, FUN=function(x) x)
clusterApply.gdsn(cl, "test1.gds", "matrix", margin=1,
selection = list(rep(c(TRUE, FALSE), 5), rep(c(TRUE, FALSE), 3)),
FUN=function(x) x)
clusterApply.gdsn(cl, "test1.gds", "matrix", margin=2,
selection = list(rep(c(TRUE, FALSE), 5), rep(c(TRUE, FALSE), 3)),
FUN=function(x) x)
# Apply functions over rows or columns of multiple data sets
clusterApply.gdsn(cl, "test2.gds", c("X", "Y", "Z1"), margin=c(1, 1, 1),
FUN=function(x) x)
# with variable names
clusterApply.gdsn(cl, "test2.gds", c(X="X", Y="Y", Z="Z2"), margin=c(2, 2, 1),
FUN=function(x) x)
# stop clusters
stopCluster(cl)
# delete the temporary file
unlink(c("test1.gds", "test2.gds"), force=TRUE)
Run the code above in your browser using DataLab