0

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.

Nacre
  • 65
  • 2
  • 8
  • 1
    It would help if you made a more [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data that removes complexity not relevant to your question. This simple example works for me: `f<-function(x) {png(paste0("gear-",x$gear[1],".png")); plot(drat~qsec, x);dev.off();x}; results<-.ddply(mtcars, .(gear), f)` (files are produced in the working directory). This leads me to believe it is possible to open a device and write in a plyr call. – MrFlick Mar 09 '16 at 02:05
  • By changing the code above to do a plot(temp$RP) rather than image(temp$RP), this does indeed produce the .png file with the plot. However, plot() does not represent this data correctly, which is why I was using image(). So now it appears that the problem is that the plot produced from the image() function is not making it into the device, which is perplexing. I'm working on a getting a reproducible example up. Should be done shortly. – Nacre Mar 09 '16 at 02:23
  • 1
    Change it to `print(image(T2))`. The Matrix library (which you are apparently using) overrides the default base `image()` function to return a `trellis` object which needs to be explicitly `print()`ed – MrFlick Mar 09 '16 at 03:56

1 Answers1

1

This question was answered by MrFlick's comment:

Change it to print(image(T2)). The Matrix library (which you are apparently using) overrides the default base image() function to return a trellis object which needs to be explicitly print()ed.

tdy
  • 36,675
  • 19
  • 86
  • 83
Nacre
  • 65
  • 2
  • 8