Skip to content

Bug in as.data.frame.Node: NA for certain lists slots #135

@thothal

Description

@thothal

There are some cases of list fields where print (respectively the underlying as.data.frame.Node) does not work as intended:

library(data.tree)
data(acme)
## Problem: elements which are length 1 _lists_ (Case #1)
acme$Set(bad1 = list(list(list(a = 1)), 
                     list(list(a = 1, b = 2))), 
         filterFun = isNotRoot)
## Problem: elements will be transformed to matrix (Case #2)
acme$Set(bad2 = list(list(a = 1, b = 2)))

print(acme, "bad1") ## prints but shows NA and warning
#                           levelName bad1
# 1  Acme Inc.                          NA
# 2   ¦--Accounting                     NA
# 3   ¦   ¦--New Software               NA
# 4   ¦   °--New Accounting Standards   NA
# 5   ¦--Research                       NA
# 6   ¦   ¦--New Product Line           NA
# 7   ¦   °--New Labs                   NA
# 8   °--IT                             NA
# 9       ¦--Outsource                  NA
# 10      ¦--Go agile                   NA
# 11      °--Switch to R                NA
# Warning message:
# In `[<-.data.frame`(`*tmp*`, colName, value = list(`Acme Inc.` = NA,  :
#   provided 11 variables to replace 1 variables

print(acme, "bad2") ## doesn't even print
# Error in `[<-.data.frame`(`*tmp*`, colName, value = c(1, 2, 1, 2, 1, 2,  : 
#   replacement has 22 rows, data has 11

The error comes from these lines.

  • Case #1: stems from the fact that sapply creates a list, because for a length(.) == 1 object it simply returns the object, which is in this case a length 1 list, thus the result must eb a list as well
  • Case #2: comes from teh fact that Get does some simplification by default, and transforms the output to a 2 x # of nodes matrix, which is then treated as a vector by sapply.

Solution to this problem is to use Get with simplify = FALSE, retort to the more predictable lapply and doing an unlist in the end.

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