R: Display last warnings

last_warnings {rlang} | R Documentation

## Display last warnings

### Description

`last_warnings()` and `last_messages()` return a list of all warnings and messages that occurred during the last R command.

`global_entrace()` must be active in order to log the messages and warnings.

By default the warnings and messages are printed with a simplified backtrace, like `last_error()`. Use `summary()` to print the conditions with a full backtrace.

### Usage

    last_warnings(n = NULL)

    last_messages(n = NULL)

### Arguments

`n` | How many warnings or messages to display. Defaults to all.

### Examples

Enable backtrace capture with `global_entrace()`:

    global_entrace()

Signal some warnings in nested functions. The warnings inform about which function emitted a warning but they don't provide information about the call stack:

    f <- function() { warning("foo"); g() }
    g <- function() { warning("bar", immediate. = TRUE); h() }
    h <- function() warning("baz")

    f()
    #> Warning in g() : bar
    #> Warning messages:
    #> 1: In f() : foo
    #> 2: In h() : baz

Call `last_warnings()` to see backtraces for each of these warnings:

    last_warnings()
    #> [[1]]
    #> <warning/rlang_warning>
    #> Warning in `f()`: foo
    #> Backtrace:
    #>  1. global f()
    #>
    #> [[2]]
    #> <warning/rlang_warning>
    #> Warning in `g()`: bar
    #> Backtrace:
    #>  1. global f()
    #>  2. global g()
    #>
    #> [[3]]
    #> <warning/rlang_warning>
    #> Warning in `h()`: baz
    #> Backtrace:
    #>  1. global f()
    #>  2. global g()
    #>  3. global h()

To get a full backtrace, use `summary()`. In this case the full backtraces do not include more information:

    summary(last_warnings())
    #> [[1]]
    #> <warning/rlang_warning>
    #> Warning in `f()`:
    #> foo
    #> Backtrace:
    #>     x
    #>  1. \-global f()
    #>
    #> [[2]]
    #> <warning/rlang_warning>
    #> Warning in `g()`:
    #> bar
    #> Backtrace:
    #>     x
    #>  1. \-global f()
    #>  2.   \-global g()
    #>
    #> [[3]]
    #> <warning/rlang_warning>
    #> Warning in `h()`:
    #> baz
    #> Backtrace:
    #>     x
    #>  1. \-global f()
    #>  2.   \-global g()
    #>  3.     \-global h()

### See Also

`last_error()`

[Package rlang version 1.0.2 Index]