Learn R Programming

Ecfun (version 0.1-4)

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 endchar
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
##
## 1.  typical use 
##
tstVec <- c('Now', 'is', 'the', 'time')
msg <- createMessage(tstVec, 9, collapse=':', 
                     endchars='//')
stopifnot(
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...'
stopifnot(
all.equal(msg0, msg.)
)

##
## 3.  default 
##
y <- createMessage(character(3), default='y') 
stopifnot(
all.equal(y, 'y')
)

Run the code above in your browser using DataLab