Skip to content

Commit

Permalink
function fixes
Browse files Browse the repository at this point in the history
* bug fix for `extract_data` so it works even after you have joined other columns
* edit `plot_trait_distribution_beeswarm` to work with a single table and for its message to accurately reflect which grouping variables are allowed
  • Loading branch information
ehwenk committed Nov 14, 2024
1 parent 09f96f2 commit 6a05516
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
4 changes: 3 additions & 1 deletion R/extract_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ extract_data <- function(database, table = NA, col, col_value) {
ret_tmp[["traits"]] <- database[["traits"]]%>%
dplyr::semi_join(cc_traits, by = columns_to_select)

columns_to_select_excluded <- intersect(setdiff(names(database$excluded_data), "value"), names(database[[table[[i]]]]))

# Use same filtering join to trim excluded data
ret_tmp[["excluded_data"]] <- database[["excluded_data"]] %>%
dplyr::semi_join(cc_traits, by = columns_to_select)
dplyr::semi_join(cc_traits, by = columns_to_select_excluded)


for (j in seq_along(tables_tmp$tables_to_cut)) {
Expand Down
37 changes: 27 additions & 10 deletions R/plot_trait_distribution_beeswarm.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ plot_trait_distribution_beeswarm <- function(database,
hide_ids = FALSE) {

# Check compatability
status <- check_compatibility(database)
status <- check_compatibility(database, single_table_allowed = TRUE)

# If compatible
if(!status) {
function_not_supported(database)
Expand All @@ -44,17 +44,27 @@ plot_trait_distribution_beeswarm <- function(database,
factor(p, levels=names(my_shapes))
}

tax_info <- database_trait$taxa %>% dplyr::select(taxon_name, family)
if (is.null(dim(database_trait))) {

tax_info <- database_trait$taxa %>% dplyr::select(taxon_name, family, genus)

data <-
database_trait$traits %>%
dplyr::left_join(by = "taxon_name", tax_info)

} else {

data <- database_trait

Check warning on line 57 in R/plot_trait_distribution_beeswarm.R

View check run for this annotation

Codecov / codecov/patch

R/plot_trait_distribution_beeswarm.R#L57

Added line #L57 was not covered by tests

data <-
database_trait$traits %>%
}

data <- data %>%
dplyr::mutate(shapes = as_shape(value_type)) %>%
dplyr::left_join(by = "taxon_name", tax_info) %>%
dplyr::mutate(value = as.numeric(value))

# Define grouping variables and derivatives
if(!y_axis_category %in% names(data)) {
stop("Incorrect grouping variable! Currently implemented for `family` or `dataset_id`")
stop("Incorrect grouping variable! Grouping variable must be a variable in or joined to the traits table. Family and genus are supported if your input is a complete traits.build database.")

Check warning on line 67 in R/plot_trait_distribution_beeswarm.R

View check run for this annotation

Codecov / codecov/patch

R/plot_trait_distribution_beeswarm.R#L67

Added line #L67 was not covered by tests
}

# define grouping variable, ordered by group-level by mean values
Expand All @@ -77,10 +87,17 @@ plot_trait_distribution_beeswarm <- function(database,
if(!is.na(highlight) & highlight %in% data$Group) {
data <- dplyr::mutate(data, colour = ifelse(Group %in% highlight, "c", colour))
}


vals <- list(minimum = purrr::pluck(database_trait, "definitions", trait_name, "allowed_values_min"),
maximum = purrr::pluck(database_trait, "definitions", trait_name, "allowed_values_max"))
if (is.null(dim(database))) {

vals <- list(minimum = purrr::pluck(database_trait, "definitions", trait_name, "allowed_values_min"),
maximum = purrr::pluck(database_trait, "definitions", trait_name, "allowed_values_max"))

} else {

vals <- list(minimum = 0.8*min(data$value),
maximum = 1.2*max(data$value))

Check warning on line 99 in R/plot_trait_distribution_beeswarm.R

View check run for this annotation

Codecov / codecov/patch

R/plot_trait_distribution_beeswarm.R#L98-L99

Added lines #L98 - L99 were not covered by tests
}

range <- (vals$maximum/vals$minimum)

Expand Down

0 comments on commit 6a05516

Please sign in to comment.