R/fixedWidthCat.R

Defines functions sepInt numName fixedWidthCat trailingSpaces

Documented in fixedWidthCat numName sepInt

## helper function that counts trailing spaces in a string
trailingSpaces <- function(x, width){
    y <- gsub("^ *", "", x)
    tmp <- nchar(x)-nchar(y)
    if(tmp>=width)
        tmp <- min(0, width-nchar(y))
    return(tmp)
}


## This function captures the output that's generated by
## a show method for any R object and makes sure
## that lines are broken in case the output is too long
fixedWidthCat <- function(x, width=getOption("width")){
    output <-  gsub("\t", "     ", capture.output(x))
    longLines <- which(nchar(output)>width)
    for(l in longLines){
        ident <- trailingSpaces(output[l], width)
        output[l] <- strbreak(output[l], exdent=ident, collapse="\n")
    }
    cat(output, sep="\n", collapse="")
}



## match numbers from 1 to 13 to textual names
numName <- function(x){
    if(x>13)
        return(x)
    nums <- c("one", "two", "three", "four", "five", "six", "seven", "eight",
              "nine", "ten", "eleven", "twelve", "thirteen")
    nums[x]
}


## print integers with comma as 1000 separator
sepInt <- function(x){
    if(x<1000)
        return(x)
    sprintf("%d,%03d", x %/% 1000, x %% 1000)
}

Try the BiocCaseStudies package in your browser

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

BiocCaseStudies documentation built on Nov. 8, 2020, 5:12 p.m.