Skip to content

Commit

Permalink
added an option in the addParentChildNodes function to accomodate dif…
Browse files Browse the repository at this point in the history
…ferent array suffixes in the configuration file
  • Loading branch information
KevinSee committed Oct 22, 2024
1 parent 00eb21c commit a5a2e1f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
47 changes: 34 additions & 13 deletions R/addParentChildNodes.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,52 @@
#'
#' @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
#' @export
#' @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) %>%
Expand Down
11 changes: 10 additions & 1 deletion man/addParentChildNodes.Rd

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

0 comments on commit a5a2e1f

Please sign in to comment.