diff --git a/DESCRIPTION b/DESCRIPTION index 9fd22ad..b18d5af 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -37,7 +37,7 @@ URL: https://github.com/KevinSee/PITcleanr, BugReports: https://github.com/KevinSee/PITcleanr/issues Encoding: UTF-8 LazyData: true -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Suggests: ggrepel, kableExtra, diff --git a/R/addParentChildNodes.R b/R/addParentChildNodes.R index 1c987ca..dadb9d5 100644 --- a/R/addParentChildNodes.R +++ b/R/addParentChildNodes.R @@ -8,6 +8,7 @@ #' #' @param parent_child dataframe produced by `buildParentChild()`. #' @param configuration a configuration dataframe, such as one built by `buildConfig()`. +#' @inheritParams buildConfig #' #' @import dplyr tidyr stringr #' @return NULL @@ -15,24 +16,44 @@ #' @examples addParentChildNodes() addParentChildNodes = function(parent_child = NULL, - configuration = NULL) { + configuration = NULL, + array_suffix = c("UD", + "UMD", + "A0B0")) { stopifnot(!is.null(parent_child), !is.null(configuration)) + array_suffix = match.arg(array_suffix) + # get the nodes for all site codes in the parent-child table - node_long = tibble(site_code = union(parent_child$child, + if(array_suffix %in% c("UD", "UMD")) { + node_site <- + configuration %>% + select(node) %>% + distinct() %>% + mutate(site_code = case_when(stringr::str_detect(node, "_D$") & + nchar(node) >= 5 ~ stringr::str_remove(node, "_D$"), + stringr::str_detect(node, "_U$") & + nchar(node) >= 5 ~ stringr::str_remove(node, "_U$"), + stringr::str_detect(node, "_M$") & + nchar(node) >= 5 ~ stringr::str_remove(node, "_M$"), + .default = node)) + } else { + node_site <- + configuration %>% + select(node) %>% + distinct() %>% + mutate(site_code = case_when(stringr::str_detect(node, "B0$") & + nchar(node) >= 5 ~ stringr::str_remove(node, "B0$"), + stringr::str_detect(node, "A0$") & + nchar(node) >= 5 ~ stringr::str_remove(node, "A0$"), + .default = node)) + } + + node_long <- + tibble(site_code = union(parent_child$child, parent_child$parent)) %>% - left_join(configuration %>% - select(node) %>% - distinct() %>% - mutate(site_code = if_else(stringr::str_detect(node, "_D$") & - nchar(node) >= 5, - stringr::str_remove(node, "_D$"), - node), - site_code = if_else(stringr::str_detect(site_code, "_U$") & - nchar(site_code) >= 5, - stringr::str_remove(site_code, "_U$"), - site_code)), + left_join(node_site, by = "site_code") %>% distinct() %>% arrange(site_code, node) %>% diff --git a/man/addParentChildNodes.Rd b/man/addParentChildNodes.Rd index 2f36f8e..b01a735 100644 --- a/man/addParentChildNodes.Rd +++ b/man/addParentChildNodes.Rd @@ -4,12 +4,21 @@ \alias{addParentChildNodes} \title{Add Nodes to Parent-Child Table} \usage{ -addParentChildNodes(parent_child = NULL, configuration = NULL) +addParentChildNodes( + parent_child = NULL, + configuration = NULL, + array_suffix = c("UD", "UMD", "A0B0") +) } \arguments{ \item{parent_child}{dataframe produced by `buildParentChild()`.} \item{configuration}{a configuration dataframe, such as one built by `buildConfig()`.} + +\item{array_suffix}{if \code{node_assign = "array"}, should nodes uses the suffixes of _U & _D (\code{UD}), +_U, _M, & _D (\code{UMD}), or _A0 & _B0 (\code{A0B0}) to denote upstream, middle, and downstream arrays, respectively. +Default is \code{UD}. If \code{array_suffix = "UD"} or \code{array_suffix = "A0B0"}, the middle array is grouped +with the upstream array.} } \description{ When some sites in the parent-child table have multiple nodes, this function