Hi again,

I must be misunderstanding something fundamental about how an S4 class
stored in a data.frame can be persuaded to print according to a desired
format. The help page says:

format.data.frame formats the data frame column by column, applying the
appropriate method of format for each column.

Here is my class:
> setClass("A",contains="character")
[1] "A"
> a <- new("A", "aaa")
> a
An object of class "A"
[1] "aaa"

I defined my format method to display in uppercase:
> setMethod("format","A", function(x, ...) toupper(x))
[1] "format"
> format(a)
An object of class "A"
[1] "AAA"

I looked at the S4 class definition for data.frame (getClass("data.frame"))
which prompted me to store the instance 'a' into a data.frame as follows:
> adf<-new("data.frame",list(a), names="a", row.names="1")
> adf
Object of class "data.frame"
    a
1 aaa

... not upper case. The str function clearly shows that the object was
successfully stored in the data.frame:
> str(adf)
'data.frame':   1 obs. of  1 variable:
'data.frame':   1 obs. of  1 variable:
Formal class 'data.frame' [package "methods"] with 4 slots
  ..@ .Data    :List of 1
  .. ..$ :Formal class 'A' [package ".GlobalEnv"] with 1 slots
  .. .. .. ..@ .Data: chr "aaa"
  ..@ names    : chr "a"
  ..@ row.names: chr "1"
  ..@ .S3Class : chr "data.frame"

Is there another step I should be taking so that "A"'s format method is
found and used?

Thank you,

Dan Murphy

        [[alternative HTML version deleted]]

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to