Skip to content

Commit

Permalink
Fix check for variable names for new.env in BIOMOD_Projection and BIO…
Browse files Browse the repository at this point in the history
…MOD_EnsembleForecasting (add exact parameter in .fun_testIfIn function)
  • Loading branch information
MayaGueguen committed May 22, 2024
1 parent bdd84b0 commit e177d8a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions R/BIOMOD_EnsembleForecasting.R
Original file line number Diff line number Diff line change
Expand Up @@ -593,11 +593,11 @@ BIOMOD_EnsembleForecasting <- function(bm.em,
}

if (inherits(new.env, 'SpatRaster')) {
.fun_testIfIn(TRUE, "names(new.env)", names(new.env), bm.em@expl.var.names)
.fun_testIfIn(TRUE, "names(new.env)", names(new.env), bm.em@expl.var.names, exact = TRUE)
new.env.mask <- .get_data_mask(new.env, value.out = 1)
new.env <- mask(new.env, new.env.mask)
} else {
.fun_testIfIn(TRUE, "colnames(new.env)", colnames(new.env), bm.em@expl.var.names)
.fun_testIfIn(TRUE, "colnames(new.env)", colnames(new.env), bm.em@expl.var.names, exact = TRUE)
}

which.factor <- which(sapply(new.env, is.factor))
Expand Down
4 changes: 2 additions & 2 deletions R/BIOMOD_Projection.R
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,11 @@ BIOMOD_Projection <- function(bm.mod,
}

if (inherits(new.env, 'SpatRaster')) {
.fun_testIfIn(TRUE, "names(new.env)", names(new.env), bm.mod@expl.var.names)
.fun_testIfIn(TRUE, "names(new.env)", names(new.env), bm.mod@expl.var.names, exact = TRUE)
new.env.mask <- .get_data_mask(new.env, value.out = 1)
new.env <- mask(new.env, new.env.mask)
} else {
.fun_testIfIn(TRUE, "colnames(new.env)", colnames(new.env), bm.mod@expl.var.names)
.fun_testIfIn(TRUE, "colnames(new.env)", colnames(new.env), bm.mod@expl.var.names, exact = TRUE)
new.env <- new.env[ , bm.mod@expl.var.names, drop = FALSE]
}

Expand Down
5 changes: 3 additions & 2 deletions R/biomod2_internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@
return(test)
}

.fun_testIfIn <- function(test, objName, objValue, values)
.fun_testIfIn <- function(test, objName, objValue, values, exact = FALSE)
{
if (any(! objValue %in% values)) {
if (any(! objValue %in% values) ||
(exact == TRUE && any(! values %in% objValue))) {
stop(paste0("\n", objName, " must be '",
ifelse(length(values) > 1,
paste0(paste0(values[1:(length(values) -1)], collapse = "', '"),
Expand Down

0 comments on commit e177d8a

Please sign in to comment.