Skip to content

Commit

Permalink
replaced grepl with str_detect. changed order that nodes were sorted,…
Browse files Browse the repository at this point in the history
… since A0 is now _U and B0 is now _D. Dropped the need for %<>%
  • Loading branch information
KevinSee committed Aug 18, 2023
1 parent 9d044f2 commit 7511779
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions R/addParentChildNodes.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
#' @author Kevin See
#'
#' @param parent_child dataframe produced by `buildParentChild()`.
#' @param configuration a configuration dataframe, such as one built by `buildmy_config()`.
#' @param configuration a configuration dataframe, such as one built by `buildConfig()`.
#'
#' @import dplyr tidyr
#' @importFrom magrittr %<>%
#' @import dplyr tidyr stringr
#' @return NULL
#' @export
#' @examples addParentChildNodes()
Expand All @@ -26,31 +25,31 @@ addParentChildNodes = function(parent_child = NULL,
left_join(configuration %>%
select(node) %>%
distinct() %>%
mutate(site_code = if_else(grepl("_D$", node) &
mutate(site_code = if_else(stringr::str_detect(node, "_D$") &
nchar(node) >= 5,
str_remove(node, "_D"),
stringr::str_remove(node, "_D$"),
node),
site_code = if_else(grepl("_U$", site_code) &
site_code = if_else(stringr::str_detect(site_code, "_U$") &
nchar(site_code) >= 5,
str_remove(site_code, "_U"),
stringr::str_remove(site_code, "_U$"),
site_code)),
by = "site_code") %>%
distinct() %>%
arrange(site_code, desc(node)) %>%
arrange(site_code, node) %>%
group_by(site_code) %>%
mutate(n_nodes = n_distinct(node)) %>%
mutate(node_num = paste("node", 1:n(), sep = "_")) %>%
ungroup() %>%
left_join(parent_child %>%
select(matches("child")) %>%
rename(site_code = child) %>%
rlang::set_names(nm = str_remove,
rlang::set_names(nm = stringr::str_remove,
pattern = "child_") %>%
bind_rows(parent_child %>%
select(matches("parent")) %>%
distinct() %>%
rename(site_code = parent) %>%
rlang::set_names(nm = str_remove,
rlang::set_names(nm = stringr::str_remove,
pattern = "parent_")) %>%
distinct(),
by = "site_code")
Expand Down Expand Up @@ -115,22 +114,22 @@ addParentChildNodes = function(parent_child = NULL,
child) %>%
distinct()

if(sum(grepl("hydro", names(parent_child))) > 0) {
pc_nodes %<>%
left_join(node_long %>%
select(parent = node,
parent_hydro = hydro),
by = "parent") %>%
left_join(node_long %>%
select(child = node,
child_hydro = hydro),
by = "child") %>%
arrange(parent_hydro,
child_hydro)
if(sum(stringr::string_detect(names(parent_child), "hydro")) > 0) {

This comment has been minimized.

Copy link
@mackerman44

mackerman44 Aug 28, 2023

Collaborator

Ah, just caught this. It seems string_detect() was accidentally used, rather than str_detect(). I resolved this in my most recent commit.

pc_nodes <- pc_nodes %>%
left_join(node_long %>%
select(parent = node,
parent_hydro = hydro),
by = "parent") %>%
left_join(node_long %>%
select(child = node,
child_hydro = hydro),
by = "child") %>%
arrange(parent_hydro,
child_hydro)
}

if(sum(grepl("rkm", names(parent_child))) > 0) {
pc_nodes %<>%
if(sum(stringr::str_detect(names(parent_child), "rkm")) > 0) {
pc_nodes <- pc_nodes %>%
select(-matches('rkm')) %>%
left_join(node_long %>%
select(parent = node,
Expand All @@ -144,5 +143,5 @@ addParentChildNodes = function(parent_child = NULL,
select(any_of(names(parent_child))) %>%
distinct()
}
return(pc_nodes)
return(pc_nodes)
}

0 comments on commit 7511779

Please sign in to comment.