[R] Strange behavior with S3 class

philippe massicotte pmassicotte at hotmail.com
Mon Jan 18 12:59:12 CET 2016


Hi all.
For the development of a package I created a S3 class with a "setter" operator "<-".
Running the following code will create two objects: eem and eem2. Additionally, the class eem has a names.eem function used to retrieve the value of the sample field.
names.eem <- function(x, ...){  x$sample}
# First constructoreem1 <- function(sample){  eem <- list(sample = sample)  class(eem) <- "eem"  return(eem)}
# Second constructoreem2 <- function(sample){  eem <- list(sample = sample)  class(eem) <- "eem2"  return(eem)}
test1 <- eem1("justaname")test2 <- eem2("justaname")

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?
Thank you,Philippe  		 	   		  
	[[alternative HTML version deleted]]



More information about the R-help mailing list