valentinitnelav / bootstrapnet

R package for bootstrapping indices of bipartite ecological networks
https://valentinitnelav.github.io/bootstrapnet/
MIT License
5 stars 2 forks source link

Replace all try() with tryCatch() when bootstrapping #32

Open valentinitnelav opened 4 years ago

valentinitnelav commented 4 years ago

Would it be better to replace all the try statements with tryCatch, because we can assign return values like NA in case of error?

So, for example in boot_networklevel_once(), instead of having this:

  for (i in 1:length(ids_lst)){
    metric_lst[[i]] <- try({
      web <- data[ids_lst[[i]], table(get(col_lower), get(col_higher))]
      set.seed(42)
      bipartite::networklevel(web = web, index = index, level = level, ...)
    })
  }

we could better have this:

  for (i in 1:length(ids_lst)){
    metric_lst[[i]] <- tryCatch({
      web <- data[ids_lst[[i]], table(get(col_lower), get(col_higher))]
      set.seed(42)
      bipartite::networklevel(web = web, index = index, level = level, ...)
    },
    error = function(e) NA)
  }

This will return NA-s in case of errors, though it might be dangerous because try can be still informative because it can return the error message ... Though tryCatch can also be 'instructed' to pass the error message, but is just getting more verbose than try in that case.