attributes: Get or set attributes of an object (vectorized)

Description Usage Arguments Details Value Examples

Description

%@% is an infix-form wrapper for the attr function.

Usage

1
2
3
4
5
6
7
x %@% attribute

x %try@% attribute

SetAttribute(x, attribute, value)

x %@% attribute <- value

Arguments

x

An object with attributes

attribute

A character string that names an attribute of the object.

value

a new value for an attribute

Details

A Trial object is a data-frame of looking data, and metadata about the trial like the images onscreen or duration of the carrier phrase are stored in attributes attached to the data-frame. The functions described below making working with attributes a little easier.

%@% is a utility function for getting the value of an attribute from an R object. This is a wrapper for the built-in function attr. %@%.list applies %@% onto each object in a list of R objects. Therefore, %@% cannot grab attributes of lists, unless they are lists within lists. This is weird; maybe I should fix this.

Attributes can be set two ways:

  1. x <- SetAttribute(x, attribute, value)

  2. x %@% attribute <- value

If the object is a TrialList then then assignment is vectorized, meaning that each object (i.e., Trial) is updated with the value (which is recycled as needed).

%try@% replaces NULL values with NA if the attribute is missing.

Value

The value in the attribute slot of object, or the values of the attribute for each element element in a list.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
Cat <- function(name, owner) {
 structure(list(Name = name), Owner = owner, class = "cat")
}

kiki <- Cat("Kiki", "AML")
nooper <- Cat("Nooper", "TJM")

kiki %@% "Owner"
# [1] "AML"

list(nooper, kiki) %@% "Owner"
# [1] "TJM" "AML"

# Setting/overwriting a attribute
nooper %@% "Owner" <- NA
list(nooper, kiki) %@% "Owner"
# [1] NA    "AML"

tjmahr/lookr documentation built on May 31, 2019, 3:41 p.m.