# Create a list containing objects of different classes
example = list(1,
as.integer(1:3),
bit64::as.integer64(1),
diag(5),
array(0,c(2,3,5)),
list(a=1,b='c'),
data.frame(x=1, y=1:10, char=LETTERS[1:10])
)
# Loop over all the objects to check if the custom attribute is correctly extracted.
for (i in seq_along(example)) {
x = example[[i]]
attr(x,'MyAttribute') = 'Test'
cat(sprintf('Class = %s, Custom attribute(s) = %s\n',class(x)[1],
paste(names(userattributes(x)), collapse = ', ')))
}
Run the code above in your browser using DataLab