R/swap_in_uris.r

Defines functions lookup_new_name swap_in_uris

#' @title swap uris for column or list names
#' @description Swap in URIS for names using the config's uri lookup
#' @param annotations tibble, dataframe, list with names.
#' @param uri_lookup list in the form of name=uri.
#' @return annotations object with names replaced by uris from lookup when applicable
swap_in_uris <- function(annotations, uri_lookup=list()) {
  new_names <- sapply(names(annotations), lookup_new_name, lu_list=uri_lookup, USE.NAMES = F)
  if(!is.null(annotations)){ names(annotations) <- new_names }
  return(annotations)
}

#' @title lookup new name
#' @describeIn swap_in_uris Lookup replacement for name
#' @param x name to lookup
#' @param lu_list lookup list to check x against names for value of new name
#' @return new name or original x if x not present in names of lookup list.
lookup_new_name <- function(x, lu_list){
  lu_flat <- unlist(lu_list)
  res <- lu_flat[x]
  ifelse(is.na(res), x, res)
}
Display-Lab/bit-stomach documentation built on Dec. 12, 2021, 1:35 a.m.