emmanuelparadis / ape

Analysis of Phylogenetics and Evolution
https://emmanuelparadis.github.io/
GNU General Public License v2.0
53 stars 11 forks source link

`[<-.multiPhylo` does not support empty `[]` #36

Closed ms609 closed 3 years ago

ms609 commented 3 years ago

The syntax myList[] <- X typically allows entries of a list to be modified without overwriting attributes.

Compare:

myList <- structure(list(1, 2, 3, 4, 5), class = 'myClass')
class(myList) # "myClass"

myList[] <- sapply(myList, `*`, 2)
class(myList) # attributes retained

myList <- lapply(myList, `*`, 2)
class(myList) # attributes lost

However, the [<-.multiPhylo function fails when a TipLabel attribute is present and no parameter is passed, i.e.

trees <- c(read.tree(text = '(a, b, c);'), read.tree(text = '(a, b, c);'))
DoSomething <- function(tree) return(tree)

# This drops the multiPhylo class
bad <- lapply(trees, DoSomething)

# Keep multiPhylo class: This works okay
trees[] <- lapply(trees, DoSomething)

# But if trees have a TipLabels attribute, we fail:
attr(trees, 'TipLabel') <- c('a', 'b', 'c')
trees[] <- lapply(trees, DoSomething)

# Error in `[<-.multiPhylo`(`*tmp*`, , value = list(list(edge = c(4L, 4L,  :
#   argument "..1" is missing, with no default
emmanuelparadis commented 3 years ago

A fixed version is on GH. I did a few tests but more tests will be welcome. Best, Emmanuel