From ad02c87d37a6efeacf6a5ba32b099c410cd042a4 Mon Sep 17 00:00:00 2001 From: hansvancalster Date: Tue, 13 Feb 2024 11:38:13 +0100 Subject: [PATCH 1/3] improve function: also allow input to be a character vector --- source/r/check_presence.R | 50 +++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/source/r/check_presence.R b/source/r/check_presence.R index 62f4e4b..95e61be 100644 --- a/source/r/check_presence.R +++ b/source/r/check_presence.R @@ -1,16 +1,29 @@ -library(dplyr) -library(purrr) - -# Set working directory -setwd("YOUR_WORKING_DIRECTORY") - -# West-Europa -# Belgiƫ, Frankrijk, Duitsland, Luxemburg, Nederland, Zwitserland, Oostenrijk - - -check_presence_from_file <- function(input_file = "./EJP_inse01_species_unique.txt", countries = c("BE", "FR", "DE", "LU", "NL", "CH", "AT")) { - # Read scientific names from the input file - scientificNames <- readLines(input_file) +#' 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 + scientificNames <- readLines(input) + } else if (is.character(input)) { + scientificNames <- input + } else { + stop("Input must be either a character vector or a valid file path.") + } # Remove any empty lines scientificNames <- scientificNames[scientificNames != ""] @@ -47,13 +60,10 @@ check_presence_from_file <- function(input_file = "./EJP_inse01_species_unique.t } # Create tibble with results - result_table <- tibble::tibble(present = presence, scientificName = scientificNames) + result_table <- tibble::tibble( + present = presence, + scientificName = scientificNames + ) return(result_table) } - -# Example usage: -result_WestEurope <- check_presence_from_file() -print(result_WestEurope) - -write.csv(result_WestEurope, "result_WestEurope.csv") From 1b5740ec81496c7656dd86044e1e455ad5502066 Mon Sep 17 00:00:00 2001 From: hansvancalster Date: Tue, 13 Feb 2024 11:43:55 +0100 Subject: [PATCH 2/3] use snake case --- source/r/check_presence.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/source/r/check_presence.R b/source/r/check_presence.R index 95e61be..99f2bb7 100644 --- a/source/r/check_presence.R +++ b/source/r/check_presence.R @@ -18,18 +18,18 @@ check_presence <- function( require(purrr) if (!is.null(input) && file.exists(input)) { # Read scientific names from the input file - scientificNames <- readLines(input) + scientific_names <- readLines(input) } else if (is.character(input)) { - scientificNames <- input + scientific_names <- input } else { stop("Input must be either a character vector or a valid file path.") } # Remove any empty lines - scientificNames <- scientificNames[scientificNames != ""] + scientific_names <- scientific_names[scientific_names != ""] # Initialize vector to store presence results - presence <- logical(length(scientificNames)) + presence <- logical(length(scientific_names)) # Loop over each country for (country in countries) { @@ -53,7 +53,7 @@ check_presence <- function( } # Map over scientific names and check occurrence data for the current country - country_presence <- purrr::map_lgl(scientificNames, check_occurrence) + country_presence <- purrr::map_lgl(scientific_names, check_occurrence) # Update presence vector with results for the current country presence <- presence | country_presence @@ -62,7 +62,7 @@ check_presence <- function( # Create tibble with results result_table <- tibble::tibble( present = presence, - scientificName = scientificNames + scientific_name = scientific_names ) return(result_table) From 78dbfdc8b51685da51b801ea5cdfb013a213219f Mon Sep 17 00:00:00 2001 From: hansvancalster Date: Tue, 13 Feb 2024 11:51:23 +0100 Subject: [PATCH 3/3] fix linter --- source/r/check_presence.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/r/check_presence.R b/source/r/check_presence.R index 99f2bb7..25f1a33 100644 --- a/source/r/check_presence.R +++ b/source/r/check_presence.R @@ -52,7 +52,8 @@ check_presence <- function( } } - # Map over scientific names and check occurrence data for the current country + # Map over scientific names and check occurrence data for the current + # country country_presence <- purrr::map_lgl(scientific_names, check_occurrence) # Update presence vector with results for the current country