Skip to content

Commit f95ad19

Browse files
committed
Small checks for missing data and gracefully returning template #136
1 parent 05b87cf commit f95ad19

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

R/engine_gdb.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,10 @@ engine_gdb <- function(x,
559559
newdata$rowid <- 1:nrow(newdata)
560560
# Subset to non-missing data
561561
newdata_sub <- subset(newdata, stats::complete.cases(newdata))
562+
if(nrow(newdata_sub)==0) {
563+
cli::cli_alert_danger("Every observation has missing data?")
564+
newdata_sub <- newdata
565+
}
562566

563567
if(getOption("ibis.runparallel",default = FALSE)){
564568
check_package("doFuture")

R/engine_glmnet.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,10 @@ engine_glmnet <- function(x,
860860
# Make a subset of non-na values
861861
df$rowid <- 1:nrow(df)
862862
df_sub <- base::subset(df, stats::complete.cases(df))
863+
if(nrow(df_sub)==0) {
864+
cli::cli_alert_danger("Every observation has missing data?")
865+
df_sub <- df
866+
}
863867
if(!is.Waiver(model$offset)) ofs <- model$offset[df_sub$rowid] else ofs <- NULL
864868
assertthat::assert_that(nrow(df_sub)>0)
865869

R/project.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,11 @@ methods::setMethod(
806806

807807
# --- #
808808
# Now predict
809-
out <- x$project(newdata = env, layer = layer)
809+
out <- try({ x$project(newdata = env, layer = layer) })
810+
if(inherits(out, 'try-error')){
811+
cli::cli_alert_danger("Projection failed! Returning emptyraster gracefully")
812+
return(template)
813+
}
810814
names(out) <- paste0("suitability", "_", layer)
811815
if(is.na(terra::crs(out))) terra::crs(out) <- terra::crs( model$background )
812816
# --- #

0 commit comments

Comments
 (0)