tryCatch中的错误消息范围

时间:2015-11-16 15:33:13

标签: r

有人可以解释为什么在打印错误消息时,下面的错误消息代码无法访问变量col1col2吗?

#######ONLY NEEDED TO REPRODUCE MY ERROR
library(matrixcalc)
library(ppcor)
exampledf<- matrix(ceiling(runif(16,0,50)), ncol=4)
while(is.singular.matrix(exampledf)!=TRUE){
  exampledf<- matrix(ceiling(runif(16,0,50)), ncol=4)
}
colnames(exampledf)<-c("col1","col2","col3","col4")

x<-as.data.frame(exampledf)
x<-x[complete.cases(x),] #semi-partial func below requires complete cases
spcor.test(x$col1,x$col2,x$col3)$estimate #test that one value works
cont<-"col3"
cols<-cbind(colnames(x))
cols<-cols[-c(grep(cont,cols))]
################################################################ THE IMPORTANT BIT BELOW
apply(expand.grid(cols, cols), 1, FUN= function(col1,col2) {
  tryCatch( #errors caused by matrix not singular in most cases
    {
      spcor.test(x[col1],x[col2],x[,c(cont)])$estimate
    },
    error=function(cond){
      message(paste('error:',cond,"cols: ",col1,col2))
      return(NA)}
  )
})

确切的错误消息:

 Error in paste("error:", cond, "cols: ", col1, col2) : 
  argument "col2" is missing, with no default 

0 个答案:

没有答案
相关问题