使用trycatch在r中写入错误

时间:2018-03-26 08:19:22

标签: r try-catch

我使用过trycatch,这样如果在执行r代码期间发生错误,它就不会中断。

我想在其中一个文件中写入错误,但不确定如何完成。

以下是使用的代码

library(forecast)
library(data.table) 
library(RODBC)


forecast_data <- data.frame(Project_ID=character(),
                            Period_End=character(),
                            Point_Forecast=character(),
                            Lower_Limit_95=character(),
                            Upper_Limit_95=character(),
                            stringsAsFactors=FALSE)  

Data <- read.csv("Data.csv", header=TRUE,na.strings=c("NULL",""))[ ,c('Project_ID', 'Period_End_Date', 'Overall_Backlog_Processing_Efficiency_Incident')]

result = tryCatch({


  if (nrow(Data) >= 1)
  { 
    backlog <- as.vector(Data$Overall_Backlog_Processing_Efficiency_Incident)

    i <- 1
    datalist = list()

    for (i in 1:8) {

      backlogts <- tbats(backlog)
      fc2 <- forecast(backlogts, h=1)
      fc2

      fc_2 <- as.data.frame(fc2)

      fc_2$i <- i  # maybe you want to keep track of which iteration produced it?
      datalist[[i]] <- fc_2 # add it to your list

      backlog <- append(backlog,round(fc_2[1,1], digits = 2))

      i <- i +1

    }

    forecast_data = do.call(rbind, datalist)
    forecast_data$`Point Forecast` <- round(forecast_data$`Point Forecast` , digits = 3)

    nextweekday <- function(date, wday) {
      date <- as.Date(date)
      diff <- wday - wday(date)
      if( diff < 0 )
        diff <- diff + 7
      return(date + diff)
    }

    a <- tail(Data$Period_End_Date, n=1)
    a <- as.Date(a, "%d-%b-%y")
    b <- tail(Data$Project_ID, n=1)

    Period_End_Date <- data.table(date=seq(as.Date(nextweekday(a,1)), by=7, length=8), key="date")
    forecast_data  = cbind(forecast_data, Period_End_Date)
    names(forecast_data)[names(forecast_data) == 'date'] <- 'Period_End'
    forecast_data$Period_End <- as.character(forecast_data$Period_End)
    forecast_data$Project_ID <- b
    forecast_data <- forecast_data[c(8,7,1,4,5)]
    names(forecast_data)[names(forecast_data) == 'Lo 95'] <- 'Lower_Limit_95'
    names(forecast_data)[names(forecast_data) == 'Hi 95'] <- 'Upper_Limit_95'
    names(forecast_data)[names(forecast_data) == 'Point Forecast'] <- 'Point_Forecast'
  }

},
warning = function(w) {},
error = function(e) {
  forecast_data <- data.frame(Project_ID=character(),
                              Period_End=character(),
                              Point_Forecast=character(),
                              Lower_Limit_95=character(),
                              Upper_Limit_95=character(),
                              stringsAsFactors=FALSE)
  print(paste("MY_ERROR:  ",e))
})

我尝试在错误=函数(e)下打印错误打印(粘贴(“MY_ERROR:”,错误)),但它无法正常工作

我有什么遗失的吗?请指教。

1 个答案:

答案 0 :(得分:0)

这有用吗

Minimum Android version: Android 5.0(API Level 21-Lollipop)
Target Android version: Android 6.0(API Level 23-Marshmallow)

Target Framework: Android 8.1(Oreo)

或者,使用foo <- function(x){ output <- tryCatch(x, error = function(e) e) ifelse(is(output, "error"), "error", "no error") } foo() # error foo(1) # no error

purrr