From mtmorgan at fhcrc.org  Mon Jun 29 15:19:32 2009
From: mtmorgan at fhcrc.org (Martin Morgan)
Date: Mon, 29 Jun 2009 06:19:32 -0700
Subject: [Rd] S4 and connection slot [Sec=Unclassified]
In-Reply-To: <42C7503E0B99F248B06BC65E7D2B4E020122C50FB2AB@EX2K7-CCR.AAD.GOV.AU>
References: <42C7503E0B99F248B06BC65E7D2B4E020122C50FB2AB@EX2K7-CCR.AAD.GOV.AU>
Message-ID: <4A48BF64.7070004@fhcrc.org>

Troy Robertson wrote:
> Right you are Martin,
> 
> When you simplify things right down your code below works.
> 
> I had kept getting the error below:
> 
> Error in validObject(.Object) :
>   invalid class "KrillEnvironment" object: class for slot "datafileConn" ("file")
> 
> But now realise that it is when the class extends '.environment' and passes through the .xData slot:

I think the problem now is that the object is not initialized properly,
specifically that the 'datafileConn' slot is NULL, but should be 'file'.
This is true of the simpler case as well

> setClass("Element", representation=representation(datafileConn="file"))
[1] "Element"
> validObject(new("Element"))
Error in validObject(new("Element")) :
  invalid class "Element" object: invalid object for slot "datafileConn"
in class "Element": got class "NULL", should be or extend class "file"

datafileConn needs to be assigned appropriately in the class prototype
(but then all instances would inherit the same connection) or in the
initialize method.

I'm not sure that including a connection in a slot is going to be a good
idea, though -- a connection has reference-like semantics, so you can
end up with multiple objects pointing to the same connection, Also when
an object is garbage collected the connection will not be closed
automatically.

Martin

> --------------------------------
> setClass("Element",representation=representation(datafileConn="file"), contains=".environment")
> 
> setMethod("initialize", "Element",
>   function(.Object,.xData=new.env(parent=emptyenv())) {
>     .Object <- callNextMethod(.Object, .xData=.xData)
>     return(.Object)
>   }
> )
> 
> s <- new("Element")
> --------------------------------
> 
> Not sure why that changes things though?
> 
> Troy
> 
>> -----Original Message-----
>> From: Martin Morgan [mailto:mtmorgan at fhcrc.org]
>> Sent: Monday, 29 June 2009 2:21 PM
>> To: Troy Robertson
>> Subject: Re: [Rd] S4 and connection slot [Sec=Unclassified]
>>
>> Troy Robertson wrote:
>>> Hi all,
>>>
>>>
>>>
>>> I am having a problem trying to declare a slot representation to hold a
>> file connection.
>>> With V2.8.0 I had been using:
>>>
>>>
>>>
>>> setClass("Element",
>>>
>>>     representation(
>>>
>>>         [other slots removed],
>>>
>>>         datafileConn    = setOldClass(c("file","connection"))
>>>
>>>     )
>>>
>>> )
>>>
>> I think
>>
>> setOldClass(c("file", "connection"))
>>
>> setClass("Element",
>>          representation=representation(datafileConn="file"))
>>
>> getClass("file")
>> getClass("Element")
>>
>> but I don't usually use S3 classes so I'm not sure...
>>
>> Martin
>>
>>>
>>> This resulted in a warning but still ran okay.
>>>
>>>
>>>
>>> No however with V2.9.0 I am getting an error:
>>>
>>>
>>>
>>> Error in representation(state = "list", initialState = "list", polygons
>> = "list",  :
>>>   element 12 of the representation was not a single character string
>>>
>>>
>>>
>>> How can I declare a slot of the appropriate class?  The strings
>> "connection", "file" fail.
>>>
>>>
>>> Troy
>>>
>>>
>>>
>>>
>>>
>>> Troy Robertson
>>>
>>> Database and Computing Support Provider
>>>
>>> Southern Ocean Ecosystems, ERM/Fish
>>>
>>> Australian Antarctic Division
>>>
>>> Channel Highway, Kingston 7050
>>>
>>> PH: 03 62323571
>>>
>>> Troy.Robertson at aad.gov.au
>>>
>>>
>>>
>>>
>>>
>> __________________________________________________________________________
>> _
>>>     Australian Antarctic Division - Commonwealth of Australia
>>> IMPORTANT: This transmission is intended for the addressee only. If you
>> are not the
>>> intended recipient, you are notified that use or dissemination of this
>> communication is
>>> strictly prohibited by Commonwealth law. If you have received this
>> transmission in error,
>>> please notify the sender immediately by e-mail or by telephoning +61 3
>> 6232 3209 and
>>> DELETE the message.
>>>         Visit our web site at http://www.antarctica.gov.au/
>>>
>> __________________________________________________________________________
>> _
>>>       [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-devel at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> ___________________________________________________________________________
> 
>     Australian Antarctic Division - Commonwealth of Australia
> IMPORTANT: This transmission is intended for the addressee only. If you are not the
> intended recipient, you are notified that use or dissemination of this communication is
> strictly prohibited by Commonwealth law. If you have received this transmission in error,
> please notify the sender immediately by e-mail or by telephoning +61 3 6232 3209 and
> DELETE the message.
>         Visit our web site at http://www.antarctica.gov.au/
> ___________________________________________________________________________



[Rd] S4 and connection slot [Sec=Unclassified]
Martin Morgan