I think my issue is actually with future
and exporting methods rather than tidytree
, but if someone could explain to me what is actually happening and how I should be writing my code to avoid this problem it would be greatly appreciated.
I am using tidytree
to do some phylogenetic analyses. The function offspring
works fine when I use lapply
, but throws the following error when I use future_lapply
:
Error in UseMethod("offspring") :
no applicable method for 'offspring' applied to an object of class "phylo"
Reproducible example:
library(ape)
library(tidytree)
library(future.apply)
tree <- rtree(4)
lapply(5:7, function(x) offspring(tree,x, tiponly=TRUE))
future_lapply(5:7, function(x) tidytree::offspring(tree,x, tiponly=TRUE))
And corresponding output:
> lapply(5:7, function(x) offspring(tree,x, tiponly=TRUE))
[[1]]
[1] 1 4 2 3
[[2]]
[1] 4 2 3
[[3]]
[1] 2 3
> future_lapply(5:7, function(x) tidytree::offspring(tree,x, tiponly=TRUE))
Error in UseMethod("offspring") :
no applicable method for 'offspring' applied to an object of class "phylo"
I can reproduce this error also using lapply()
;
library(ape)
library(tidytree)
tree <- rtree(4)
y <- lapply(5:7, FUN = function(x) offspring(tree, x, tiponly = TRUE))
#> Error in UseMethod("offspring") :
#> no applicable method for 'offspring' applied to an object of class "phylo"
And, also without lapply()
;
library(ape)
library(tidytree)
tree <- rtree(4)
y <- offspring(tree, 5L, tiponly = TRUE)
#> Error in UseMethod("offspring") :
#> no applicable method for 'offspring' applied to an object of class "phylo"
This happens because tree
is of class phylo
:
class(tree)
#> [1] "phylo"
but tidytree does not provide any S3 methods for this class;
methods("offspring")
#> [1] offspring.tbl_tree*
#> see '?methods' for accessing help and source code
Looking at the example in ?tidytree::offspring
, it looks like you forgot:
tree <- as_tibble(tree)
Adding that makes it work, including with future.apply.
EDIT: Added the solution.