From 30fe41ba408e51d3bc515ab1f286947c8860ff17 Mon Sep 17 00:00:00 2001 From: ehwenk Date: Tue, 12 Nov 2024 08:45:00 +1100 Subject: [PATCH] edits to df_to_list and list_to_df functions - rename all with `convert_` instead of `util_` - add `convert_list_to_df1` from traits.build, so all 3 functions in one location --- NAMESPACE | 5 ++- R/bind_databases.R | 2 +- R/check_compatibility.R | 2 +- R/utils.R | 40 +++++++++++++++---- ...il_df_to_list.Rd => convert_df_to_list.Rd} | 11 ++--- man/convert_list_to_df1.Rd | 23 +++++++++++ ..._list_to_df2.Rd => convert_list_to_df2.Rd} | 14 ++++--- 7 files changed, 74 insertions(+), 23 deletions(-) rename man/{util_df_to_list.Rd => convert_df_to_list.Rd} (53%) create mode 100644 man/convert_list_to_df1.Rd rename man/{util_list_to_df2.Rd => convert_list_to_df2.Rd} (63%) diff --git a/NAMESPACE b/NAMESPACE index 4def034..cf686d9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,9 @@ export("%>%") export(as_wide_table) export(bind_databases) export(bind_trait_values) +export(convert_df_to_list) +export(convert_list_to_df1) +export(convert_list_to_df2) export(extract_data) export(extract_dataset) export(extract_taxa) @@ -31,8 +34,6 @@ export(summarise_austraits) export(summarise_trait_means) export(trait_pivot_longer) export(trait_pivot_wider) -export(util_df_to_list) -export(util_list_to_df2) import(RefManageR) importFrom(lifecycle,deprecated) importFrom(magrittr,"%>%") diff --git a/R/bind_databases.R b/R/bind_databases.R index 4d9faa4..a99f03f 100644 --- a/R/bind_databases.R +++ b/R/bind_databases.R @@ -60,7 +60,7 @@ bind_databases <- function(database_1, ...) { dplyr::select(-dplyr::any_of(c("dataset_id", "additional_role"))) %>% dplyr::distinct() %>% dplyr::arrange(.data$last_name, .data$given_name) %>% - util_df_to_list() + convert_df_to_list() ret <- list(traits = combine("traits", databases) %>% dplyr::arrange(.data$dataset_id, .data$observation_id, .data$trait_name), locations = combine("locations", databases) %>% dplyr::arrange(.data$dataset_id, .data$location_id), diff --git a/R/check_compatibility.R b/R/check_compatibility.R index 515306a..8691fc2 100644 --- a/R/check_compatibility.R +++ b/R/check_compatibility.R @@ -23,7 +23,7 @@ check_compatibility <- function(austraits) { compiled_by_traits.build <- austraits$metadata$related_identifiers %>% - util_list_to_df2() %>% + convert_list_to_df2() %>% dplyr::filter(relation_type == "isCompiledBy") |> dplyr::filter(stringr::str_detect(identifier, "github.com/traitecoevo/traits.build")) diff --git a/R/utils.R b/R/utils.R index 361c020..3fe101b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,18 +1,43 @@ -#' Convert dataframe to list +#' Convert dataframe to list #' -#' Convert a dataframe to a named list, useful when converting to yaml. +#' @description Convert a dataframe to a named list, +#' useful when converting a datafreme a to yaml. #' #' @param df A dataframe #' @return A (yaml) list #' @export -#' @examples util_df_to_list(dplyr::starwars) -util_df_to_list <- function(df) { +#' @examples convert_df_to_list(dplyr::starwars) +convert_df_to_list <- function(df) { attr(df, "out.attrs") <- NULL unname(lapply(split(df, seq_len(nrow(df))), as.list)) } +#' Convert list with single entries to dataframe +#' +#' @description Convert a list with a single level of entries to a dataframe, +#' useful when converting a yaml into a dataframe. +#' +#' @param my_list A list with single entries +#' @return A tibble with two columns +#' @export +#' @examples \dontrun{ +#' convert_list_to_df1(as.list(dplyr::starwars)[2]) +#' } +convert_list_to_df1 <- function(my_list) { + + for (f in names(my_list)) { + if (is.null(my_list[[f]])) + my_list[[f]] <- NA + } + + tibble::tibble(key = names(my_list), value = unname(unlist(my_list))) +} -#' Convert a list of lists to dataframe; requires that every list have same named elements. +#' Convert list of lists to dataframe +#' +#' @description Convert a list of lists to a dataframe, +#' useful when converting a multi-level yaml into a dataframe. +#' Function required that every list have same named elements. #' #' @param my_list A list of lists to dataframe #' @param as_character A logical value, indicating whether the values are read as character @@ -22,9 +47,9 @@ util_df_to_list <- function(df) { #' @examples demo_list1 <- list(word1 = "this", word2 = "is", word3 = "an", word4 = "example", word5 = "list") #' demo_list2 <- list(word1 = "and", word2 = "a", word3 = "second", word4 = "list", word5 = "also") #' combined_list <- list(demo_list1, demo_list2) -#' util_list_to_df2(combined_list) +#' convert_list_to_df2(combined_list) -util_list_to_df2 <- function(my_list, as_character = TRUE, on_empty = NA) { +convert_list_to_df2 <- function(my_list, as_character = TRUE, on_empty = NA) { if (is.null(my_list) || any(is.na(my_list)) || length(my_list) == 0) return(on_empty) @@ -35,7 +60,6 @@ util_list_to_df2 <- function(my_list, as_character = TRUE, on_empty = NA) { dplyr::bind_rows(lapply(my_list, tibble::as_tibble)) } - #' Notify user the function they are using is no longer support #' #' @param austraits diff --git a/man/util_df_to_list.Rd b/man/convert_df_to_list.Rd similarity index 53% rename from man/util_df_to_list.Rd rename to man/convert_df_to_list.Rd index 92dd004..a3e1cca 100644 --- a/man/util_df_to_list.Rd +++ b/man/convert_df_to_list.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/utils.R -\name{util_df_to_list} -\alias{util_df_to_list} +\name{convert_df_to_list} +\alias{convert_df_to_list} \title{Convert dataframe to list} \usage{ -util_df_to_list(df) +convert_df_to_list(df) } \arguments{ \item{df}{A dataframe} @@ -13,8 +13,9 @@ util_df_to_list(df) A (yaml) list } \description{ -Convert a dataframe to a named list, useful when converting to yaml. +Convert a dataframe to a named list, +useful when converting a datafreme a to yaml. } \examples{ -util_df_to_list(dplyr::starwars) +convert_df_to_list(dplyr::starwars) } diff --git a/man/convert_list_to_df1.Rd b/man/convert_list_to_df1.Rd new file mode 100644 index 0000000..b6387d9 --- /dev/null +++ b/man/convert_list_to_df1.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{convert_list_to_df1} +\alias{convert_list_to_df1} +\title{Convert list with single entries to dataframe} +\usage{ +convert_list_to_df1(my_list) +} +\arguments{ +\item{my_list}{A list with single entries} +} +\value{ +A tibble with two columns +} +\description{ +Convert a list with a single level of entries to a dataframe, +useful when converting a yaml into a dataframe. +} +\examples{ +\dontrun{ +convert_list_to_df1(as.list(dplyr::starwars)[2]) +} +} diff --git a/man/util_list_to_df2.Rd b/man/convert_list_to_df2.Rd similarity index 63% rename from man/util_list_to_df2.Rd rename to man/convert_list_to_df2.Rd index fae5878..a550d53 100644 --- a/man/util_list_to_df2.Rd +++ b/man/convert_list_to_df2.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/utils.R -\name{util_list_to_df2} -\alias{util_list_to_df2} -\title{Convert a list of lists to dataframe; requires that every list have same named elements.} +\name{convert_list_to_df2} +\alias{convert_list_to_df2} +\title{Convert list of lists to dataframe} \usage{ -util_list_to_df2(my_list, as_character = TRUE, on_empty = NA) +convert_list_to_df2(my_list, as_character = TRUE, on_empty = NA) } \arguments{ \item{my_list}{A list of lists to dataframe} @@ -17,11 +17,13 @@ util_list_to_df2(my_list, as_character = TRUE, on_empty = NA) tibble } \description{ -Convert a list of lists to dataframe; requires that every list have same named elements. +Convert a list of lists to a dataframe, +useful when converting a multi-level yaml into a dataframe. +Function required that every list have same named elements. } \examples{ demo_list1 <- list(word1 = "this", word2 = "is", word3 = "an", word4 = "example", word5 = "list") demo_list2 <- list(word1 = "and", word2 = "a", word3 = "second", word4 = "list", word5 = "also") combined_list <- list(demo_list1, demo_list2) -util_list_to_df2(combined_list) +convert_list_to_df2(combined_list) }