From 8045645569483b02bee0ef8fe736275c8484f916 Mon Sep 17 00:00:00 2001 From: Hugo Gruson <10783929+Bisaloo@users.noreply.github.com> Date: Thu, 10 Jul 2025 10:11:01 +0200 Subject: [PATCH 1/4] Use xor() where possible --- R/fd_feve.R | 3 +-- R/fd_raoq.R | 9 ++------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/R/fd_feve.R b/R/fd_feve.R index aadcc3ce..855273d7 100644 --- a/R/fd_feve.R +++ b/R/fd_feve.R @@ -34,8 +34,7 @@ #' @export fd_feve <- function(traits = NULL, sp_com, dist_matrix = NULL) { - if ((!is.null(traits) && !is.null(dist_matrix)) || - (is.null(traits) && is.null(dist_matrix))) { + if (!xor(is.null(traits), is.null(dist_matrix))) { stop( "Please provide either a trait dataset or a dissimilarity matrix", call. = FALSE diff --git a/R/fd_raoq.R b/R/fd_raoq.R index e77f1533..22d9f595 100644 --- a/R/fd_raoq.R +++ b/R/fd_raoq.R @@ -34,8 +34,7 @@ #' @export fd_raoq <- function(traits = NULL, sp_com, dist_matrix = NULL) { - if ((!is.null(traits) && !is.null(dist_matrix)) || - (is.null(traits) && is.null(dist_matrix))) { + if (!xor(is.null(traits), is.null(dist_matrix))) { stop( "Please provide either a trait dataset or a dissimilarity matrix", call. = FALSE @@ -79,11 +78,7 @@ fd_raoq <- function(traits = NULL, sp_com, dist_matrix = NULL) { } - if (is.null(rownames(sp_com))) { - - rownames(sp_com) <- paste0("s", seq_len(nrow(sp_com))) - - } + rownames(sp_com) <- rownames(sp_com) %||% paste0("s", seq_len(nrow(sp_com))) # Standardize abundance per site site_abundances <- rowSums(sp_com, na.rm = TRUE) From 7db98f101904376f2d46a539b2a7ce046811993a Mon Sep 17 00:00:00 2001 From: Hugo Gruson <10783929+Bisaloo@users.noreply.github.com> Date: Thu, 10 Jul 2025 10:11:35 +0200 Subject: [PATCH 2/4] Mark fixed pattern detection as such --- vignettes/_fundiversity_2-performance.Rmd | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vignettes/_fundiversity_2-performance.Rmd b/vignettes/_fundiversity_2-performance.Rmd index 006750cb..a5614376 100644 --- a/vignettes/_fundiversity_2-performance.Rmd +++ b/vignettes/_fundiversity_2-performance.Rmd @@ -386,8 +386,7 @@ all_bench_sites <- list(fric = bench_sites_fric, raoq = bench_sites_raoq, feve = bench_sites_feve) %>% bind_rows(.id = "fd_index") %>% - mutate(n_sites = gsub("sites", "", expr) %>% - as.numeric()) + mutate(n_sites = as.numeric(gsub("sites", "", expr, fixed = TRUE))) all_bench_sites %>% ggplot(aes(n_sites, time * 1e-9, color = fd_index)) + From 06d6ac8e354ed4a1b664fa388172d4d49d3b7f8a Mon Sep 17 00:00:00 2001 From: Hugo Gruson <10783929+Bisaloo@users.noreply.github.com> Date: Thu, 10 Jul 2025 10:12:35 +0200 Subject: [PATCH 3/4] Use anyNA() where possible --- DESCRIPTION | 6 +++--- R/fd_fric_intersect.R | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5f9bc8e1..65acaa9c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: fundiversity Title: Easy Computation of Functional Diversity Indices Version: 1.1.1 -Authors@R: +Authors@R: c( person("Matthias", "GreniƩ", email = "matthias.grenie@gmail.com", @@ -24,8 +24,8 @@ Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.2 -Depends: - R (>= 2.10) +Depends: + R (>= 3.2.0) Imports: future.apply, geometry, diff --git a/R/fd_fric_intersect.R b/R/fd_fric_intersect.R index dc8cb46a..e5dc11c7 100644 --- a/R/fd_fric_intersect.R +++ b/R/fd_fric_intersect.R @@ -145,7 +145,7 @@ fd_fric_intersect <- function(traits, sp_com, stand = FALSE) { } }, future.globals = FALSE) - if (any(is.na(fric_intersect))) { + if (anyNA(fric_intersect)) { warning( "Some sites had less species than traits so returned FRic_intersect ", "is 'NA'", From 51fb1a1188a1bea5e9f8d6f578216f3c5ecca3d8 Mon Sep 17 00:00:00 2001 From: Hugo Gruson <10783929+Bisaloo@users.noreply.github.com> Date: Thu, 10 Jul 2025 11:06:30 +0200 Subject: [PATCH 4/4] Use %||% where possible --- R/fd_feve.R | 6 +----- R/fd_fric_intersect.R | 5 +---- R/utils.R | 9 +++++++++ 3 files changed, 11 insertions(+), 9 deletions(-) create mode 100644 R/utils.R diff --git a/R/fd_feve.R b/R/fd_feve.R index 855273d7..a448e8a5 100644 --- a/R/fd_feve.R +++ b/R/fd_feve.R @@ -70,11 +70,7 @@ fd_feve <- function(traits = NULL, sp_com, dist_matrix = NULL) { ) } - if (is.null(rownames(sp_com))) { - - rownames(sp_com) <- paste0("s", seq_len(nrow(sp_com))) - - } + rownames(sp_com) <- rownames(sp_com) %||% paste0("s", seq_len(nrow(sp_com))) # Standardize abundance per site site_abundances <- rowSums(sp_com, na.rm = TRUE) diff --git a/R/fd_fric_intersect.R b/R/fd_fric_intersect.R index e5dc11c7..793aba3a 100644 --- a/R/fd_fric_intersect.R +++ b/R/fd_fric_intersect.R @@ -88,11 +88,8 @@ fd_fric_intersect <- function(traits, sp_com, stand = FALSE) { } - if (is.null(rownames(sp_com))) { + rownames(sp_com) <- rownames(sp_com) %||% paste0("s", seq_len(nrow(sp_com))) - rownames(sp_com) <- paste0("s", seq_len(nrow(sp_com))) - - } max_range <- 1 diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 00000000..0ec70aa2 --- /dev/null +++ b/R/utils.R @@ -0,0 +1,9 @@ +# This can be deleted in favour of base R version if we ever start depending +# on R (>= 4.4.0) +`%||%` <- function(x, y) { + if (is.null(x)) { + y + } else { + x + } +} \ No newline at end of file