r trycatch()没有运行错误函数

时间:2015-02-12 10:42:59

标签: r function error-handling

以为我设法正确地实现了R的trycatch,但是尝试在我的代码中构建更多功能已经让我感到沮丧......任何帮助都值得赞赏!

背景

我开始做一些工作,我们有一些传感器数据来自某些设备。我们已经确定了一个“有趣”发生的时间,而且我正在编写一个小帮助函数来帮助我筛选所有通道,寻找那些当时做“有趣”的东西。最终我想自动检测'有趣',但现在只是有人在ggplot中观察它。

工作起点:

以下代码效果很好。代码试图从数据库中获取数据,如果不能(因为通道尚未在数据库中激活),它只是循环到下一个通道。如果成功则将其打印在ggplot中,并且用户可以注视它并按“Enter”传递它,输入“1”将其突出显示为“有趣的频道”,或输入“x”以停止并返回到目前为止有趣的频道。

getDataPlot <- function(site_code,loc_id,from,to,resolution,connection,table,title="loc_id"){

  #Retrieve data from the Cassandra database
  dataframe <- getData(site_code,loc_id,from,to,resolution,connection,table)

  #Set chart title
  if(title=="loc_id"){
    title <- loc_id
  }

  #plot results for user to look at
  print(ggplot(dataframe, aes(x=gmt_event, y=data_value)) + geom_line() + ggtitle(title))
}



runThroughChannels <- function(channelList,site_code,from,to,resolution,connection,table){
  #Set up blank dataframe to capture 'interesting' channels
  interestingChannels <- data.frame(NAME=c(),MEAS_LOC_ID=c(),UNITS=c())

  #loop through the channels
  for(i in 1:nrow(channelList)){
    loc_id <- toString(channelList$MEAS_LOC_ID[i])
    title <- paste(channelList$NAME[i]," (Chnl ",i," of ",nrow(channelList),")",sep="")

    #Try and plot the data, if fails, set inputline to 'E' as a flag to skip on
    tryCatch(
{
  getDataPlot(site_code,loc_id,from,to,resolution,connection,table,title)
  inputline <- readline() #Waits for user input once chart is plotted
},
error=function(e){
  inputline <- "E"
})

#Handle user input
if(inputline=="x"){
  return(interestingChannels)
}else if(inputline=="1"){
  #Add this channel to the list of interesting ones
  interestingChannels <- rbind(interestingChannels,channelList[i,])
}
  }
return(interestingChannels)
}

当我为数据库中尚未激活的通道运行getDataPlot可能是相关的,我的trycatch(显然)成功捕获的错误看起来像这样(这是预期的):

> getDataPlot("0000000000443337","23","2013-12-04 11:00:00","2013-12-04 13:00:00","minute",casscon,"events_by_location")

 Error in seq.POSIXt(startTime, collectedData[nrow(collectedData), ]$gmt_event,  : 
  'to' must be of length 1 In addition: Warning message:
All formats failed to parse. No formats found. 

不起作用的位

很多频道只是零,在寻找“有趣”的东西时,我不需要打扰用户。因此,如果有一个标志来启用该行为,我希望getDataPlot在值全为零时以及当它无法获取数据时抛出错误,所以我尝试了:

getDataPlot <- function(site_code,loc_id,from,to,resolution,connection,table,title="loc_id",error_if_zero=FALSE){

  #Retrieve data from the Cassandra database
  dataframe <- getData(site_code,loc_id,from,to,resolution,connection,table)

  #Set chart title
  if(title=="loc_id"){
    title <- loc_id
  }

  #New section - throw an error if zeros only so that we skip on to the next channel
  if(error_if_zero){
    if(sum(abs(dataframe$data_value))==0){
      stop("Zeros only")
    }
  }

  #plot results for user to look at
  print(ggplot(dataframe, aes(x=gmt_event, y=data_value)) + geom_line() + ggtitle(title))
}



runThroughChannels <- function(channelList,site_code,from,to,resolution,connection,table){
  #Set up blank dataframe to capture 'interesting' channels
  interestingChannels <- data.frame(NAME=c(),MEAS_LOC_ID=c(),UNITS=c())

  #loop through the channels
  for(i in 1:nrow(channelList)){
    loc_id <- toString(channelList$MEAS_LOC_ID[i])
    title <- paste(channelList$NAME[i]," (Chnl ",i," of ",nrow(channelList),")",sep="")

    #Try and plot the data, if fails, set inputline to 'E' as a flag to skip on
    tryCatch(
      {
        getDataPlot(site_code,loc_id,from,to,resolution,connection,table,title,TRUE)
        inputline <- readline() #Waits for user input once chart is plotted
      },
      error=function(e){
        inputline <- "E"
      })

    #Handle user input
    if(inputline=="x"){
      return(interestingChannels)
    }else if(inputline=="1"){
      #Add this channel to the list of interesting ones
      interestingChannels <- rbind(interestingChannels,channelList[i,])
    }
  }
  return(interestingChannels)
}

但我得到Error: object 'inputline' not found并通过调试运行它看起来getDataPlot行似乎在tryCatch中运行,但是当它停止时它只是跳到if(inputline=="x")

我尝试了什么

相当令人愤怒的是,在if(!exists(inputline)){inputline <- "E"}之前放置一个hacky if(inputline=="x")Error in exists(inputline) : object 'inputline' not found会产生{{1}}。当然,这个错误中的讽刺幽默并没有丢失在我身上,但它确实提示了一个相当快速的转向StackOverflow !!!

0 个答案:

没有答案