CmlMagneville / mFD

:package: A Computation of Functional Spaces and Functional Diversity Indices
https://cmlmagneville.github.io/mFD/
GNU General Public License v2.0
25 stars 5 forks source link

funct.dist check error with only quantitative traits #34

Closed acottin closed 1 year ago

acottin commented 1 year ago

Hello,

First, thanks for the nice package and its documentation. I got an error after a package update (to 1.0.4), when computing functional distance with only quantitative traits.

Error in if (tr_cat$trait_type == rep("Q", nrow(tr_cat))) { : 
  the condition has length > 1

To reproduce :

library(mFD)
library(tidyverse)

data("fruits_traits", package = "mFD")
data("fruits_traits_cat", package = "mFD")

# keep only numeric traits (change fuzzy to quant for example)
fruits_traits     <- select(fruits_traits, where(~ is.numeric(.x)))
fruits_traits_cat <- tibble(trait_name = colnames(fruits_traits),
                            trait_type = "Q")

sp_dist_fruits <- mFD::funct.dist(sp_tr         = fruits_traits,
                                  tr_cat        = fruits_traits_cat,
                                  metric        = "euclidean",
                                  scale_euclid  = "scale_center")

I think the fix is simple, with either

if (all(tr_cat$trait_type == rep("Q", nrow(tr_cat)))) {

or

if (all(unique(tr_cat$trait_type == "Q"))) {

Should I make a pull request ?


> sessionInfo()
R version 4.2.0 (2022-04-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.2 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
 [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C               LC_TIME=fr_FR.UTF-8        LC_COLLATE=fr_FR.UTF-8     LC_MONETARY=fr_FR.UTF-8   
 [6] LC_MESSAGES=fr_FR.UTF-8    LC_PAPER=fr_FR.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] lubridate_1.9.2 forcats_1.0.0   stringr_1.5.0   dplyr_1.1.2     purrr_1.0.1     readr_2.1.4     tidyr_1.3.0     tibble_3.2.1   
 [9] ggplot2_3.4.2   tidyverse_2.0.0 mFD_1.0.4      

loaded via a namespace (and not attached):
 [1] tidyselect_1.2.0  GA_3.2.3          splines_4.2.0     lattice_0.21-8    colorspace_2.1-0  vctrs_0.6.3       generics_0.1.3   
 [8] geometry_0.4.7    mgcv_1.9-0        utf8_1.2.3        rlang_1.1.1       pillar_1.9.0      glue_1.6.2        withr_2.5.0      
[15] foreach_1.5.2     lifecycle_1.0.3   gawdis_0.1.5      munsell_0.5.0     gtable_0.3.3      codetools_0.2-19  magic_1.6-1      
[22] permute_0.9-7     tzdb_0.4.0        parallel_4.2.0    fansi_1.0.4       Rcpp_1.0.11       scales_1.2.1      vegan_2.6-4      
[29] abind_1.4-5       hms_1.1.3         digest_0.6.33     stringi_1.7.12    grid_4.2.0        ade4_1.7-22       cli_3.6.1        
[36] tools_4.2.0       magrittr_2.0.3    patchwork_1.1.2   FD_1.0-12.1       cluster_2.1.4     crayon_1.5.2      ape_5.7-1        
[43] pkgconfig_2.0.3   MASS_7.3-60       Matrix_1.6-0      timechange_0.2.0  rstudioapi_0.15.0 iterators_1.0.14  R6_2.5.1         
[50] nlme_3.1-162      compiler_4.2.0  
marcelxelo commented 1 year ago

I had the same problem, and your solution worked well. The function should be updated with that adjustment.

Thanks!

CmlMagneville commented 1 year ago

Hi! I've seen this issue but I can't have a look at it for now. I will do it as soon as possible in September. Thank you for telling me ;)

CmlMagneville commented 1 year ago

This bug has been fixed on the devel version of the mFD package on Github (changed in CRAN, next submission of the package).