-
Notifications
You must be signed in to change notification settings - Fork 41
Closed
Description
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 11The error comes from these lines.
- Case
#1: stems from the fact thatsapplycreates a list, because for alength(.) == 1object 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 thatGetdoes some simplification by default, and transforms the output to a2 x # of nodesmatrix, which is then treated as a vector bysapply.
Solution to this problem is to use Get with simplify = FALSE, retort to the more predictable lapply and doing an unlist in the end.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels