Learn R Programming

rlang (version 0.2.1)

set_attrs: Add attributes to an object

Description

set_attrs() adds, changes, or zaps attributes of objects. Pass a single unnamed NULL as 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.

...

A list of named attributes. These have explicit splicing semantics. Pass a single unnamed NULL to zap all attributes from .x.

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 experimental, expect API changes.

  • set_attrs() should probably set the attributes as a whole. Another function with add_ prefix would be in charge of adding an attribute to the set.

  • mut_attrs() should be renamed to use the poke_ prefix. Also it may be useful to allow any kind of objects, not just non-copyable ones.

Details

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

Examples

Run this code
# NOT RUN {
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
# }

Run the code above in your browser using DataLab