I'm using ddply to do a computationally intense analysis dozens of times on a large data frame. The data frame consists of sequenced data (row 1 = trial 1, row 2 = trial 2, etc.) for each subject and there are several blocks of trials per subject. The analysis returns several numerical measures which I bind and return, but it also returns a sparse matrix represenation of the data, which I want to plot into a file.
data <- read.table("data.tsv", header= TRUE)
fqra <- function(x) {
temp <- crqa(as.numeric(x$to.roi), as.numeric(x$to.roi), delay = 1, embed = 1,
rescale = 0, radius = .001, normalize=0,mindiagline = 2, minvertline = 2,
tw=0, whiteline = FALSE, recpt=FALSE, side="upper",
checkl = list(do = FALSE, thrshd = 3, datatype = "categorical",
pad = FALSE))
directory = "~/TS14data/Plots/"
a = paste(directory, as.character(x$sid[1]), as.character(x$game.num[1]), ".png",sep="_")
png(filename = a)
b<- image(temp$RP)
dev.off()
return(cbind(temp$RR, temp$DET, temp$NRLINE, temp$maxL,
temp$L, temp$ENTR, temp$rENTR, temp$LAM, temp$TT))
}
results <- ddply(Eyedatdata, .(subject, block), fqra)
The code runs with no errors, and gives me my results by subject and block with no problems, but the plots are nowhere to be found. Is this a problem with how plyr operates? Can I not open a device within a function within ddply?
EDIT : A simpler example reproduces this problem. I have a sparse matrix and I want to creat an image of it and place it in a file.
library(matrix)
f<-function(x) {
T2 <- new("dgTMatrix", i = as.integer(c(1,1,0,3,3)), j = as.integer(c(2,2,4,0,0)), x=10*1:5, Dim=4:5)
png("example.png");
image(T2)
dev.off()
}
results<-ddply(mtcars, .(gear), f)
Removing everything inside the function and running it independently produces the "example.png" plot. What appears to be happening is that image(T2) does not output to the file from within a ddply call. I have no idea why this would happen.