Skip to content

as.Node.list() converting last list names to attributes even when NULL / empty #169

@Obsidian-user

Description

@Obsidian-user

I am new to data.tree, so apologies if I missing an established convention; I did search extensively, but I am still only human.

I have provided a reproducible example at the bottom.

Currently, when a nested list is converted by as.Node the lowest level name of a list is automatically converted into an attribute of its parent node even when this leaf-list value is NULL. This becomes a problem when representing node paths as names of a nested list. This happens when using the data.tree recommended YAML format and representing these paths as nested keys.

I consider this a bug as this behaviour can be unexpected since:

  • Attributes are not necessary for nodes or leaves, and are not created when constructed with data.tree::Node$new()
  • The last level name is treated differently to the others.
  • Conceptually, an empty YAML “key” is not equivalent to a “value”. As data.tree recommends YAML, it would be consistent to have a method to correctly import a pure node path.

Work arounds for this can be difficult, at least for those new to the package, due to:

  • less established tools for working directly with the nested list input
  • data.tree’s uncommon-in-R conventions OO-function-style with mixed style and brief documentation (e.g. Set)

Currently, I am using purrr::modify_tree(leaf = \(x) list(end = NA_character_)) to add another level to lists to the leaves to be converted to an attribute and ignored. I classify this as a work-around and not a solution because I have added additional structures to my imported object, and my leaves now have empty attributes so are not entirely equivalent to the intended object.

Possible solutions:

  • “Only names”: modify as.Node to not convert NULL valued named lists into attributes.
  • “Toggle attributes”: Add an option to as.Node to allow users to prevent automatic conversion of values to attributes e.g. treat all non-list values as nodes
  • (Less favourable): Provide an alternative to consistently import a text file as a node path

A genuine thank you for all your work.

Reproducible example:

library(yaml)
library(data.tree)

yaml <- "
name: OS Students 2014/15
OS X:
  Yosemite:
  Leopard:
Linux:
  Debian:
  Ubuntu:
Windows:
  W7:
  W8:
  W10:
"

osList <- yaml::yaml.load(yaml)
osNode <- as.Node(osList)
plot(osNode)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions