R/other.R

Defines functions paste2 niceprint

paste2 <- function(..., sep = "") {
    #' a revised `paste0` that will output NA for an idx if any of the variables has NA at that idx;
    #' modified from: 42- and Ben Bolker's answer here: https://stackoverflow.com/questions/13673894/suppress-nas-in-paste
    #' example:
    #'  foo <- c(LETTERS[1:3],NA, NA, NA)
    #'  bar <- c(NA,2:4, NA, NA)
    #'  baz <- c("a",NA,"c","d", 'd', NA)
    #'  paste2(foo, '_', bar, '__', baz)
    #'  # [1] NA        NA        "C_3_c" NA        NA        NA
    L <- list(...)
    na.rows <- rowSums(do.call(cbind, unname(lapply(L, is.na)))) > 0
    L <- lapply(L,function(x) {x[is.na(x)] <- ""; x})
    res <- gsub(paste0("(^", sep, "|", sep, "$)"), "",
         gsub(paste0(sep, sep), sep,
              do.call(paste, c(L, list(sep = sep))) ))
    is.na(res) <- na.rows
    return(res)
}


# print output with comment char at te beginning
niceprint <- function(x) cat(paste0('# ', capture.output(x)), sep = '\n')
mt1022/Rutils documentation built on May 25, 2019, 10:34 p.m.