Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

as_name

Extract names from symbols


Description

as_name() converts symbols to character strings. The conversion is deterministic. That is, the roundtrip symbol -> name -> symbol always gets the same result.

  • Use as_name() when you need to transform a symbol to a string to refer to an object by its name.

  • Use as_label() when you need to transform any kind of object to a string to represent that object with a short description.

Expect as_name() to gain name-repairing features in the future.

Note that rlang::as_name() is the opposite of base::as.name(). If you're writing base R code, we recommend using base::as.symbol() which is an alias of as.name() that follows a more modern terminology (R types instead of S modes).

Usage

as_name(x)

Arguments

x

A string or symbol, possibly wrapped in a quosure. If a string, the attributes are removed, if any.

Value

A character vector of length 1.

See Also

as_label() for converting any object to a single string suitable as a label. as_string() for a lower-level version that doesn't unwrap quosures.

Examples

# Let's create some symbols:
foo <- quote(foo)
bar <- sym("bar")

# as_name() converts symbols to strings:
foo
as_name(foo)

typeof(bar)
typeof(as_name(bar))

# as_name() unwraps quosured symbols automatically:
as_name(quo(foo))

rlang

Functions for Base Types and Core R and 'Tidyverse' Features

v0.4.11
MIT + file LICENSE
Authors
Lionel Henry [aut, cre], Hadley Wickham [aut], mikefc [cph] (Hash implementation based on Mike's xxhashlite), Yann Collet [cph] (Author of the embedded xxHash library), RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.