Skip to content

Commit

Permalink
Small checks for missing data and gracefully returning template #136
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Jung committed Feb 5, 2025
1 parent 05b87cf commit f95ad19
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions R/engine_gdb.R
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,10 @@ engine_gdb <- function(x,
newdata$rowid <- 1:nrow(newdata)
# Subset to non-missing data
newdata_sub <- subset(newdata, stats::complete.cases(newdata))
if(nrow(newdata_sub)==0) {
cli::cli_alert_danger("Every observation has missing data?")
newdata_sub <- newdata
}

if(getOption("ibis.runparallel",default = FALSE)){
check_package("doFuture")
Expand Down
4 changes: 4 additions & 0 deletions R/engine_glmnet.R
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,10 @@ engine_glmnet <- function(x,
# Make a subset of non-na values
df$rowid <- 1:nrow(df)
df_sub <- base::subset(df, stats::complete.cases(df))
if(nrow(df_sub)==0) {
cli::cli_alert_danger("Every observation has missing data?")
df_sub <- df
}
if(!is.Waiver(model$offset)) ofs <- model$offset[df_sub$rowid] else ofs <- NULL
assertthat::assert_that(nrow(df_sub)>0)

Expand Down
6 changes: 5 additions & 1 deletion R/project.R
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,11 @@ methods::setMethod(

# --- #
# Now predict
out <- x$project(newdata = env, layer = layer)
out <- try({ x$project(newdata = env, layer = layer) })
if(inherits(out, 'try-error')){
cli::cli_alert_danger("Projection failed! Returning emptyraster gracefully")
return(template)
}
names(out) <- paste0("suitability", "_", layer)
if(is.na(terra::crs(out))) terra::crs(out) <- terra::crs( model$background )
# --- #
Expand Down

0 comments on commit f95ad19

Please sign in to comment.