R/oc_edges_dtf.R

Defines functions oc_edges_dtf

Documented in oc_edges_dtf

#' Create a dataframe of edges
#'
#' @param .data A dataframe of organisation data
#' @param nodes A daataframe of nodes calculated based on the organisation data
#'
#' @return A dataframe of edges for the organisation
#'
oc_edges_dtf <- function(.data, nodes){

  edges <- .data %>%
    dplyr::left_join(nodes, by ="Team Member") %>% # join the node id's for people
    dplyr::rename(from = .data$id) %>%
    dplyr::left_join(nodes, by = c("Manager" = "Team Member")) %>%  # join the node id's for managers
    dplyr::rename(to = .data$id) %>%
    dplyr::mutate(
      id = dplyr::row_number(),
      rel = NA # required for certain graph types
    ) %>%
    dplyr::mutate(color = dplyr::case_when(
      tolower(`Reporting Line`) == "solid" ~ "black",
      tolower(`Reporting Line`) == "dotted" ~ "grey",
      TRUE ~ "orange"
    )) %>%
    dplyr::mutate(
      penwidth = dplyr::case_when(
        `Reporting Line` == "Solid" ~ 2, # double width
        `Reporting Line` == "Dotted" ~ 1, # single width
        TRUE ~ 1
      )
    ) %>%

    dplyr::filter(!is.na(.data$to)) %>% # remove the artificially added edges above top nodes
    dplyr::select(.data$id, .data$from, .data$to, .data$rel, .data$color, .data$penwidth)

  return(edges)

}
ThomUK/orgcharter documentation built on Feb. 21, 2022, 12:03 a.m.