R 如果返回值为零,则跳过脚本(我的意思是如果数据帧有空行)
我想使用Rpostgresql从数据库中收集一些数据,并为其制作条形图。这是剧本R 如果返回值为零,则跳过脚本(我的意思是如果数据帧有空行),r,ggplot2,rpostgresql,R,Ggplot2,Rpostgresql,我想使用Rpostgresql从数据库中收集一些数据,并为其制作条形图。这是剧本 library(RPostgreSQL) library(ggplot2) 我知道这些错误是因为数据帧l2为空。因此,在这种情况下,如何避免上述错误并只绘制一个图形(我的意思是,如果返回值/数据帧为空,则跳过脚本) 将代码包装在函数中,并进行适当的输入检查。以下是一个简化的示例: Error in cut.default(l2$eventtime, breaks = "hour") : 'x' must
library(RPostgreSQL)
library(ggplot2)
我知道这些错误是因为数据帧l2为空。因此,在这种情况下,如何避免上述错误并只绘制一个图形(我的意思是,如果返回值/数据帧为空,则跳过脚本) 将代码包装在函数中,并进行适当的输入检查。以下是一个简化的示例:
Error in cut.default(l2$eventtime, breaks = "hour") :
'x' must be numeric
在
if(nrow(l1)>0){dothis}
中包装代码部分?
Error in cut.default(l2$eventtime, breaks = "hour") :
'x' must be numeric
myfun <- function(DF) {
stopifnot(nrow(DF) > 0)
return("result of calculations")
}
myfun(iris)
#[1] "result of calculations"
myfun(iris[0,])
#Error: nrow(DF) > 0 is not TRUE
tryCatch(myfun(iris[0,]), error = function(e) "alternative result")
#[1] "alternative result"