R/utils.R

str_tpl_format <- function(tpl, l){
  if("list" %in% class(l)){
    listToNameValue <- function(l){
      mapply(function(i,j) list(name = j, value = i), l, names(l), SIMPLIFY = FALSE)
    }
    f <- function(tpl,l){
      gsub(paste0("{",l$name,"}"), l$value, tpl, fixed = TRUE)
    }
    return( Reduce(f,listToNameValue(l), init = tpl))
  }
  if("data.frame" %in% class(l)){
    myTranspose <- function(x) lapply(1:nrow(x), function(i) lapply(l, "[[", i))
    return( unlist(lapply(myTranspose(l), function(l, tpl) str_tpl_format(tpl, l), tpl = tpl)) )
  }
}

match_replace <- function (v, dic, force = TRUE){
  matches <- dic[[2]][match(v, dic[[1]])]
  out <- matches
  if (!force)
    out[is.na(matches)] <- v[is.na(matches)]
  out
}
jpmarindiaz/RNeo4jUtils documentation built on May 8, 2019, 7:56 p.m.