-
Notifications
You must be signed in to change notification settings - Fork 0
/
Process_species.R
65 lines (52 loc) · 2.04 KB
/
Process_species.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Load necessary libraries
library(rmarkdown)
#### Define the working directory for outputs #####################
workdir <- "D:/temp/SDM_Herpetofauna/Maxent_HighRes_SSP245SSP585/"
output_dir <- file.path(workdir, "html")
species_list <- file.path("D:/temp/SDM_Herpetofauna/Snakes/SpeciesList.txt")
###################################################################
# Create the output directory if it doesn’t exist
if (!dir.exists(output_dir)) dir.create(output_dir, recursive = TRUE)
# Read the species list from the text file
sp_list <- readLines(species_list)
# Base parameters that stay constant across reports and override defaults
base_params <- list(
Workdir = workdir,
Country = "",
Continent = "europe",
Database = "gbif",
Limit = 1500,
ExportRaster = "YES",
Range = "YES",
Climate.model = "MPI-ESM1-2-HR",
Ncores = 10,
SSP = "245",
Plot.xmin = "-11",
Plot.xmax = "33",
Plot.ymin = "34",
Plot.ymax = "65"
)
# Loop through each species and render an HTML report
for (species in sp_list) {
print(paste("Processing species:", species))
# Update species-specific parameter
params <- base_params
params$Species <- species # Set the species for this iteration
# Define the output file path
output_file <- file.path(output_dir, paste0(gsub(" ", "_", species), ".html"))
# Check if output file already exists; skip if it does
if (file.exists(output_file)) {
cat("Output already exists for species:", species, "- skipping to next species.\n")
next # Skip to the next iteration
}
# Use tryCatch to handle errors and continue the loop
tryCatch({
rmarkdown::render("D:/KMMA_documents/Rscripts/Markdown/SDM_Maxent/Maxent_SDM_HighRes.Rmd",
params = params,
output_file = output_file,
envir = new.env())
cat("Report generated for species:", species, "\n")
}, error = function(e) {
cat("Error encountered for species:", species, "- skipping to next species.\n")
})
}