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_feve.R b/R/fd_feve.R index aadcc3ce..a448e8a5 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 @@ -71,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 dc8cb46a..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 @@ -145,7 +142,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'", 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) 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 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)) +