Skip to content

Commit

Permalink
Add taxonTree() #43
Browse files Browse the repository at this point in the history
  • Loading branch information
brownag committed Mar 9, 2023
1 parent 7c56eb6 commit f0da5c4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ Suggests:
rmarkdown,
markdown,
soilDB,
ape
ape,
data.tree
RoxygenNote: 7.2.3
Roxygen: list(markdown = TRUE)
VignetteBuilder: knitr
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export(parent_level)
export(parse_family)
export(preceding_taxon_codes)
export(relative_taxon_code_position)
export(taxonTree)
export(taxon_code_to_taxon)
export(taxon_to_level)
export(taxon_to_taxon_code)
Expand Down
34 changes: 34 additions & 0 deletions R/taxonTree.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

#' Create a `data.tree` Object from Taxon Names
#'
#' @param taxon A vector of taxon names
#' @param root Label for root node. Default: `"Soil Taxonomy"`; `NULL` for "unrooted" tree.
#' @param verbose Print tree output? Default: `TRUE`
#' @param ... Additional arguments to `data.tree::as.Node.data.frame()`
#'
#' @return A data.tree object (invisibly). A text representation of the tree is printed when `verbose=TRUE`.
#' @export
#' @examplesIf !inherits(requireNamespace("data.tree", quietly = TRUE), 'try-error')
#' @examples
#' taxonTree(c("hapludults", "hapludalfs"))
taxonTree <- function(taxon, root = "Soil Taxonomy", verbose = TRUE, ...) {
if (!requireNamespace("data.tree")) {
stop("package 'data.tree' is required", call. = FALSE)
}

x <- unique(do.call('c', getChildTaxa(taxon)))
y <- getTaxonAtLevel(x, level = c("order", "suborder", "greatgroup", "subgroup"))
y <- y[order(taxon_to_taxon_code(x)),]
y <- y[complete.cases(y),]

y$pathString <- with(y, paste0(ifelse(length(root) > 0, paste0(root, "/"), ""),
order, "/", suborder, "/", greatgroup, "/", subgroup))
n <- data.tree::as.Node(y, ...)
if (isTRUE(verbose)) {
print(n, limit = NULL)
}

invisible(n)
}

taxonTree("alfisols")
28 changes: 28 additions & 0 deletions man/taxonTree.Rd

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

0 comments on commit f0da5c4

Please sign in to comment.