I have a script that works fine when I run it manually in R Studio, but does not work when I run it from another program through a wrapper.
I get this info in my debug output:
[912] Error in xj[i] : only 0's may be mixed with negative subscripts 
[912] Calls: GetTopN -> cor -> is.data.frame -> [ -> [.data.frame 
If I save the image right before I get the error and then load it in R Studio I get the same error when I execute GetTopN(10). However, if I re-run the statement actionlist<- sqlQuery(channel,al_string) within R Studio and then execute GetTopN(10) everything works as it should.
I even tried to save the image within R Studio right before the critical call, and then load it through the wrapper before executing GetTopN(10) and I got the same error.
I checked and all of the relevant variables (crs,z,x,n) appear to have the proper values. I have no idea what could be the cause of this, and I'd really appreciate some help!
Here is what is being executed (in order):
#INIT:
library(RODBC)
library(stats)
channel<- odbcConnect("data")
crs<-mat.or.vec(3000,5) #will hold correlations
n1<-seq(-33,0)
#Get whole series
z <- sqlQuery(channel,"SELECT RPos,M1,M2,M3,M4 FROM `data`.`z` ")
al_string <- "SELECT RPos,OpenTime FROM z JOIN actionlist on(OpenTime = pTime)"
trim_string<- "DELETE FROM ActionList WHERE OpenTime NOT IN (SELECT OpenTime FROM ReducedList)"
GetTopN<-function(n)
{ 
  for(i in 1:nrow(actionlist))
  {
   crs[i,1]<-actionlist$OpenTime[i]
   for(j in 2:ncol(z)) 
   {
    crs[i,j]<-cor(z[actionlist$RPos[i]+n1,j],x[,j])
   }
  }
  avc <- (cbind(crs[,1],rowSums(crs[,2:5])))
  sorted <- crs[order(avc[,2], decreasing=T),1] 
  topx<- head(sorted,n)
  bottomx <- tail(sorted,n)
  DF<-as.data.frame(c(topx,bottomx),row.names=NULL) 
  colnames(DF)[1]<-'OpenTime'
  sqlSave(channel,dat=DF,tablename='ReducedList',append=F,rownames=F,safer=F) 
  sqlQuery(channel,trim_string)
}
curpTime <- 1275266400
actionlist<- sqlQuery(channel,al_string)
x<- sqlQuery(channel,paste('SELECT pTime,M1,M2,M3,M4 FROM z WHERE pTime <= ',curpTime,' AND 
pTime > ',curpTime,'-(300*34) ORDER BY pTime ASC'))
GetTopN(10)
I saved my workspace too if it might help (4.7mb): workspace
If connecting to my MYSQL database would help, it should be open on 74.73.17.163:3306
 
     
    