[R] Rprof causing R to crash

Marian Talbert mtalbert at usgs.gov
Wed Dec 12 00:05:13 CET 2012


I'm trying to use Rprof() to identify bottlenecks and speed up a particullary
slow section of code which reads in a portion of a tif file and compares
each of the values to values of predictors used for model fitting.  I've
written up an example that anyone can run.  Generally temp would be a
section of a tif read into a data.frame and used later for other processing. 
The first portion which just records the time works in about 6 seconds the
second part causes RGui to immediately close with no error or warning.  Any
advice on how to get Rprof to work or how to speed up this code would be
greatly appreciated.  I'm using Windows 7 (which might be my problem) and R
version 2.15.0.  

CalcMESS<-function(tiff.entry,pred.vect){
              f<-sum(pred.vect<tiff.entry)/length(pred.vect)*100
              if(is.na(f)) return(NA)
              if(f==0)
return((tiff.entry-min(pred.vect))/(max(pred.vect)-min(pred.vect))*100)
              if(0<f & f<=50) return(2*f)
              if(50<=f & f<100) return(2*(100-f))
              if(f==100)
return((max(pred.vect)-tiff.entry)/(max(pred.vect)-min(pred.vect))*100)
              else return(NA)
}

train.dat <- data.frame(a=runif(200),b=runif(200),c=runif(200),d=runif(200))
temp <-
data.frame(a=runif(130000),b=runif(130000),c=runif(130000),d=runif(130000))
pred.rng<-temp
vnames.final.mod <- names(train.dat)
nvars.final <- length(vnames.final.mod)

start.time<-Sys.time()
             for(k in 1:nvars.final){
                       
pred.range<-train.dat[,match(vnames.final.mod[k],names(train.dat))]
                       
pred.rng[,k]<-mapply(CalcMESS,tiff.entry=temp[,match(vnames.final.mod[k],names(temp))],MoreArgs=list(pred.vect=pred.range))
                         }
Sys.time()-start.time     


Rprof("C:\\temp\\mapply.out")
             for(k in 1:nvars.final){
                       
pred.range<-train.dat[,match(vnames.final.mod[k],names(train.dat))]
                       
pred.rng[,k]<-mapply(CalcMESS,tiff.entry=temp[,match(vnames.final.mod[k],names(temp))],MoreArgs=list(pred.vect=pred.range))
                         } 
Rprof(NULL)



--
View this message in context: http://r.789695.n4.nabble.com/Rprof-causing-R-to-crash-tp4652846.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list