Skip to content

Commit

Permalink
Fix #106
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Grenié committed Apr 5, 2024
1 parent e01aaf6 commit c1f32fa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
11 changes: 6 additions & 5 deletions R/fb_plot_trait_correlation.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ fb_plot_trait_correlation <- function(
check_species_categories(species_categories)

# Keep only numeric columns
trait_type <- vapply(species_traits[, -1, drop = FALSE], typeof, character(1))

numerical_traits <- trait_type %in% c("double", "integer")
numerical_traits <- vapply(
species_traits[, -1, drop = FALSE], is.numeric, logical(1)
)

if (!any(numerical_traits)) {
stop("No numerical traits found, cannot plot trait correlations")
stop("No numerical traits found, cannot plot trait correlations",
call. = FALSE)
}

if (!all(numerical_traits)) {
message("Non-numerical traits found, only keeping numerical traits ",
"to show trait correlation")
"to display trait correlations")
}

# Subset Traits --------------------------------------------------------------
Expand Down
27 changes: 20 additions & 7 deletions tests/testthat/test-fb_plot_trait_correlation.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
# Initial data -----------------------------------------------------------------

sp_trait <- data.frame(species = letters[1:3], trait1 = letters[1:3],
trait2 = 1:3, trait3 = 3:1)
sp_trait <- data.frame(
species = letters[1:3],
trait1 = letters[1:3],
trait2 = 1:3,
trait3 = 3:1,
trait4 = factor(letters[1:3])
)


# Actual Tests -----------------------------------------------------------------

test_that("fb_plot_trait_correlation() works", {

# Wrong input
# Wrong input, only non-numerical traits
expect_error(
fb_plot_trait_correlation(sp_trait[, 1:2]),
"No numerical traits found, cannot plot trait correlations",
fixed = TRUE
)

# Wrong input, only non-numerical traits
expect_error(
fb_plot_trait_correlation(sp_trait[, c(1:2, 5)]),
"No numerical traits found, cannot plot trait correlations",
fixed = TRUE
)

# Good input
expect_silent(res <- fb_plot_trait_correlation(species_traits))

Expand All @@ -25,7 +37,7 @@ test_that("fb_plot_trait_correlation() works", {
res <- fb_plot_trait_correlation(sp_trait),
paste0(
"Non-numerical traits found, only keeping numerical traits ",
"to show trait correlation"
"to display trait correlations"
),
fixed = TRUE
)
Expand All @@ -36,22 +48,23 @@ test_that("fb_plot_trait_correlation() works", {
# Single category
expect_silent(
given_plot <- fb_plot_trait_correlation(
sp_trait[, -2], data.frame(species = sp_trait$species, category = "A")
sp_trait[, -c(2, 5)],
data.frame(species = sp_trait$species, category = "A")
)
)

# Less categories than species
expect_silent(
given_plot <- fb_plot_trait_correlation(
sp_trait[, -2],
sp_trait[, -c(2, 5)],
data.frame(species = sp_trait$species, category = c(1, 1, 2))
)
)

# As many categories as species
expect_silent(
given_plot <- fb_plot_trait_correlation(
sp_trait[, -2],
sp_trait[, -c(2, 5)],
data.frame(species = sp_trait$species,
category = sp_trait$species)
)
Expand Down

0 comments on commit c1f32fa

Please sign in to comment.