Skip to content

Commit

Permalink
Avoid tidyselect warnings - avoid superseded functions
Browse files Browse the repository at this point in the history
See  #102.
Use  pkgname::pkgfunction (see #86)
  • Loading branch information
damianooldoni committed Jun 26, 2024
1 parent 4c9a86b commit 87f93d6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 77 deletions.
6 changes: 0 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export(visualize_pathways_level1)
export(visualize_pathways_level2)
export(visualize_pathways_year_level1)
export(visualize_pathways_year_level2)
importFrom(assertable,assert_colnames)
importFrom(assertthat,assert_that)
importFrom(assertthat,is.date)
importFrom(dplyr,"%>%")
Expand All @@ -38,18 +37,13 @@ importFrom(dplyr,group_by)
importFrom(dplyr,inner_join)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,mutate_if)
importFrom(dplyr,pull)
importFrom(dplyr,rename)
importFrom(dplyr,rename_at)
importFrom(dplyr,rowwise)
importFrom(dplyr,sample_n)
importFrom(dplyr,select)
importFrom(dplyr,starts_with)
importFrom(dplyr,ungroup)
importFrom(dplyr,vars)
importFrom(purrr,map2_chr)
importFrom(purrr,map_chr)
importFrom(purrr,pmap_dfr)
importFrom(readr,read_tsv)
importFrom(readr,write_tsv)
Expand Down
101 changes: 48 additions & 53 deletions R/get_table_pathways.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@
#'
#' @return a data.frame
#' @export
#' @importFrom dplyr %>% filter distinct mutate mutate_if group_by count ungroup
#' rowwise sample_n pull select rename_at as_tibble .data
#' @importFrom assertthat assert_that
#' @importFrom assertable assert_colnames
#' @importFrom stringr str_c
#' @importFrom purrr pmap_dfr map_chr
#' @importFrom dplyr %>% .data
#' @examples
#' \dontrun{
#' library(readr)
Expand Down Expand Up @@ -94,100 +89,100 @@ get_table_pathways <- function(df,
"Not Chordata"
)
# initial input checks
assert_that(is.data.frame(df), msg = "df is not a data frame.")
assertthat::assert_that(is.data.frame(df), msg = "df is not a data frame.")
if (!is.null(category)) {
assert_that(is.character(category),
assertthat::assert_that(is.character(category),
msg = paste0(
"Category has to be a character. One of: ",
paste(categories, collapse = ", "),
"."
)
)
assert_that(category %in% categories,
assertthat::assert_that(category %in% categories,
msg = paste0(
"Category not correct. Choose one of: ",
paste(categories, collapse = ", "),
"."
)
)
}
assert_colnames(df, kingdom_names, only_colnames = FALSE)
assert_that(is.character(kingdom_names),
assertable::assert_colnames(df, kingdom_names, only_colnames = FALSE)
assertthat::assert_that(is.character(kingdom_names),
msg = "Parameter 'kingdom_names' should be a character."
)
assert_that(is.numeric(n_species),
assertthat::assert_that(is.numeric(n_species),
msg = "Parameter 'n_species' should be a number."
)
assert_that(n_species > 0,
assertthat::assert_that(n_species > 0,
msg = "Parameter 'n_species' should be a positive number."
)
assert_that(n_species == as.integer(n_species),
assertthat::assert_that(n_species == as.integer(n_species),
msg = "Parameter 'n_species' should be an integer."
)
if (!is.null(from)) {
assert_that(is.numeric(from),
assertthat::assert_that(is.numeric(from),
msg = "Parameter 'from' should be a number (year)."
)
assert_that(from > 0,
assertthat::assert_that(from > 0,
msg = "Parameter 'from' should be a positive number."
)
assert_that(from == as.integer(from),
assertthat::assert_that(from == as.integer(from),
msg = "Parameter 'from' should be an integer."
)
assert_that(from <= as.numeric(substr(Sys.Date(), start = 1, stop = 4)),
assertthat::assert_that(from <= as.numeric(substr(Sys.Date(), start = 1, stop = 4)),
msg = paste0(
"Invalid year in 'from'. ",
"Choose a year smaller than ",
substr(Sys.Date(), start = 1, stop = 4)
)
)
assert_that(is.character(first_observed),
assertthat::assert_that(is.character(first_observed),
msg = "Column 'first_observed' should be a character."
)
assert_colnames(df, first_observed, only_colnames = FALSE)
assertable::assert_colnames(df, first_observed, only_colnames = FALSE)
}

assert_colnames(df, species_names, only_colnames = FALSE)
assert_that(is.character(species_names),
assertable::assert_colnames(df, species_names, only_colnames = FALSE)
assertthat::assert_that(is.character(species_names),
msg = "Parameter 'species_names' should be a character."
)

# convert factors to characters (in case stringsAsFactors = TRUE)
if (any(map_chr(names(df), ~ class(df[[.]])) == "factor")) {
if (any(purrr::map_chr(names(df), ~ class(df[[.]])) == "factor")) {
warning("Factors are converted to characters.")
df <-
df %>%
mutate_if(is.factor, as.character)
dplyr::mutate_if(is.factor, as.character)
}
# rename to default column name
df <-
df %>%
rename_at(vars(kingdom_names), ~"group") %>%
rename_at(vars(species_names), ~"taxa_names")
dplyr::rename(group = !!kingdom_names) %>%
dplyr::rename(taxa_names = !!species_names)
if (!is.null(from)) {
df <-
df %>%
rename_at(vars(first_observed), ~"first_observed")
dplyr::rename(first_observed = !!first_observed)
}
# handle asymmetric category system (Chordata, Not Chordta are not kingdoms)
if (!is.null(category)) {
if (!category %in% c("Chordata", "Not Chordata")) {
filtered_data <- df %>% filter(.data$group == category)
filtered_data <- df %>% dplyr::filter(.data$group == category)
} else {
# check parameter phylum
assert_that(is.character(phylum_names),
assertthat::assert_that(is.character(phylum_names),
msg = "Parameter 'phylum_names' should be a character."
)
assert_colnames(df, phylum_names, only_colnames = FALSE)
df <-
df %>%
rename_at(vars(phylum_names), ~"phylum_group")
dplyr::rename(phylum_group = !!phylum_names)
if (category == "Chordata") {
filtered_data <- df %>% filter(.data$phylum_group == category)
filtered_data <- df %>% dplyr::filter(.data$phylum_group == category)
} else {
filtered_data <- df %>%
filter(.data$group == "Animalia") %>%
filter(.data$phylum_group != "Chordata")
dplyr::filter(.data$group == "Animalia") %>%
dplyr::filter(.data$phylum_group != "Chordata")
}
}
} else {
Expand All @@ -197,41 +192,41 @@ get_table_pathways <- function(df,
if (!is.null(from)) {
filtered_data <-
filtered_data %>%
filter(.data$first_observed >= from)
dplyr::filter(.data$first_observed >= from)
}
# Handle NAs, "unknown" and hierarchy (1st and 2nd level)
preprocess_data <-
filtered_data %>%
# Handle NAs, "unknown" and hierarchy (1st and 2nd level)
mutate(pathway_level1 = ifelse(is.na(.data$pathway_level1) |
dplyr::mutate(pathway_level1 = ifelse(is.na(.data$pathway_level1) |
.data$pathway_level1 == "",
"unknown",
.data$pathway_level1
)) %>%
mutate(pathway_level2 = ifelse(.data$pathway_level1 != "unknown" &
dplyr::mutate(pathway_level2 = ifelse(.data$pathway_level1 != "unknown" &
!is.na(.data$pathway_level2) & .data$pathway_level2 != "",
.data$pathway_level2,
"unknown"
))
# Create groups based on pathway level1 and level2
preprocess_data <-
preprocess_data %>%
distinct(
dplyr::distinct(
.data$taxa_names, .data$pathway_level1, .data$pathway_level2
) %>%
group_by(.data$pathway_level1, .data$pathway_level2)
dplyr::group_by(.data$pathway_level1, .data$pathway_level2)

# Assess size of sample per group
pathway_data <-
preprocess_data %>%
count() %>%
rowwise() %>%
mutate(size_sample = ifelse(n > n_species,
dplyr::count() %>%
dplyr::rowwise() %>%
dplyr::mutate(size_sample = ifelse(n > n_species,
n_species, n
))
# Make df with sample species
samples <-
pmap_dfr(
purrr::pmap_dfr(
list(
pathway_data$pathway_level1,
pathway_data$pathway_level2,
Expand All @@ -240,46 +235,46 @@ get_table_pathways <- function(df,
function(p1, p2, s) {
set_species <-
preprocess_data %>%
filter(.data$pathway_level1 == p1) %>%
filter(.data$pathway_level2 == p2)
dplyr::filter(.data$pathway_level1 == p1) %>%
dplyr::filter(.data$pathway_level2 == p2)
if (s < nrow(preprocess_data)) {
examples <- sample_n(set_species, s)
examples <- dplyr::sample_n(set_species, s)
} else {
examples <- set_species
}
examples <-
examples %>%
pull(.data$taxa_names)
dplyr::pull(.data$taxa_names)

tibble(examples = str_c(examples, collapse = ", ")) %>%
mutate(
dplyr::tibble(examples = stringr::str_c(examples, collapse = ", ")) %>%
dplyr::mutate(
pathway_level1 = as.character(p1),
pathway_level2 = as.character(p2)
)
}
)
# No samples
if (length(names(samples)) == 0) {
samples <- tibble(
samples <- dplyr::tibble(
examples = character(),
pathway_level1 = character(),
pathway_level2 = character()
)
}
# Join pathways and samples together
if (nrow(pathway_data) == 0) {
tibble(
dplyr::tibble(
pathway_level1 = character(0),
pathway_level2 = character(0),
n = integer(0),
examples = character(0)
)
} else {
pathway_data %>%
left_join(samples,
dplyr::left_join(samples,
by = c("pathway_level1", "pathway_level2")
) %>%
select(-.data$size_sample) %>%
ungroup()
dplyr::select(-"size_sample") %>%
dplyr::ungroup()
}
}
36 changes: 18 additions & 18 deletions tests/testthat/test-output_get_table_pathways.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ testthat::test_that("Basic usage: default values", {
# except order of examples which is randomized by default
testthat::expect_equal(
pathways_default %>%
dplyr::select(-.data$examples),
dplyr::select(-"examples"),
pathways_default_from_factors %>%
dplyr::select(-.data$examples)
dplyr::select(-"examples")
)
# large input, more pathways, more rows
testthat::expect_true(nrow(pathways_default_large) > nrow(pathways_default))
Expand All @@ -134,8 +134,8 @@ testthat::test_that("Basic usage: default values", {
# same content of dfs. No examples take into account as they are randomly
# selected
testthat::expect_equal(
pathways_default %>% dplyr::select(-.data$examples),
output_test_df_basic %>% dplyr::select(-.data$examples)
pathways_default %>% dplyr::select(-"examples"),
output_test_df_basic %>% dplyr::select(-"examples")
)
})

Expand Down Expand Up @@ -187,26 +187,26 @@ testthat::test_that("Use with 'category'", {
testthat::expect_true(all(class(pathways_plants) == class(pathways_animals)))
testthat::expect_true(all(names(pathways_plants) == names(pathways_animals)))
testthat::expect_equal(
pathways_animals %>% dplyr::select(-.data$examples),
output_test_df_animals %>% dplyr::select(-.data$examples)
pathways_animals %>% dplyr::select(-"examples"),
output_test_df_animals %>% dplyr::select(-"examples")
)
testthat::expect_equal(
pathways_chordata %>% dplyr::select(-.data$examples),
output_test_df_chordata %>% dplyr::select(-.data$examples)
pathways_chordata %>% dplyr::select(-"examples"),
output_test_df_chordata %>% dplyr::select(-"examples")
)
testthat::expect_equal(
pathways_not_chordata %>% dplyr::select(-.data$examples),
output_test_df_not_chordata %>% dplyr::select(-.data$examples)
pathways_not_chordata %>% dplyr::select(-"examples"),
output_test_df_not_chordata %>% dplyr::select(-"examples")
)
# same result if kingdom col has different name than default
testthat::expect_equal(
pathways_animals %>% dplyr::select(-.data$examples),
pathways_animals_2 %>% dplyr::select(-.data$examples)
pathways_animals %>% dplyr::select(-"examples"),
pathways_animals_2 %>% dplyr::select(-"examples")
)
# same result if phylum col has different name than default
testthat::expect_equal(
pathways_not_chordata %>% dplyr::select(-.data$examples),
pathways_not_chordata_2 %>% dplyr::select(-.data$examples)
pathways_not_chordata %>% dplyr::select(-"examples"),
pathways_not_chordata_2 %>% dplyr::select(-"examples")
)
})

Expand All @@ -229,9 +229,9 @@ testthat::test_that("Use with 'from'", {
# from is earlier than any first_observed value
testthat::expect_equal(
pathways_1000 %>%
dplyr::select(-.data$examples),
dplyr::select(-"examples"),
output_test_df_basic %>%
dplyr::select(-.data$examples)
dplyr::select(-"examples")
)
testthat::expect_true(sum(pathways_1000$n) > sum(pathways_2012$n))
testthat::expect_true(sum(pathways_2012$n) > sum(pathways_2018$n))
Expand All @@ -257,9 +257,9 @@ testthat::test_that("Use with 'n_species'", {
)
testthat::expect_equal(
pathways_n_species_10 %>%
dplyr::select(-.data$examples),
dplyr::select(-"examples"),
get_table_pathways(input_test_df) %>%
dplyr::select(-.data$examples)
dplyr::select(-"examples")
)
testthat::expect_true(all(purrr::map_lgl(
pathways_n_species_10$examples,
Expand Down

0 comments on commit 87f93d6

Please sign in to comment.