R中的函数和try() [英] Functions and try() in R
问题描述
如果我使用:
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" forclass(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 intry
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 ofodbcDriverConnect
function. If you look at the code there are linesstat <- .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 withtry
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 dotryCatch(ch<-odbcConnect("TEST"), warning=function(w) print("FAIL!"))
which creates
ch
variable when succeed and print message when failed.
Orch <- tryCatch(odbcConnect("TEST"), warning=function(w) {print("FAIL!");return(NA)})
which always creates
ch
variable but in case of failure there isNA
value.这篇关于R中的函数和try()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!