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

Martin Morgan mtmorgan at fhcrc.org
Mon Jun 29 15:19:32 CEST 2009


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/
> ___________________________________________________________________________



More information about the R-devel mailing list