Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt check presence #9

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions source/r/check_presence.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#' Deze functie gaat na of een soort volgens GBIF voorkomt in een gekozen set
#' van landen
#'
#' Default zijn West-Europese landen ingesteld: België, Frankrijk, Duitsland,
#' Luxemburg, Nederland, Zwitserland, Oostenrijk
#'
#' @param input A character vector of Scientific names or a .txt file from which
#' Scientific Names can be read.
#' @param countries a character vector of country codes
#'
#' @example
#' result_WestEurope <- check_presence_from_file()
#' print(result_WestEurope)
check_presence <- function(
input = NULL,
countries = c("BE", "FR", "DE", "LU", "NL", "CH", "AT")) {
require(dplyr)
require(purrr)
if (!is.null(input) && file.exists(input)) {
# Read scientific names from the input file
scientific_names <- readLines(input)
} else if (is.character(input)) {
scientific_names <- input
} else {
stop("Input must be either a character vector or a valid file path.")
}

# Remove any empty lines
scientific_names <- scientific_names[scientific_names != ""]

# Initialize vector to store presence results
presence <- logical(length(scientific_names))

# Loop over each country
for (country in countries) {
# Function to check occurrence data and handle errors
check_occurrence <- function(name) {
data <- tryCatch({
rgbif::occ_data(
scientificName = name,
country = country, # Specify one country at a time
limit = 1
)
}, error = function(e) {
return(list(error = TRUE))
})

if (inherits(data, "error") || is.null(data$data)) {
return(FALSE) # Occurrence data is not present or error occurred
} else {
return(TRUE) # Occurrence data is present
}
}

# Map over scientific names and check occurrence data for the current country

Check warning on line 55 in source/r/check_presence.R

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/r/check_presence.R,line=55,col=81,[line_length_linter] Lines should not be more than 80 characters.
country_presence <- purrr::map_lgl(scientific_names, check_occurrence)

# Update presence vector with results for the current country
presence <- presence | country_presence
}

# Create tibble with results
result_table <- tibble::tibble(
present = presence,
scientific_name = scientific_names
)

return(result_table)
}
Loading