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

set_attrs

Add attributes to an object


Description

Experimental lifecycle

Deprecated lifecycle

set_attrs() adds, changes, or zaps attributes of objects. Pass a single unnamed NULL argument to zap all attributes. For uncopyable types, use mut_attrs().

Usage

set_attrs(.x, ...)

mut_attrs(.x, ...)

Arguments

.x

An object to decorate with attributes.

...

<dynamic> A list of named attributes. Pass a single unnamed NULL argument to zap all attributes from .x.

Details

Unlike structure(), these setters have no special handling of internal attributes names like .Dim, .Dimnames or .Names.

Value

set_attrs() returns a modified shallow copy of .x. mut_attrs() invisibly returns the original .x modified in place.

Life cycle

These functions are deprecated since rlang 0.3.0.

Examples

set_attrs(letters, names = 1:26, class = "my_chr")

# Splice a list of attributes:
attrs <- list(attr = "attr", names = 1:26, class = "my_chr")
obj <- set_attrs(letters, splice(attrs))
obj

# Zap attributes by passing a single unnamed NULL argument:
set_attrs(obj, NULL)
set_attrs(obj, !!! list(NULL))

# Note that set_attrs() never modifies objects in place:
obj

# For uncopyable types, mut_attrs() lets you modify in place:
env <- env()
mut_attrs(env, foo = "bar")
env

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.