Skip to content

S4 / S3 / Sweave problem

5 messages · Paul Gilbert, Martin Maechler, Brian Ripley

#
I'm not sure if this as a bug or something I am doing wrong. It occurs 
in both 2.7.0 and 2.6.2.

require("methods")

setClassUnion("OptionalPOSIXct",   c("POSIXct",   "NULL"))

setClass("TSmetax",
   representation(serIDs="character", ExtractionDate="OptionalPOSIXct" ))

setGeneric("TSmetax",
    def= function(x, ...) standardGeneric("TSmetax"))

setMethod("TSmetax",   signature(x="character"),
    definition= function(x, ...){
        new("TSmetax", serIDs=x, ExtractionDate=Sys.time())
        } )

 > z <- new("TSmetax", serIDs="whatever", ExtractionDate= NULL)
 > print(z)
An object of class "TSmetax"
Slot "serIDs":
[1] "whatever"

Slot "ExtractionDate":
NULL

Now if I make this an attribute of an S3 class an print, I get
 > zz <- ts(1:10)
 > attr(zz, "Meta") <- z
 > print(zz)
Time Series:
Start = 1
End = 10
Frequency = 1
  [1]  1  2  3  4  5  6  7  8  9 10
attr(,"Meta")
<S4 Type Object>
attr(,"serIDs")
[1] "whatever"
attr(,"ExtractionDate")
`NULL`
attr(,"class")
[1] "TSmetax"
attr(,"class")attr(,"package")
[1] ".GlobalEnv"
 >

The `NULL` shows up i
  shows up in the tex file generated by a vignette as
<soh>NULL<soh> which causes  tex (pdftex) to throw an  error.
(It also seems to mess up my mail tool, so I hope this goes through.)

Paul
====================================================================================

La version fran?aise suit le texte anglais.

------------------------------------------------------------------------------------

This email may contain privileged and/or confidential in...{{dropped:26}}
#
I'm not sure what the issue being asked about here is.  The peculiar 
printing I don't see and may well be a locale issue.  (Is this UTF-8 and 
TeX has not been told so?)

The other issue is that when printing an object with attributes, there is 
no consideration of S4 classes amongst the attributes.  That was either an 
undocumented design decision or an omission.  This is different from
$tsp
[1]  1 10  1

$class
[1] "ts"

$Meta
An object of class ?TSmetax?
Slot "serIDs":
[1] "whatever"

Slot "ExtractionDate":
NULL

since although attributes are internally a pairlist and printed by a 
special C function, attributes() returns a list.

It would be fairly easy to include a branch for S4 objects, but there are 
ambuiguities still over what they are (I wrote up a set of questions over 
a year ago about this).  But it would seem fairly safe to send them to 
show() in the same circumstances that autoprinting does for apparently S4 
objects.
On Sun, 4 May 2008, Paul Gilbert wrote:

            
My guess is that it was never intended that S4 objects be used as
attributes, in the same way that one of the unanswered questions is if S4
objects should be allowed to have attributes (other than slots).

  
    
#
Prof Brian Ripley wrote:
It seems to happen in locales UTF-8, POSIX and C.  I have not tried others.
Yes, I do have the impression that print() does not do exactly what one 
might like when it finds an S4 attribute in an S3 object. I hope this is 
not a design decision. It would make it very difficult to migrate toward 
S4 if it is not possible to include S4 objects in S3 objects this way.
====================================================================================

La version fran?aise suit le texte anglais.

------------------------------------------------------------------------------------

This email may contain privileged and/or confidential information, and the Bank of
Canada does not waive any related rights. Any distribution, use, or copying of this
email or the information it contains by other than the intended recipient is
unauthorized. If you received this email in error please delete it immediately from
your system and notify the sender promptly by email that you have done so. 

------------------------------------------------------------------------------------

Le pr?sent courriel peut contenir de l'information privil?gi?e ou confidentielle.
La Banque du Canada ne renonce pas aux droits qui s'y rapportent. Toute diffusion,
utilisation ou copie de ce courriel ou des renseignements qu'il contient par une
personne autre que le ou les destinataires d?sign?s est interdite. Si vous recevez
ce courriel par erreur, veuillez le supprimer imm?diatement et envoyer sans d?lai ?
l'exp?diteur un message ?lectronique pour l'aviser que vous avez ?limin? de votre
ordinateur toute copie du courriel re?u.
#

        
PaulG> Prof Brian Ripley wrote:
>> I'm not sure what the issue being asked about here is.  The peculiar 
    >> printing I don't see and may well be a locale issue.  (Is this UTF-8 and 
    >> TeX has not been told so?)

    PaulG> It seems to happen in locales UTF-8, POSIX and C.  I have not tried others.

    >> 
    >> The other issue is that when printing an object with attributes, there 
    >> is no consideration of S4 classes amongst the attributes.  That was 
    >> either an undocumented design decision or an omission.  This is 
    >> different from
    >> 
    >>> print(attributes(zz))
    >> $tsp
    >> [1]  1 10  1
    >> 
    >> $class
    >> [1] "ts"
    >> 
    >> $Meta
    >> An object of class ?TSmetax?
    >> Slot "serIDs":
    >> [1] "whatever"
    >> 
    >> Slot "ExtractionDate":
    >> NULL
    >> 
    >> since although attributes are internally a pairlist and printed by a 
    >> special C function, attributes() returns a list.
    >> 
    >> It would be fairly easy to include a branch for S4 objects, but there 
    >> are ambuiguities still over what they are (I wrote up a set of questions 
    >> over a year ago about this).  But it would seem fairly safe to send them 
    >> to show() in the same circumstances that autoprinting does for 
    >> apparently S4 objects.

    PaulG> Yes, I do have the impression that print() does not do exactly what one 
    PaulG> might like when it finds an S4 attribute in an S3 object. I hope this is 
    PaulG> not a design decision. It would make it very difficult to migrate toward 
    PaulG> S4 if it is not possible to include S4 objects in S3 objects this way.

I'm pretty sure that this has *not* been a design decision, but
rather an oversight till now.

To me, it seems even a "natural" oversight: If you are working
for fully designed classes, it seems awkward to simultaneously
use attributes of S3- (or "no class"-) objects.
I do understand though that there can be situations, in
particular migrations ones, where you want to do this ... and
should be allowed to and should see print() working.

Martin
>> On Sun, 4 May 2008, Paul Gilbert wrote:
>> 
    >>> I'm not sure if this as a bug or something I am doing wrong. It occurs 
    >>> in both 2.7.0 and 2.6.2.
    >> 
    >> My guess is that it was never intended that S4 objects be used as
    >> attributes, in the same way that one of the unanswered questions is if S4
    >> objects should be allowed to have attributes (other than slots).
    >> 
    >>> 
    >>> require("methods")
    >>> 
    >>> setClassUnion("OptionalPOSIXct",   c("POSIXct",   "NULL"))
    >>> 
    >>> setClass("TSmetax",
    >>> representation(serIDs="character", ExtractionDate="OptionalPOSIXct" ))
    >>> 
    >>> setGeneric("TSmetax",
    >>> def= function(x, ...) standardGeneric("TSmetax"))
    >>> 
    >>> setMethod("TSmetax",   signature(x="character"),
    >>> definition= function(x, ...){
    >>> new("TSmetax", serIDs=x, ExtractionDate=Sys.time())
    >>> } )
    >>> 
    >>>> z <- new("TSmetax", serIDs="whatever", ExtractionDate= NULL)
    >>>> print(z)
    >>> An object of class "TSmetax"
    >>> Slot "serIDs":
    >>> [1] "whatever"
    >>> 
    >>> Slot "ExtractionDate":
    >>> NULL
    >>> 
    >>> Now if I make this an attribute of an S3 class an print, I get
    >>>> zz <- ts(1:10)
    >>>> attr(zz, "Meta") <- z
    >>>> print(zz)
    >>> Time Series:
    >>> Start = 1
    >>> End = 10
    >>> Frequency = 1
    >>> [1]  1  2  3  4  5  6  7  8  9 10
    >>> attr(,"Meta")
    >>> <S4 Type Object>
    >>> attr(,"serIDs")
    >>> [1] "whatever"
    >>> attr(,"ExtractionDate")
    >>> `NULL`
    >>> attr(,"class")
    >>> [1] "TSmetax"
    >>> attr(,"class")attr(,"package")
    >>> [1] ".GlobalEnv"
    >>>> 
    >>> 
    >>> The `NULL` shows up i
    >>> shows up in the tex file generated by a vignette as
    >>> <soh>NULL<soh> which causes  tex (pdftex) to throw an  error.
    >>> (It also seems to mess up my mail tool, so I hope this goes through.)
    >> 
    >> 
    PaulG> ====================================================================================

    PaulG> La version fran?aise suit le texte anglais.

    PaulG> ------------------------------------------------------------------------------------

    PaulG> This email may contain privileged and/or confidential information, and the Bank of
    PaulG> Canada does not waive any related rights. Any distribution, use, or copying of this
    PaulG> email or the information it contains by other than the intended recipient is
    PaulG> unauthorized. If you received this email in error please delete it immediately from
    PaulG> your system and notify the sender promptly by email that you have done so. 

    PaulG> ------------------------------------------------------------------------------------

    PaulG> Le pr?sent courriel peut contenir de l'information privil?gi?e ou confidentielle.
    PaulG> La Banque du Canada ne renonce pas aux droits qui s'y rapportent. Toute diffusion,
    PaulG> utilisation ou copie de ce courriel ou des renseignements qu'il contient par une
    PaulG> personne autre que le ou les destinataires d?sign?s est interdite. Si vous recevez
    PaulG> ce courriel par erreur, veuillez le supprimer imm?diatement et envoyer sans d?lai ?
    PaulG> l'exp?diteur un message ?lectronique pour l'aviser que vous avez ?limin? de votre
    PaulG> ordinateur toute copie du courriel re?u.
    PaulG> ______________________________________________
    PaulG> R-devel at r-project.org mailing list
    PaulG> https://stat.ethz.ch/mailman/listinfo/r-devel
#
On Mon, 5 May 2008, Martin Maechler wrote:

            
But you do!  The issue is that S4 uses show() and not print(), and it was 
print() that is being called.  And the nub is
An object of class ?TSmetax?
Slot "serIDs":
[1] "whatever"

Slot "ExtractionDate":
NULL

calls show() whereas we have
<S4 Type Object>
attr(,"serIDs")
[1] "whatever"
attr(,"ExtractionDate")
`NULL`
attr(,"class")
[1] "TSmetax"
attr(,"class")attr(,"package")
[1] ".GlobalEnv"

This happens in print(zz) because printAttributes calls print() with a 
'digits' setting -- it has to because it does not know where it is called 
from.

It does seem reasonable for print(zz) to do as it did: the real problem is 
that show(zz) does not work correctly.

I've altered printAttributes in r-devel to call show() on S4 objects, but 
that does mean that arguments such as 'digits' will not be respected.
(There is potentially a problem if show() then calls print() which I have 
yet to analyse.)