[R] Strange behavior with S3 class

S Ellison S.Ellison at LGCGroup.com
Mon Jan 18 15:28:08 CET 2016


> This is "bugged":
>   > str(test1)List of 1$ justaname: chr "justaname"- attr(*, "class")= chr "eem"
> This is ok:    > str(test2)List of 1$ sample: chr "justaname"- attr(*, "class")= chr
> "eem2"
> It seems that override of the "<-" is causing problem since in str(test1) the
> variable name is not preserved.
> Any idea?

Compare 
str(test1)
# List of 1
#  $ justaname: chr "justaname"
#  - attr(*, "class")= chr "eem"

str(unclass(test1))
# List of 1
#  $ sample: chr "justaname"

That says the internal variable name is preserved but while it still has the 'eem' class the name is reported differently.
That is presumably because you defined a 'names' function for objects of class 'eem' which returns the contents of $sample as the name, not "sample". Looks like str is using your names.eem method to extract the name of each component of your object, which is kind of what redefining 'names' asks for. And just to demonstrate that str does indeed use the current class's names method: 

names.eem <- function(x, ...){  "AnotherName"}
str(test1)
# List of 1
#  $ AnotherName: chr "justaname"
#  - attr(*, "class")= chr "eem"

So it isn't your '<-', it's because you overrode 'names'


S Ellison



*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list