Skip to content

Commit

Permalink
edits to df_to_list and list_to_df functions
Browse files Browse the repository at this point in the history
- rename all with `convert_` instead of `util_`
- add `convert_list_to_df1` from traits.build, so all 3 functions in one location
  • Loading branch information
ehwenk committed Nov 11, 2024
1 parent eb07fe8 commit 30fe41b
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 23 deletions.
5 changes: 3 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,"%>%")
Expand Down
2 changes: 1 addition & 1 deletion R/bind_databases.R
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion R/check_compatibility.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down
40 changes: 32 additions & 8 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 30 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L28-L30

Added lines #L28 - L30 were not covered by tests
}

tibble::tibble(key = names(my_list), value = unname(unlist(my_list)))

Check warning on line 33 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L33

Added line #L33 was not covered by tests
}

#' 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
Expand All @@ -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)
Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions man/util_df_to_list.Rd → man/convert_df_to_list.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/convert_list_to_df1.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions man/util_list_to_df2.Rd → man/convert_list_to_df2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 30fe41b

Please sign in to comment.