Skip to content

Commit

Permalink
Updating print function and messaging style (#122)
Browse files Browse the repository at this point in the history
* Changed class name and generic name

* Rename summarise_austraits to summarise_database

* Updated format for print message for v<5

* Updated start up message
  • Loading branch information
fontikar authored Nov 14, 2024
1 parent 09f96f2 commit f16e41e
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 67 deletions.
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(print,austraits)
S3method(print,traits.build)
export("%>%")
export(as_wide_table)
export(bind_databases)
Expand Down Expand Up @@ -30,7 +30,7 @@ export(plot_locations)
export(plot_site_locations)
export(plot_trait_distribution_beeswarm)
export(separate_trait_values)
export(summarise_austraits)
export(summarise_database)
export(summarise_trait_means)
export(trait_pivot_longer)
export(trait_pivot_wider)
Expand Down
2 changes: 1 addition & 1 deletion R/austraits-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
if(getRversion() >= "2.15.1") utils::globalVariables(c(".", "dplyr::n()"))

.onAttach <- function(libname, pkgname) {
packageStartupMessage("Thanks for showing interest in `austraits`! Please consider citing this package - citation('austraits')")
cli::cli_inform("Thanks for showing interest in `austraits`! Please consider citing this package - citation('austraits')", class = "packageStartupMessage")
}

#' @keywords internal
Expand Down
2 changes: 1 addition & 1 deletion R/extract_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ extract_data <- function(database, table = NA, col, col_value) {
"taxa","contributors","sources","definitions","schema", "metadata","build_info")]

# Assign class
attr(ret, "class") <- "austraits"
attr(data, "class") <- "traits.build"

}

Expand Down
2 changes: 1 addition & 1 deletion R/load_austraits.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ load_austraits <- function(doi = NULL, version = NULL, path = "data/austraits",
data <- readRDS(file_nm)

# Assign class
attr(data, "class") <- "austraits"
attr(data, "class") <- "traits.build"

data
}
Expand Down
108 changes: 74 additions & 34 deletions R/print.austraits.R
Original file line number Diff line number Diff line change
@@ -1,49 +1,89 @@
#' @title Generic for outputting a nice summary for austraits objects
#'
#' @name print.austraits
#' @param database traits.build database
#' @name print.traits.build
#' @param x traits.build database
#' @param \dots passed to print
#'
#' @return nicely printed table
#' @export

print.austraits <- function(database, ...){

print.traits.build <- function(x, ...){
# browser()
# Setting up
version <- database$build_info$version %>% as.character()
nrecords <- nrow(database$traits)
nspecies <- unique(database$traits$taxon_name) %>% length()
ntraits <- unique(database$traits$trait_name) %>% length()

cat("This is version",
version,
"of austraits!\n",
"\nThis object contains a total of",
nrecords, "records",
"for", nspecies, "taxa and",
ntraits, "traits.\n")
version <- x$build_info$version %>% as.character()
nrecords <- nrow(x$traits)
nspecies <- unique(x$traits$taxon_name) %>% length()
ntraits <- unique(x$traits$trait_name) %>% length()


if(package_version(version) <= '3.0.2'){
if(package_version(version) < '5.0.0'){

# Setting up
database_name <- x$definitions$austraits$description

cat("\nThis object is a 'list' with the following components:\n\n",
database$definitions$austraits$elements %>%
purrr::map(~.x[["description"]]) %>%
as.vector() %>%
sprintf("- `%s`: %s", names(.), .) %>%
paste(collapse="\n")
)
fun <- function() {
cli::cli_h1("This database contains a total of {nrecords} records, for {nspecies} taxa and {ntraits} traits.")

cli::cli_h2("This object is a 'list' with the following components:")
cli::cli_div(theme = list(span.emph = list(color = "forestgreen")))
cli::cli_ul()
cli::cli_li("{.emph traits}: A table containing measurements of traits.")
cli::cli_li("{.emph locations}: A table containing observations of location/site characteristics associated with information in `traits`. Cross referencing between the two dataframes is possible using combinations of the variables `dataset_id`, `location_name`.")
cli::cli_li("{.emph contexts}: A table containing observations of contextual characteristics associated with information in `traits`. Cross referencing between the two dataframes is possible using combinations of the variables `dataset_id`, `link_id`, and `link_vals`.")
cli::cli_li("{.emph methods}: A table containing details on methods with which data were collected, including time frame and source. Cross referencing with the `traits` table is possible using combinations of the variables `dataset_id`, `trait_name`.")
cli::cli_li("{.emph excluded_data}: A table of data that did not pass quality test and so were excluded from the master dataset.")
cli::cli_li("{.emph taxonomic_updates}: A table of all taxonomic changes implemented in the construction of AusTraits. Changes are determined by comapring against the APC (Australian Plant Census) and APNI (Australian Plant Names Index).")
cli::cli_li("{.emph taxa}: A table containing details on taxa associated with information in `traits`. This information has been sourced from the APC (Australian Plant Census) and APNI (Australian Plant Names Index) and is released under a CC-BY3 license.")
cli::cli_li("{.emph definitions}: A copy of the definitions for all tables and terms. Information included here was used to process data and generate any documentation for the study.")
cli::cli_li("{.emph contributors}: A table of people contributing to each study.")
cli::cli_li("{.emph sources}: Bibtex entries for all primary and secondary sources in the compilation.")
cli::cli_li("{.emph build_info}: A description of the computing environment used to create this version of the dataset, including version number, git commit and R session_info.")
cli::cli_end()

cli::cli_alert_info("To access a component, try using the $ e.g. austraits$traits")
}

fun()
} else{
cat("\nThis object is a 'list' with the following components:\n\n",
database$schema$austraits$elements %>%
purrr::map(~.x[["description"]]) %>%
as.vector() %>%
sprintf("- `%s`: %s", names(.), .) %>%
paste(collapse="\n")
)
database_name <- x$metadata$title

traits.build_version <- x$metadata$related_identifiers |>
convert_list_to_df2() |>
dplyr::filter(resource_type == "software") |>
dplyr::pull(version)

fun <- function() {
cli::cli_h1("This is {version} of {database_name}!")

cli::cli_bullets(c(
"i" = "This database is built using traits.build version {traits.build_version}",
"i" = "This database contains a total of {nrecords} records, for {nspecies} taxa and {ntraits} traits."
)
)

cli::cli_h2("This object is a 'list' with the following components:")
cli::cli_div(theme = list(span.emph = list(color = "forestgreen")))
cli::cli_ul()
cli::cli_li("{.emph traits}: A table containing measurements of traits.")
cli::cli_li("{.emph locations}: A table containing observations of location/site characteristics associated with information in `traits`. Cross referencing between the two dataframes is possible using combinations of the variables `dataset_id`, `location_name`.")
cli::cli_li("{.emph contexts}: A table containing observations of contextual characteristics associated with information in `traits`. Cross referencing between the two dataframes is possible using combinations of the variables `dataset_id`, `link_id`, and `link_vals`.")
cli::cli_li("{.emph methods}: A table containing details on methods with which data were collected, including time frame and source. Cross referencing with the `traits` table is possible using combinations of the variables `dataset_id`, `trait_name`.")
cli::cli_li("{.emph excluded_data}: A table of data that did not pass quality test and so were excluded from the master dataset.")
cli::cli_li("{.emph taxonomic_updates}: A table of all taxonomic changes implemented in the construction of AusTraits. Changes are determined by comapring against the APC (Australian Plant Census) and APNI (Australian Plant Names Index).")
cli::cli_li("{.emph taxa}: A table containing details on taxa associated with information in `traits`. This information has been sourced from the APC (Australian Plant Census) and APNI (Australian Plant Names Index) and is released under a CC-BY3 license.")
cli::cli_li("{.emph contributors}: A table of people contributing to each study.")
cli::cli_li("{.emph sources}: Bibtex entries for all primary and secondary sources in the compilation.")
cli::cli_li("{.emph definitions}: A copy of the definitions for all tables and terms. Information included here was used to process data and generate any documentation for the study.")
cli::cli_li("{.emph schema}: A copy of the schema for all tables and terms. Information included here was used to process data and generate any documentation for the study.")
cli::cli_li("{.emph metadata}: Metadata associated with the dataset, including title, creators, license, subject, funding sources.")
cli::cli_li("{.emph build_info}: A description of the computing environment used to create this version of the dataset, including version number, git commit and R session_info.")
cli::cli_end()

cli::cli_alert_info("To access a component, try using the $ e.g. austraits$traits")
}

fun()
}


cat("\n\nTo access a component, try using the $ e.g. austraits$traits")
}


18 changes: 9 additions & 9 deletions R/summarise_austraits.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
#' @title Summarise counts for a particular variable of interest
#'
#' @name summarise_austraits
#' @name summarise_database
#' @param database traits.build database (list object)
#' @param var variable you use wish to see summary of (trait_name, genus, family)
#'
#' @return dataframe of unique levels of variable with counts and percentage
#' @export
#' @examples
#' \dontrun{
#' summarise_austraits(database = austraits, "trait_name")
#' summarise_austraits(database = austraits, "family")
#' summarise_database(database = austraits, "trait_name")
#' summarise_database(database = austraits, "family")
#' }


summarise_austraits <- function(database, var){
summarise_database <- function(database, var){

if(!var %in% c("trait_name", "family", "genus")){
stop(paste0("Print summary for ", var, " has not been implemented! see examples)"))
}

switch(var,
trait_name = summarise_austraits_traits(database, var),
genus = summarise_austraits_taxa(database, var),
family = summarise_austraits_taxa(database, var)
trait_name = summarise_database_traits(database, var),
genus = summarise_database_taxa(database, var),
family = summarise_database_taxa(database, var)
)
}

#' @noRd
#' @keywords internal

summarise_austraits_traits <-function(database, var) {
summarise_database_traits <-function(database, var) {

ret <-
database[["traits"]] %>%
Expand Down Expand Up @@ -60,7 +60,7 @@ summarise_austraits_traits <-function(database, var) {
#' @noRd
#' @keywords internal

summarise_austraits_taxa <-function(database, var) {
summarise_database_taxa <-function(database, var) {

#Join taxonomic info
database <- database %>% join_taxa()
Expand Down
8 changes: 4 additions & 4 deletions man/print.austraits.Rd → man/print.traits.build.Rd

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

10 changes: 5 additions & 5 deletions man/summarise_austraits.Rd → man/summarise_database.Rd

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

20 changes: 10 additions & 10 deletions tests/testthat/test-summarise_austraits.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ not_supported_austraits <- list(austraits_3.0.2_lite, austraits_4.2.0_lite)


test_that("Function works", {
expect_visible(austraits_5.0.0_lite %>% summarise_austraits("family"))
expect_visible(austraits_5.0.0_lite %>% summarise_austraits("genus"))
expect_visible(austraits_5.0.0_lite %>% summarise_austraits("trait_name"))
expect_visible(austraits_5.0.0_lite %>% summarise_database("family"))
expect_visible(austraits_5.0.0_lite %>% summarise_database("genus"))
expect_visible(austraits_5.0.0_lite %>% summarise_database("trait_name"))
})


test_that("Throws errors", {
expect_error(austraits_5.0.0_lite %>% summarise_austraits("observation_id"))
expect_error(austraits_5.0.0_lite %>% summarise_austraits("trait"))
expect_error(austraits_5.0.0_lite %>% summarise_austraits("unit"))
expect_error(austraits_5.0.0_lite %>% summarise_austraits("source"))
expect_error(austraits_5.0.0_lite %>% summarise_database("observation_id"))
expect_error(austraits_5.0.0_lite %>% summarise_database("trait"))
expect_error(austraits_5.0.0_lite %>% summarise_database("unit"))
expect_error(austraits_5.0.0_lite %>% summarise_database("source"))

})

test_that("Output correct", {
family <- austraits_5.0.0_lite %>% summarise_austraits("family")
genus <- austraits_5.0.0_lite %>% summarise_austraits("genus")
trait_nm <- austraits_5.0.0_lite %>% summarise_austraits("trait_name")
family <- austraits_5.0.0_lite %>% summarise_database("family")
genus <- austraits_5.0.0_lite %>% summarise_database("genus")
trait_nm <- austraits_5.0.0_lite %>% summarise_database("trait_name")

expect_length(family, 5)
expect_length(genus, 5)
Expand Down

0 comments on commit f16e41e

Please sign in to comment.