TheoreticalEcology / s-jSDM

Scalable joint species distribution modeling
https://cran.r-project.org/web/packages/sjSDM/index.html
GNU General Public License v3.0
68 stars 14 forks source link

Bug: plot.sjSDM doesnt work for species with whitespace #115

Closed dansmi-hub closed 1 year ago

dansmi-hub commented 1 year ago

Method plot.sjSDM will fail if you pass a model with species names that contain white space (Genus; Species). This is obviously very common and the error message is unintuitive.

library(sjSDM)

model = simulate_SDM(env = 1)
fitted_model = sjSDM(Y = model$response, model$env_weights, se = TRUE)

# No species names
plot(fitted_model)

# Make species names
fitted_model$species = paste("Sp", letters[1:5])
plot(fitted_model)

Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent Warning message: In par(oldpar) : calling par(new=TRUE) with no plot

Problem comes from plotsjSDMcoef line 11. Easy fix for this without rewriting the plotting function is specifying the need for no white space in species for the plot function error, or removing trailing white space in the object before attempting to plot.

Something akin to janitor::make_clean_names() but in base R. I'm happy to look into this when I have more time.

MaximilianPi commented 1 year ago

Hi @dansmi-hub,

ah, good point. So the summary creates SP_name*Env_name combinations separated by a whitespace. And the whitespace is then used in the plot function to separate those two again, so either we use another character (e.g. ":"), or we store the names separately in the summary object, which is probably the more elegant option.

We would appreciate a pull request :). At the moment I don't have much time, but I guess in 1-2 months it will be time for a new CRAN version, because some bugs/improvements have accumulated.