[Rd] Odd behavior of symbol objects with classes/attributes
Winston Chang
winstonchang1 at gmail.com
Mon Jun 17 19:01:32 CEST 2013
I've been trying to add classes and attributes to symbol objects
(created with quote()), and the behavior is very strange, as
illustrated in the examples below.
If symbols aren't meant to have classes and attributes attached to
them, then perhaps R should throw errors when you attempt to do it?
# Using str() strips class from object
x <- quote(foo)
class(x) <- "bar"
str(x)
# Class 'bar' symbol foo
str(x)
# symbol foo
# Attempting to overwrite doesn't affect class.
# str() still strips class from the object.
x <- quote(foo)
class(x) <- "bar"
x <- quote(foo)
str(x)
# Class 'bar' symbol foo
str(x)
# symbol foo
# Changing class of one object affects other
x <- quote(foo)
y <- quote(foo)
class(x) <- "bar"
class(y)
# [1] "bar"
str(y)
# Class 'bar' symbol foo
str(y)
# symbol foo
str(x)
# symbol foo
# Changing attribute of one object affects other
# Unlike with class, str() doesn't cause other attributes to disappear
x <- quote(foo)
y <- quote(foo)
attr(x, "a") <- "bar"
str(y)
# length 1 foo
# - attr(*, "a")= chr "bar"
str(y)
# length 1 foo
# - attr(*, "a")= chr "bar"
str(quote(foo))
# length 1 foo
# - attr(*, "a")= chr "bar"
-Winston
More information about the R-devel
mailing list