Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如果返回值为零,则跳过脚本(我的意思是如果数据帧有空行)_R_Ggplot2_Rpostgresql - Fatal编程技术网

R 如果返回值为零,则跳过脚本(我的意思是如果数据帧有空行)

R 如果返回值为零,则跳过脚本(我的意思是如果数据帧有空行),r,ggplot2,rpostgresql,R,Ggplot2,Rpostgresql,我想使用Rpostgresql从数据库中收集一些数据,并为其制作条形图。这是剧本 library(RPostgreSQL) library(ggplot2) 我知道这些错误是因为数据帧l2为空。因此,在这种情况下,如何避免上述错误并只绘制一个图形(我的意思是,如果返回值/数据帧为空,则跳过脚本) 将代码包装在函数中,并进行适当的输入检查。以下是一个简化的示例: Error in cut.default(l2$eventtime, breaks = "hour") : 'x' must

我想使用Rpostgresql从数据库中收集一些数据,并为其制作条形图。这是剧本

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"