R/codePaths_to_namedVector.R

Defines functions codePaths_to_namedVector

Documented in codePaths_to_namedVector

#' Get a vector to find the full paths based on the leaf code identifier
#'
#' This function names a vector with the leaf code
#' using the `codeTreeMarker` stored in the [rock::opts] object as marker.
#'
#' @param x A vector of code paths.
#'
#' @return The named vector of code paths.
#' @export
#'
#' @examples codePaths_to_namedVector(
#'   c("codes>reason>parent_feels",
#'     "codes>reason>child_feels")
#' );
codePaths_to_namedVector <- function(x) {
  if (is.null(x)) {
    return(x);
  }
  codeTreeMarker <- rock::opts$get("codeTreeMarker");
  leafs <-
    gsub(
      paste0(
        "^.*",
        codeTreeMarker,
        "([a-zA-Z0-9_]+)$"
      ),
      "\\1",
      x
    );
  res <- x;
  names(res) <- leafs;
  return(res);
}

Try the rock package in your browser

Any scripts or data that you put into this service are public.

rock documentation built on Dec. 28, 2022, 1:55 a.m.