Skip to content

Commit

Permalink
Fixed issue in species suitabilities with empty columns
Browse files Browse the repository at this point in the history
  • Loading branch information
derek-corcoran-barrios committed Mar 19, 2024
1 parent c72cab0 commit 79358e1
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions R/species_suitability.R
Expand Up @@ -26,7 +26,12 @@
species_suitability <- function(Rastercurrent, species_names, name = "Problem", parameter = "SpeciesSuitability", verbose = FALSE){
SuitabilityTemp <- terra::as.data.frame(Rastercurrent, cells = T)
colnames(SuitabilityTemp)[-1] <- species_names
result <- species_names |> purrr::map(~paste_suitabilities(df = SuitabilityTemp, colname = .x)) |> purrr::reduce(c) |> paste(collapse = " ")
result <- species_names |>
purrr::map(~paste_suitabilities(df = SuitabilityTemp, colname = .x)) |>
purrr::reduce(c)

result <- result[result != ""]
result <- result |> paste(collapse = " ")
TempSpeciesNames <- paste(paste("param", parameter, "default 0 :=", result, ";"), collapse = " ")
if(file.exists(paste0(name, ".dat"))){
sink(paste0(name, ".dat"), append = T)
Expand All @@ -50,7 +55,11 @@ species_suitability <- function(Rastercurrent, species_names, name = "Problem",

paste_suitabilities <- function(df, colname){
filtered_df <- df[df[[colname]] > 0, ]
paste0(paste0("[", colname, ","), paste0(filtered_df$cell, "]", " ", as.vector(filtered_df[colname][,1])))
if(nrow(filtered_df) > 0){
paste0(paste0("[", colname, ","), paste0(filtered_df$cell, "]", " ", as.vector(filtered_df[colname][,1])))
} else if(nrow(filtered_df) == 0){
""
}
}


Expand Down

0 comments on commit 79358e1

Please sign in to comment.