Learn R Programming

Ecfun (version 0.2-0)

createMessage: Compose a message as a single substring from a character vector

Description

This is a utility function to make it easier to automatically compose informative error and warning messages without using too many characters.

Usage

createMessage(x, width.cutoff=45, default='x', collapse='; ', 
              endchars='...')

Arguments

x

input for paste

width.cutoff

maximum number of characters from x to return in a single string. This differs from the width.cutoff argument in deparse in that the output include here considers endchars, not part of deparse.

default

character string to return if nchar(x) = 0.

collapse

collapse argument for paste

endchars

a character string to indicate that part of the input string(s) was truncated.

Value

a character string with at most width.cutoff characters.

Details

x. <- paste(..., collapse='; ') nchx <- nchar(x.) maxch <- (maxchar-nchar(endchar)) if(nchx>maxch) x2 <- substring(x., 1, maxch) x. <- paste0(x2, endchar)

See Also

paste substr nchar

Examples

Run this code
# NOT RUN {
##
## 1.  typical use 
##
tstVec <- c('Now', 'is', 'the', 'time')
msg <- createMessage(tstVec, 9, collapse=':', 
                     endchars='//')
# }
# NOT RUN {
all.equal(msg, 'Now:is://')
# }
# NOT RUN {
##
## 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...'
# }
# NOT RUN {
all.equal(msg0, msg.)
# }
# NOT RUN {
##
## 3.  default 
##
y <- createMessage(character(3), default='y') 
# }
# NOT RUN {
all.equal(y, 'y')
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab