From wolski at molgen.mpg.de  Fri Sep 10 15:39:17 2004
From: wolski at molgen.mpg.de (Wolski)
Date: Fri Sep 10 15:39:26 2004
Subject: [Rd] as(object,"list")<- error In .mergeAttrs() ??
In-Reply-To: <413F12EB.5F76BBA5@research.bell-labs.com>
References: <200409071617210704.0504144D@mail.math.fu-berlin.de>
	<413DD80C.2D5EA9C9@research.bell-labs.com>
	<200409081219020786.0B1B546F@mail.math.fu-berlin.de>
	<413F12EB.5F76BBA5@research.bell-labs.com>
Message-ID: <200409101539170921.1455535A@mail.math.fu-berlin.de>

Hi!

I am open to any arbitrary way of extending a list. It does not matter to me how complicated it gets. I do it once for my class.
What matters to me is what I can do with objects extending from list. Because this is what I am doing often.
Hence,  further proposals even more complicated are wellcome.

The problem with as(mylist,"list") I would say we sorted out temporarily. Thanks.

Whats next are the following set of examples that does not work.
At least one of the following calls fails with any of thy proposed implementation patterns on version of R1.9.1 or R2.0.0.


#Example of manipulating the list part of the object. 
as(mylist,"list") <- lapply(mylist,"+",3)
as(mylist,"list") <- mylist[2:4] 					#was working on 1.9.1 patched. Does not work on 2.0.0 alpha *
as(mylist,"list") <- as(mylist,"listNamed")[2:4]			#was working on 1.9.1 patched. Does not work on 2.0.0 alpha.*


#what does not work is assigning shorter lists without names. Seems like a bug. Or is there an explanation.
as(mylist,"list")<-as.list(1:5) 					#was working on 1.9.1. Is working on 2.0.0 even if the list has no names.
as(mylist,"list")<-as.list(1:4)					#is not working on 1.9.1 2.0.0

*A little different implementation of inheritance.

The samples above almost work, except the last two on 1.9.1 with one of the implementation patterns for inheritance.
Is intended to support such assignments automatically?

Almost non of them is working with the newest version of R.2.0.0 as you can easily reproduce running the code belowe.

As I understand I can provide by myself an assignemnt replace function by setAs. 
(I got all the examples above working working in this way on 1.9.1 
-> using the property that as long as shorter list has names has names it can be assigned and the following setAs function).

	,replace=function(from,value)
	{
		if(is.null(attributes(value)$names))
			attributes(value)$names<-character(0)

		from@.Data <- value
		from@names<-names(value)
		from	
	}

It was not easy because  .mergeAttrs()  was complaining for a while.
Now there is NO chance to build this work arround. Because now the assignment does not work for lists with names of shorter length.

I dont think that it is neccessary as it is now (?) that a class which extends a basic class has already an appropriate as<- method. They do not work.
Its seems quite easy provide an setAs method or setIs method. But not for a class extending from "list". Probably I am naive.

As I realise that this assignments will work on R2.0.0, I happily also realise that inheritance from old type classes is probably not important.
I can provide a method as.list for myclass and so myclass should work with all possible functions working on lists because they call "as.list" first in most cases.

Sincerely yours
Eryk.

The not working code example.
setClass("listNamed", contains="list",representation(names="character"))

setAs("listNamed", "list", function(from) {
   if(strict) class(from) <- "list"
   from
   })

setMethod("show"
	  ,"listNamed",
          def = function(object) {
              cat("An object of class ", class(object), "\n", sep="")
              show(as(object, "list"))
          })
                                                                                                                                                                                                                                            
                                        #NAIVE inheritance from list.
                                        #define cass

setClass("Mlist"
         ,representation(info="character") #uniq names have to be unique?
         ,contains="listNamed"
         )
         
tmp <- as.list(1:4)
names(tmp) <- letters[1:4]
mylist<-new("Mlist" , tmp , info="numeric" )
mylist
                                                                                                                                                                                                                                            
#Example of manipulating the list part of the object. 
as(mylist,"list") <- lapply(mylist,"+",3)
as(mylist,"list") <- mylist[2:4]
as(mylist,"list") <- as(mylist,"listNamed")[2:4]


#what does not work is assigning shorter lists without names. Seems like a bug. Or is there an explanation.
as(mylist,"list")<-as.list(1:5)
as(mylist,"list")<-as.list(1:4)


setAs("listNamed", "list", function(from) {
   if(strict) class(from) <- "list"
   from
   }
   ,replace=function(from,value)
   {
   	print("replace")
	print(class(value))
   	from@.Data<- value
   	from@names<- names(value)
	from
   }
   )

#Example of manipulating the list part of the object. 
as(mylist,"list") <- lapply(mylist,"+",3)
as(mylist,"list") <- mylist[2:4]
as(mylist,"list") <- as(mylist,"listNamed")[2:4]


#what does not work is assigning shorter lists without names. Seems like a bug. Or is there an explanation.
as(mylist,"list")<-as.list(1:5)
as(mylist,"list")<-as.list(1:4)


[Rd] as(object,"list")<- error In .mergeAttrs() ??
Wolski