Hi Bart,

If fails because you choose to name the list elements. If you don't do
that, it should work:

d<-list(a,b)
do.call("stack", d )

If they will likely be named you could remove them first in a function like
this one:

stacklist <- function(x) {
   names(x) <- NULL
   do.call("stack", x )
 }

d<-list(a=a,b=b)
stacklist( d )


Robert


On Sat, Jun 16, 2012 at 2:05 PM, Bart <bart@njn.nl> wrote:

>  Thank you very much for your reply Robert.
> It works well, the problem starts occurring when we have a list of
> ourClass objects. Consider this bit of code after loading the raster
> package and running the example code you send.
>
> d<-list(a=a, b=b)
> stack(d)# does not work because signature x=list from raster is used
> do.call("stack", d )# does not work because there is no argument x
>
> The following examples work but are a bit longer and are less intuitive
> names(d)<-NULL
> do.call("stack", d )# works because the first argument for is taken for is
> and is of ourClass
> names(d)<-c("x","y")
> do.call("stack", d )# works because  x signature ourClass is found
>
> Of cause this example is a bit trivial but i think the use case of having
> a list of object is quite common for example resulting from an lapply
> statement. Often these list are so long that writing something like
> stack(a,b) is not possible anymore and renaming the list is a bit difficult
> to understand.
>
> Best, Bart
>
>
>
> On 06/16/2012 06:54 AM, Robert J. Hijmans wrote:
>
> Bart, I think you can do something along these lines:
>
>   # create your base class
> setClass('ourClass', representation (x = 'numeric'))
>
>  # create your stack class
> setClass('ourStack', representation (z = 'list'))
>
>  if (!isGeneric("stack")) {
>  setGeneric("stack", function(x, ...)
>  standardGeneric("stack"))
> }
>
>  setMethod("stack", signature(x='ourClass'),
> function(x, ...) {
>  x <- c(list(x), list(...))
>  i <- sapply(x, function(j) inherits(j, 'ourClass'))
>  if (!all(i)) { warning("removed ", sum(!i), " object(s) not inheriting
> from 'ourClass'" ) }
>
>  s <- new('ourStack')
>  s@z <- x[i]
>  return(s)
>  }
> )
>
>  a <- new('ourClass', x=10)
> b <- new('ourClass', x=15)
>
>  s1 <- stack(a,b,a)
> s2 <- stack(a,b,10,b,15)
>
>
>  Best, Robert
>
>
>  On Fri, Jun 15, 2012 at 7:44 AM, Bart <bart@njn.nl> wrote:
>
>> Dear list,
>>
>> Currently I'm developing an r package for movement analysis, it is quite
>> heavily based on the raster and sp package. One of the things we want to do
>> is combine multiple objects into one combined object, much like stack
>> stacks multiple rasters. We thought of using the in raster defined generic
>> stack function for ease of use but this poses a problem.
>>
>> First calling it with a list is not possible since that method is already
>> set in the raster package, then i thought about the following way.
>>
>> setMethod("stack", signature(x='ourClass'), ....
>>
>> and than call it as following
>>
>> do.call('stack', list_of_objects_of_our_class)
>>
>> I think this works in principle fine the problem is that there needs to
>> be an argument called x to the call to stack, while our list does not
>> necessarily contain this. One way around is removing the names from the
>> list but i would like to avoid that also to keep indexing simple.
>>
>> Is there any other way that we could use the stack function without
>> renaming our list?
>>
>> I hope the problem is a bit clear, thanks in advance
>>
>> Bart
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo@r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>
>

	[[alternative HTML version deleted]]

