R: Create a default name for an R object

as_label {rlang} | R Documentation

## Create a default name for an R object

### Description

`as_label()` transforms R objects into a short, human-readable description. You can use labels to:

- Display an object in a concise way, for example to labellise axes in a graphical plot.

- Give default names to columns in a data frame. In this case, labelling is the first step before name repair.

See also `as_name()` for transforming symbols back to a string. Unlike `as_label()`, `as_string()` is a well defined operation that guarantees the roundtrip symbol -> string -> symbol.

In general, if you don't know for sure what kind of object you're dealing with (a call, a symbol, an unquoted constant), use `as_label()` and make no assumption about the resulting string. If you know you have a symbol and need the name of the object it refers to, use `as_string()`. For instance, use `as_label()` with objects captured with `enquo()` and `as_string()` with symbols captured with `ensym()`.

### Usage

    as_label(x)

### Arguments

`x` | An object.

### Transformation to string

- Quosures are squashed before being labelled.

- Symbols are transformed to string with `as_string()`.

- Calls are abbreviated.

- Numbers are represented as such.

- Other constants are represented by their type, such as `<dbl>` or `<data.frame>`.

Note that simple symbols should generally be transformed to strings with `as_name()`. Labelling is not a well defined operation and no assumption should be made about how the label is created. On the other hand, `as_name()` only works with symbols and is a well defined, deterministic operation.

### See Also

`as_name()` for transforming symbols back to a string deterministically.

### Examples

    # as_label() is useful with quoted expressions:
    as_label(expr(foo(bar)))
    as_label(expr(foobar))

    # It works with any R object. This is also useful for quoted
    # arguments because the user might unquote constant objects:
    as_label(1:3)
    as_label(base::list)

[Package rlang version 0.3.2 Index]