Skip to content

Commit

Permalink
Renaming files and parameters (#119)
Browse files Browse the repository at this point in the history
* standardising file names

* standardising parameters throughout austraits (closes issue #111)

- replace all `@param austraits` and `@param aus_traits` with `@param database`
- replace all references to just the traits table (previously x, data, trait( with `@param trait_data`
- standardise description of parameters - currently using `@param database traits.build database (list object)` so people realise it is a relational database, but could change to `@param database traits.build database`

* standardise pipes

- change all native pipes (|>) to %>% pipes (closes issue #113)
  • Loading branch information
ehwenk authored Nov 13, 2024
1 parent fdaf6bb commit 70e7671
Show file tree
Hide file tree
Showing 26 changed files with 250 additions and 249 deletions.
45 changes: 22 additions & 23 deletions R/as_wide_table.R
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
#' Create a single wide table from the AusTraits data object
#' Create a single wide table from a traits.build data object
#'
#' @param austraits austraits data object
#' @param database traits.build database (list object)
#'
#' @return A single wide table with collapsed contexts and locations text and with
#' some cols renamed for alignment with other resources
#' @export
#'
#' @examples
#' \dontrun{
#' data <- austraits
#' data %>% as_wide_table()
#' austraits %>% as_wide_table()
#' }
#' @importFrom stats family
#' @importFrom utils methods

as_wide_table <- function(austraits){
as_wide_table <- function(database){
# Check compatability
status <- check_compatibility(austraits)
status <- check_compatibility(database)

# If compatible
if(!status){
function_not_supported(austraits)
function_not_supported(database)
}

# Function to collapse columns in locations and contexts into single column
Expand All @@ -33,39 +32,39 @@ as_wide_table <- function(austraits){
}

################################################################################
# Define and adapt each table in the list of austraits to prepare for the wide table format
# Define and adapt each table in the list of a traits.build database to prepare for the wide table format

# The contexts table needs the contexts collapsed to one context name per site
austraits %>%
join_contexts_old(collapse_context = TRUE) -> austraits
database %>%
join_contexts_old(collapse_context = TRUE) -> database

# Getting rid of the columns that will soon be deleted in the next austraits release and renaming the description column
austraits$methods <-
austraits$methods %>%
database$methods <-
database$methods %>%
dplyr::rename(dataset_description = "description") %>%
dplyr::distinct()

# collapse into one column
austraits$locations <-
austraits$locations %>%
database$locations <-
database$locations %>%
dplyr::filter(value!="unknown") %>%
dplyr::rename("property" = "location_property") %>%
split(., .$dataset_id) %>%
purrr::map_dfr(process_table3)

# rename taxonomic_dataset field to reflect the APC/APNI name matching process better
austraits$taxa <-
austraits$taxa %>%
database$taxa <-
database$taxa %>%
dplyr::distinct()

austraits_wide <-
austraits$traits %>%
dplyr::left_join(by=c("dataset_id", "location_id"), austraits$locations) %>%
dplyr::left_join(by=c("dataset_id", "method_id", "trait_name"), austraits$methods) %>%
dplyr::left_join(by=c("taxon_name"), austraits$taxa)
database_wide <-
database$traits %>%
dplyr::left_join(by=c("dataset_id", "location_id"), database$locations) %>%
dplyr::left_join(by=c("dataset_id", "method_id", "trait_name"), database$methods) %>%
dplyr::left_join(by=c("taxon_name"), database$taxa)

# reorder the names to be more intuitive
austraits_wide %>% dplyr::select(
database_wide %>% dplyr::select(

# The most useful (if you are filtering for just one taxon_name)
"dataset_id", "observation_id", "trait_name", "taxon_name", "value", "unit",
Expand All @@ -92,7 +91,7 @@ as_wide_table <- function(austraits){
"taxon_rank", "genus", "family"
)

austraits_wide
database_wide
}

#' Collapse columns into text string
Expand Down
2 changes: 1 addition & 1 deletion R/bind_databases.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' database object as a large list.
#'
#' @param ... Arguments passed to other functions
#' @param databases List of traits.build databases to bind together (lists of tibbles)
#' @param databases List of traits.build databases to bind together
#'
#' @return Compiled database as a single large list
#' @importFrom rlang .data
Expand Down
2 changes: 1 addition & 1 deletion R/bind_trait_values.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @description This function condenses data for studies that have multiple observations for a given trait into a single row.
#' This function concatenates multiple values into a single cell
#' @usage bind_trait_values(trait_data)
#' @param trait_data The trait data frame generated from austraits - see example
#' @param trait_data the traits table in a traits.build database -- see example
#' @return tibble that is condensed down where multiple observations in value, value_type and replicates are collapsed down and separated by '--'
#'
#' @examples
Expand Down
18 changes: 9 additions & 9 deletions R/check_compatibility.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#' @title Check compatibility of traits.build object
#' @description Function to check whether the data object has been compiled by the traits.build workflow and
#' therefore has a data structure that is appropriate for use with austraits functions.
#' @param austraits dataframe generated by traits build
#' @param database traits.build database (list object)
#'
#' @return logical (T/F) output and messaging for uncompatible versions
#'
#' @examples
#' \dontrun{
#' check_compatibility(austraits)
#' check_compatibility(database)
#' }
#' @author Elizabeth Wenk - [email protected]

check_compatibility <- function(austraits) {
check_compatibility <- function(database) {

if (is.null(austraits$metadata)) {
if (is.null(database$metadata)) {

compatible <- FALSE

Expand All @@ -22,9 +22,9 @@ check_compatibility <- function(austraits) {
} else {

compiled_by_traits.build <-
austraits$metadata$related_identifiers %>%
database$metadata$related_identifiers %>%
convert_list_to_df2() %>%
dplyr::filter(relation_type == "isCompiledBy") |>
dplyr::filter(relation_type == "isCompiledBy") %>%
dplyr::filter(stringr::str_detect(identifier, "github.com/traitecoevo/traits.build"))

if(is.null(compiled_by_traits.build) | nrow(compiled_by_traits.build) > 0) {
Expand All @@ -46,13 +46,13 @@ check_compatibility <- function(austraits) {

#' Check compatibility of traits table
#'
#' @param traits table in traits.build object
#' @param trait_data the traits table in a traits.build database
#'
#' @return logical, TRUE indicating version traits table came from traits.build version > 1.0

check_traits_compatibility <- function(traits){
check_traits_compatibility <- function(trait_data){
# Check compatibility using column
if(any(names(traits) %in% c("treatment_context_id", "repeat_measurements_id"))){
if(any(names(trait_data) %in% c("treatment_context_id", "repeat_measurements_id"))){
compatible <- TRUE
} else
compatible <- FALSE
Expand Down
2 changes: 1 addition & 1 deletion R/extract_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' The output a traits.build formatted database with all tables subset
#' based on the specified table, column (variable) and column value.
#'
#' @param database traits.build database
#' @param database traits.build database (list object)
#' @param table Table within a traits.build database
#' @param col Column name within the specified table.
#' @param col_value Value (of column, from with a table) that is used to subset database. This can be a single value or a vector. It includes partial string matches.
Expand Down
2 changes: 1 addition & 1 deletion R/extract_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @description Function to subset all data associated with a particular dataset from a traits.build relational database.
#'
#' @usage extract_dataset(database, dataset_id)
#' @param database a large list of tibbles built by `traits.build` workflow
#' @param database traits.build database (list object)
#' @param dataset_id character string that matches a `dataset_id` in the database
#' @return List of tibbles containing all traits.build data and metadata for the specified dataset(s).
#' @details
Expand Down
6 changes: 3 additions & 3 deletions R/extract_taxa.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' @description Function to subset of all data associated with a particular taxon from a traits.build relational database.
#'
#' @param database a large list of tibbles built by `traits.build` workflow
#' @param database traits.build database (list object)
#' @param family character string of family or families
#' @param genus character string of genus or genera
#' @param taxon_name character string of taxon name(s)
Expand All @@ -19,8 +19,8 @@
#'
#' @examples
#' \dontrun{
#'extract_taxa(austraits, family = "Proteaceae")
#'extract_taxa(austraits, genus = "Acacia")
#'extract_taxa(database = austraits, family = "Proteaceae")
#'extract_taxa(database = austraits, genus = "Acacia")
#' }
#' @author Fonti Kar - [email protected]
#' @export
Expand Down
4 changes: 2 additions & 2 deletions R/extract_trait.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @description Function to subset all data associated with a particular trait from a traits.build relational database.
#'
#' @usage extract_trait(database, trait_names, taxon_names)
#' @param database a large list of tibbles built by `traits.build`
#' @param database traits.build database (list object)
#' @param trait_names character string of trait(s) for which data will be extracted
#' @param taxon_names optional argument, specifying taxa for which data will be extracted
#' @return List of tibbles containing all traits.build data and metadata for the specified trait(s).
Expand All @@ -19,7 +19,7 @@
#'
#' @examples
#' \dontrun{
#'extract_trait(database, "wood_density", taxon_name = "Acacia celsa")
#'extract_trait(database = austraits, trait_names = "wood_density", taxon_names = "Acacia celsa")
#' }
#' @author Daniel Falster - [email protected]
#' @export
Expand Down
2 changes: 1 addition & 1 deletion R/flatten_database.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' but all measurement-related metadata (methods, location properties, context properties, contributors)
#' are now included as additional columns in a single table.
#'
#' @param database A traits.build database
#' @param database traits.build database (list object)
#' @param format A parameter for the locations, contexts and data contributors tables specifying how data are packed.
#' All three can be formatted as a single compacted column(s) will have a human readable column ("single_column_pretty")
#' or using json ("single_column_json") syntax. For location properties or context properties there is also
Expand Down
Loading

0 comments on commit 70e7671

Please sign in to comment.