warnings {base}R Documentation

Print Warning Messages


warnings and its print method print the variable last.warning in a pleasing form.



## S3 method for class 'warnings'
summary(object, ...)

## S3 method for class 'warnings'
print(x, tags,
      header = ngettext(n, "Warning message:\n", "Warning messages:\n"),
## S3 method for class 'summary.warnings'
print(x, ...)



arguments to be passed to cat (for warnings()).


a "warnings" object as returned by warnings().


a "warnings" or "summary.warnings" object.


if not missing, a character vector of the same length as x, to “label” the messages. Defaults to paste0(seq_len(n), ": ") for n >= 2 where n <- length(x).


a character string cat()ed before the messages are printed.


See the description of options("warn") for the circumstances under which there is a last.warning object and warnings() is used. In essence this is if options(warn = 0) and warning has been called at least once.

Note that the length(last.warning) is maximally getOption("nwarnings") (at the time the warnings are generated) which is 50 by default. To increase, use something like

  options(nwarnings = 10000)  

It is possible that last.warning refers to the last recorded warning and not to the last warning, for example if options(warn) has been changed or if a catastrophic error occurred.


warnings() returns an object of S3 class "warnings", basically a named list.

summary(<warnings>) returns a "summary.warnings" object which is basically the list of unique warnings (unique(object)) with a "counts" attribute, somewhat experimentally.


It is undocumented where last.warning is stored nor that it is visible, and this is subject to change.


Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

See Also



## NB this example is intended to be pasted in,
##    rather than run by example()
ow <- options("warn")
for(w in -1:1) {
   options(warn = w); cat("\n warn =", w, "\n")
   for(i in 1:3) { cat(i,"..\n"); m <- matrix(1:7, 3,4) }
## at the end prints all three warnings, from the 'option(warn = 0)' above
options(ow) # reset to previous, typically 'warn = 0'
tail(warnings(), 2) # see the last two warnings only (via '[' method)

## Often the most useful way to look at many warnings:

op <- options(nwarnings = 10000) ## <- get "full statistics"
x <- 1:36; for(n in 1:13) for(m in 1:12) A <- matrix(x, n,m) # There were 105 warnings ...
options(op) # revert to previous (keeping 50 messages by default)

[Package base version 3.5.0 Index]