envfit() Error in object$weights
Closed this issue · 8 comments
When running envfit() with vegan_2.6-8, I received the following error message.
> Error in object$weights : $ operator is invalid for atomic vectors
The code performs as expected with the following session information:
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggforce_0.4.2 featuretable_0.0.11 lubridate_1.9.2
[4] forcats_1.0.0 stringr_1.5.1 dplyr_1.1.0
[7] purrr_1.0.2 readr_2.1.4 tidyr_1.3.1
[10] tibble_3.2.1 ggplot2_3.4.1 tidyverse_2.0.0
[13] vegan_2.6-4 lattice_0.22-6 permute_0.9-7
The error is produced with:
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] vegan_2.6-4 lattice_0.22-6 permute_0.9-7 ggforce_0.4.2
[5] cowplot_1.1.3 viridis_0.6.5 viridisLite_0.4.2 featuretable_0.0.11
[9] lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4
[13] purrr_1.0.2 readr_2.1.5 tidyr_1.3.1 tibble_3.2.1
[17] ggplot2_3.5.1 tidyverse_2.0.0 biplotr_0.0.13 magrittr_2.0.3
and
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] vegan_2.6-8 lattice_0.22-6 permute_0.9-7 ggforce_0.4.2 cowplot_1.1.3
[6] viridis_0.6.5 viridisLite_0.4.2 featuretable_0.0.11 lubridate_1.9.3 forcats_1.0.0
[11] stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2 readr_2.1.5 tidyr_1.3.1
[16] tibble_3.2.1 ggplot2_3.5.1 tidyverse_2.0.0 biplotr_0.0.13 magrittr_2.0.3
Therefore, I believe a dependency update is causing the issue. Please advise.
Really hard to give an advice since I cannot reproduce this and there is no reproducible example. I do not even know if this error comes from vegan or from some other package. Element object$weights
is only referenced to in fitspecaccum
and stressplot.wcmdscale
and neither of these is connected to envfit
.
It would be best to have a reproducible example. In lack of that, the minimum is to have traceback()
which returns the call stack. With that we could see what is the function that gives the error and how we got there.
Only you (@AlexaBennett) can debug this; start from a clean session (R --vanilla
) and run your vegan code. Add 1 package at a time and rerun your vegan code. Rinse and repeat until you find loading which package leads to the error.
@jarioksa not much point keeping this open until the OP posts something we can work with (they can still comment and edit the issue even when closed)
There is a point of keeping this open because I'm working on this. This looks like a permanent existing problem in stats:::weights.default
which we have circumvented in many other functions in 3ddac18. We should handle those cases smoothly. After I get my trial fix done, I'll ask OP to test the new code.
Oops, sorry - I didn't realise you were actually working on this. Nobbling weights.default
sounds... icky.
Here is a way to get the same error message (but I do not know if the OP has the same case):
library(vegan)
data(dune, dune.env)
mod <- cca(dune)
envfit(mod ~ ., dune.env) # this works
envfit(scores(mod, display="sites") ~ ., dune.env) # this fails, but should work
## Error in object$weights : $ operator is invalid for atomic vectors
In old code I solved this by replacing stats:::weights.default
within function to always return NULL
, and in 3ddac18 by only allowing stats:::weights.default
if the argument is non-atomic (plain matrix is atomic).
@AlexaBennett : I do not have sufficient information of your particular case. I know how to get this error message (when you should not), but I do not know if this solves your problem. I do not even know what was your command that gave the error. However, if it was something like:
envfit(x ~ ., x.env)
Try replacing this with:
envfit(as.data.frame(x) ~ ., x.env)
If this fixes your problem, my planned fix will probably solve the same problem.
@jarioksa Thank you for following up on this!
Try replacing this with:
envfit(as.data.frame(x) ~ ., x.env)
The function runs as expected (again). Here is a little added context about the arguments I used. The argument ord
was a matrix with row names as sample names and columns as PC1..PCn. The argument env
was a data frame with row names as sample names and columns containing either continuous or discrete variables.