Skip to content

Commit

Permalink
Import austraits (#177)
Browse files Browse the repository at this point in the history
Removal of functions from traits.build to accompany release of austraits 3.0

  *  util_df_to_list and util_list_to_df are now sourced from austraits (as convert_df_to_list and convert_list_to_df)
  * build_combine is now replaced by austraits::bind_databases
  *  plot_trait_distribution_beeswarm, trait_pivot_longer and trait_pivot_wider had been in both packages and have now been removed from traits.build
  *  the function flatten_database is a replacement for database_create_combined_table, which was still on a branch
  *  at the bottom of utils.R, the functions that have been moved and renamed are also assigned their old names, to ensure that the names included in our Eco Informatics publication are still "valid" and that people using old remake/build.R files are not inconvenienced by these changes
  * add deprecation warnings:
    -  Added lifecycle as dependency, reverted function for util_list_to_df1, added deprecation warning, reexported austraits::convert_list_to_df1
    -  Enabled md capabailities in rd docs, added lifecycle as a dependency, reexported austraits::convert_list_to_df2
    -  Deprecated util_list_to_df2 added lifecycle as a dependency, reexported austraits::convert_list_to_df2
    -  Deprecated convert_df_to_list, reexported austraits::convert_list_to_df
    - Deprecated build_combine, reexported austraits::bind_databases
    - Calling incorrect function in utils, added suppresswarnings in tests

---------

Co-authored-by: Fonti Kar <f.kar@unsw.edu.au>
  • Loading branch information
ehwenk and fontikar authored Nov 29, 2024
1 parent a17b455 commit c5014dc
Show file tree
Hide file tree
Showing 81 changed files with 410 additions and 726 deletions.
12 changes: 8 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Depends:
lubridate,
readr,
stringr,
tidyr
tidyr,
austraits
Imports:
crayon,
git2r,
Expand All @@ -38,7 +39,8 @@ Imports:
testthat,
tibble,
whisker,
yaml
yaml,
lifecycle
Suggests:
furrr,
remake,
Expand All @@ -57,8 +59,10 @@ Suggests:
zip,
covr
Remotes:
richfitz/remake
richfitz/remake,
traitecoevo/austraits
Encoding: UTF-8
VignetteBuilder: knitr
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Config/testthat/edition: 3
Roxygen: list(markdown = TRUE)
14 changes: 11 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(bind_databases)
export(build_add_version)
export(build_combine)
export(build_setup_pipeline)
export(check_pivot_duplicates)
export(convert_df_to_list)
export(convert_list_to_df1)
export(convert_list_to_df2)
export(dataset_build)
export(dataset_configure)
export(dataset_find_taxon)
export(dataset_process)
export(dataset_report)
export(dataset_test)
export(dataset_update_taxonomy)
export(db_traits_pivot_longer)
export(db_traits_pivot_wider)
export(flatten_database)
export(get_schema)
export(get_unit_conversions)
export(metadata_add_contexts)
Expand All @@ -32,7 +35,6 @@ export(metadata_exclude_observations)
export(metadata_find_taxonomic_change)
export(metadata_remove_taxonomic_change)
export(metadata_update_taxonomic_change)
export(plot_trait_distribution_beeswarm)
export(read_csv_char)
export(read_metadata)
export(util_df_to_list)
Expand All @@ -44,6 +46,11 @@ export(util_list_to_df1)
export(util_list_to_df2)
export(write_metadata)
export(write_plaintext)
importFrom(austraits,bind_databases)
importFrom(austraits,convert_df_to_list)
importFrom(austraits,convert_list_to_df1)
importFrom(austraits,convert_list_to_df2)
importFrom(austraits,flatten_database)
importFrom(crayon,"%+%")
importFrom(crayon,blue)
importFrom(crayon,green)
Expand All @@ -59,6 +66,7 @@ importFrom(dplyr,filter)
importFrom(dplyr,full_join)
importFrom(dplyr,mutate)
importFrom(dplyr,select)
importFrom(lifecycle,deprecated)
importFrom(lubridate,dmy)
importFrom(magrittr,"%>%")
importFrom(purrr,map_chr)
Expand Down
105 changes: 0 additions & 105 deletions R/pivot.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,108 +30,3 @@ check_pivot_wider <- function(dataset) {
}

}


#' @title Pivot long format data into a wide format
#'
#' @description `trait_pivot_wider` "widens" long format data ("tidy data").
#'
#' Databases built with `traits.build` are organised in a long format where observations are on different rows and the
#' type of observation is denoted by various identifying columns (e.g `trait_name`, `dataset_id`,
#' `observation_id`, etc.).
#' This function converts the data into wide format so that each trait in its own column.
#'
#' @param traits The traits table from database (list object)
#' @return A tibble in wide format
#' @details `trait_pivot_wider` will return a single wide tibble; note that some meta-data columns
#' (unit, replicates, measurement_remarks, basis_of_record, basis_of_value) will be excluded to
#' produce a useful wide tibble.
#' @examples
#' \dontrun{
#' data <- austraits$traits %>% filter(dataset_id == "Falster_2003")
#' data # Long format
#' traits_wide <- trait_pivot_wider(data)
#' traits_wide # Wide format
#' }
#' @author Daniel Falster - daniel.falster@unsw.edu.au
#' @export
db_traits_pivot_wider <- function(traits) {

metadata_cols <- c("unit", "replicates", "measurement_remarks", "basis_of_value")

# A check for if there are more than 1 value_type for a given taxon_name, observation_id and method
check_value_type <- traits %>%
dplyr::select(dplyr::all_of(c(
"trait_name", "value", "dataset_id", "observation_id", "method_id", "method_context_id",
"repeat_measurements_id", "value_type"))) %>%
dplyr::group_by(
.data$dataset_id, .data$observation_id, .data$method_id,
.data$method_context_id, .data$repeat_measurements_id) %>%
dplyr::summarise(n_value_type = length(unique(.data$value_type))) %>%
dplyr::arrange(.data$observation_id) %>%
dplyr::filter(.data$n_value_type > 1)

if (nrow(check_value_type) > 1) {

traits %>%
tidyr::pivot_wider(
names_from = "trait_name",
values_from = "value",
id_cols = -dplyr::all_of(metadata_cols)
)

} else {

metadata_cols <- c(metadata_cols, "value_type")

traits %>%
tidyr::pivot_wider(
names_from = "trait_name",
values_from = "value",
id_cols = -dplyr::all_of(metadata_cols)
)
}

}


#' @title Pivot wide format data into a long format
#'
#' @description `trait_pivot_longer` "gathers" wide format data into a "tidy" format.
#'
#' This function converts the data into long format where observations are on different rows and the type of
#' observation is denoted by the `trait_name` column.
#' In other words, `trait_pivot_longer` reverts the actions of `trait_pivot_wider`.
#' @param wide_data Output from `trait_pivot_wider` (a tibble of wide data)
#' @return A tibble in long format
#' @details
#' `trait_pivot_longer` will return a tibble with fewer columns than the original traits table
#' The excluded columns include: "unit", "replicates", "measurement_remarks", "basis_of_record",
#' "basis_of_value" # Double check #TODO
#'
#' @examples
#' \dontrun{
#' data <- austraits$traits %>%
#' filter(dataset_id == "Falster_2003")
#' data # Long format
#' traits_wide <- trait_pivot_wider(data)
#' traits_wide # Wide format
#'
#' values_long <- trait_pivot_longer(traits_wide)
#' }
#' @author Daniel Falster - daniel.falster@unsw.edu.au
#' @author Fonti Kar - fonti.kar@unsw.edu.au
#' @export
db_traits_pivot_longer <- function(wide_data) {

# The start of the trait columns is after `original_name`
start_of_trait_cols <- which(names(wide_data) == "original_name") + 1

wide_data %>%
tidyr::pivot_longer(
cols = start_of_trait_cols:ncol(.),
names_to = "trait_name",
values_drop_na = TRUE
)

}
161 changes: 0 additions & 161 deletions R/plot.R

This file was deleted.

Loading

0 comments on commit c5014dc

Please sign in to comment.