R中的函数和try() [英] Functions and try() in R

查看:130
本文介绍了R中的函数和try()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我使用:

  result<  -  try(sqlSave(ch,df,tablename =tblTest))

我可以使用:

  if(class(result)!=try-error)

以检查是否出现问题。没有问题。



但是,如果我使用尝试与一个功能结合使用,它不能像我预期的那样工作:

  result<  -  try(ch< -odbcConnect(TEST))

对于 result 和code的integer给出-1 class(result)



所以我应该使用

  ch< -odbcConnect(TEST)
如果(ch!= -1)

并使用 geterrmessage()作为错误消息?

解决方案

p>如果仔细阅读错误消息,您可以看到 odbcConnect 给您警告。错误是由ODBC驱动程序生成的,它不是错误在 try 意义( geterrmessage()将无法工作)



您可以使用 tryCatch 来处理此问题,例如:

  tryCatch(odbcConnect(TEST),warning = function(w)print(FAIL!))
pre>




更多解释:

-1是 odbcDriverConnect 函数。如果你看代码,那里有一行

  stat < -  .Call(C_RODBCDriverConnect,as.character(connection),
id,as.integer(believeNRows),as.logical(readOnlyOptimize))
if(stat <0L){
warning(ODBC connection failed)
return(stat )
}

所以你结束没有错误(并带有警告)和整数代码从C级。实际上,当连接成功时返回此代码(但是等于1)。 当没有错误时,结果类不能是 try-error

尝试和功能,但具体的这个特定功能( odbcDriverConnect )。



当然可以使用这种行为,例如

  ch<  -  odbcConnect(TEST)
如果(ch!= -1)

使用 tryCatch 你可以做

  tryCatch(ch< -odbcConnect(TEST),warning = function(w)print FAIL!))

创建 ch 变量当成功和打印消息失败。

  ch<  -  tryCatch(odbcConnect TEST),warning = function(w){print(FAIL!); return(NA)})

始终创建 ch 变量,但如果失败,则 NA 值。


Still struggling with R, especially with error handling:

If I use:

result <- try(sqlSave(ch,df,tablename="tblTest"))

I can use:

if (class(result) != "try-error")

to check if something went wrong. No problem.

But if I use try in combination with a function it doesn't work as I expected:

 result <- try(ch<-odbcConnect("TEST"))

gives "-1" for result and "integer" for class(result)

So should I use

ch<-odbcConnect("TEST")
if (ch != -1)

and use geterrmessage() for the error message?

解决方案

If you read closely error message you could see that odbcConnect gives you warning. Error is generated by ODBC drivers and it isn't error in try meaning (geterrmessage() won't work either).

You could use tryCatch to handle this, e.g.:

tryCatch(odbcConnect("TEST"), warning=function(w) print("FAIL!"))


Some more explanation:
-1 is a result of odbcDriverConnect function. If you look at the code there are lines

stat <- .Call(C_RODBCDriverConnect, as.character(connection), 
        id, as.integer(believeNRows), as.logical(readOnlyOptimize))
if (stat < 0L) {
     warning("ODBC connection failed")
     return(stat)
}

So you end without errors (and with a warning) and with integer code from C-level. Actually this code is returned when connection is succeed too (but then is equal 1). When there is no errors then result class can't be try-error.
It is not problem with try and functions but specific of this particular function (odbcDriverConnect).

You could of course use this behaviour as in your example

ch <- odbcConnect("TEST")
if (ch != -1)

With tryCatch you could do

tryCatch(ch<-odbcConnect("TEST"), warning=function(w) print("FAIL!"))

which creates ch variable when succeed and print message when failed.
Or

ch <- tryCatch(odbcConnect("TEST"), warning=function(w) {print("FAIL!");return(NA)})

which always creates ch variable but in case of failure there is NA value.

这篇关于R中的函数和try()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆