我一直在尝试使用切达干酪包进行一些物种删除模拟,并遇到错误:
Error in RemoveNodes(new.community, new.remove, title = title, method = "cascade") :
Removing these nodes would result in an empty community
您可以重新创建错误:
library(cheddar)
data(SkipwithPond)
a<-RemoveNodes(SkipwithPond,c('Detritus','Corixidae nymphs','Agabus / Ilybius larvae'),method='cascade')
我想知道是否可以禁用此功能以便允许删除?如果没有,那么如果发生此错误,是否有办法返回某个值(在这种情况下,网络中的节点数)?
答案 0 :(得分:1)
我对cheddar
包不太了解,但你提到的第二个选项是在尝试评估表达式后“捕获”错误。输入tryCatch
。请参阅此函数的文档,但通常在将tryCatch
的结果保存到变量时,可以重定向流以适应错误。
# spaces possibly make code easier to read
a <- tryCatch(RemoveNodes(SkipwithPond, c('Detritus','Corixidae nymphs','Agabus / Ilybius larvae'), method='cascade'), error = function(e) e)
# str(a) to see what the error is (message, class...) and act on that message
# or if you want a custom message to catch
a <- tryCatch(RemoveNodes(SkipwithPond, c('Detritus','Corixidae nymphs','Agabus / Ilybius larvae'), method='cascade'), error = function(e) "empty community?")
if (a$message == "empty community?") {
# ...do something
}