createMessage {Ecfun} | R Documentation |
This is a utility function to make it easier to automatically compose informative error and warning messages without using too many characters.
createMessage(x, width.cutoff=45, default='x', collapse='; ', endchars='...')
x |
input for |
width.cutoff |
maximum number of characters from x to return
in a single string. This differs from the |
default |
character string to return if nchar(x) = 0. |
collapse |
|
endchars |
a character string to indicate that part of the input string(s) was truncated. |
x. <- paste(..., collapse='; ') nchx <- nchar(x.) maxch <- (maxchar-nchar(endchar)) if(nchx>maxch) x2 <- substring(x., 1, maxch) x. <- paste0(x2, endchar)
a character string with at most width.cutoff
characters.
Spencer Graves
## ## 1. typical use ## tstVec <- c('Now', 'is', 'the', 'time') msg <- createMessage(tstVec, 9, collapse=':', endchars='//') all.equal(msg, 'Now:is://') ## ## 2. in a function ## tstFn <- function(cl)createMessage(deparse(cl), 9) Cl <- quote(plot(1:3, y=4:6, col='red', main='Title')) msg0 <- tstFn(Cl) # check msg. <- 'plot(1...' all.equal(msg0, msg.) ## ## 3. default ## y <- createMessage(character(3), default='y') all.equal(y, 'y')