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

1
2
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.

Details

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

Value

a character string with at most width.cutoff characters.

Author(s)

Spencer Graves

See Also

paste substr nchar

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
##
## 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')

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.