Environment
Bug
tp_phylo_to_tpjson(phylo_tree, age = "") (the documented default) always fails:
library(treepplr)
tree <- ape::read.tree(text = "((1:1.0,2:1.0):0.5,(3:1.2,4:1.2):0.3);")
tp_phylo_to_tpjson(tree)
#> Error in `$<-.data.frame`(`*tmp*`, "Age", value = integer(0)) :
#> replacement has 0 rows, data has 7
Root cause
In treepplr:::tree_age_cumul():
tree_age_cumul <- function(tree, root_index, age = "branch-length") {
age_cumul <- rep(length(tree$Type), 0)
...
rep(length(tree$Type), 0) is rep(x = <tree size>, times = 0), which always returns a zero-length vector, regardless of tree size (looks like the arguments were meant to be swapped: rep(0, length(tree$Type))).
For age = "root-to-tip" or age = "tip-to-root", the subsequent traversal happens to populate age_cumul[i] by index during the walk, which auto-extends the vector in R, so those two paths work despite the bug. But for any other value of age (including the documented default ""), age_cumul stays length 0, and the final tree$Age <- age_cumul throws the data.frame replacement error above with no indication of what's actually wrong.
Impact
tp_phylo_to_tpjson()'s default argument is unusable — every caller must discover (via this error, or via source-diving) that they need to pass age = "tip-to-root" (or "root-to-tip") explicitly. This isn't mentioned in the function's documentation.
Suggested fix
Fix the rep() call: rep(0, length(tree$Type)). That alone would make age = "" behave sanely (probably as a no-op / all-zero ages, which should also be documented), and would remove the reliance on the auto-extension side-effect for the two branches that currently "work".
Environment
Bug
tp_phylo_to_tpjson(phylo_tree, age = "")(the documented default) always fails:Root cause
In
treepplr:::tree_age_cumul():rep(length(tree$Type), 0)isrep(x = <tree size>, times = 0), which always returns a zero-length vector, regardless of tree size (looks like the arguments were meant to be swapped:rep(0, length(tree$Type))).For
age = "root-to-tip"orage = "tip-to-root", the subsequent traversal happens to populateage_cumul[i]by index during the walk, which auto-extends the vector in R, so those two paths work despite the bug. But for any other value ofage(including the documented default""),age_cumulstays length 0, and the finaltree$Age <- age_cumulthrows the data.frame replacement error above with no indication of what's actually wrong.Impact
tp_phylo_to_tpjson()'s default argument is unusable — every caller must discover (via this error, or via source-diving) that they need to passage = "tip-to-root"(or"root-to-tip") explicitly. This isn't mentioned in the function's documentation.Suggested fix
Fix the
rep()call:rep(0, length(tree$Type)). That alone would makeage = ""behave sanely (probably as a no-op / all-zero ages, which should also be documented), and would remove the reliance on the auto-extension side-effect for the two branches that currently "work".