Skip to content

nested lists inconsistencies depending on NULL: entire answer dropped when ans[1L] is list(NULL) #842

@rsaporta

Description

@rsaporta

Assume there is a function which returns a nested list. Any element of the return value might be NULL, depending on the input.

If the 1st element of the return value happens to be NULL, then the entire return value is lost / discarded.

Sample Function and Data

SomeFunction <- function(x, which.to.null=1L) {
  ## Create a dummy list, length of x, as answer
  ans <- replicate(length(x), list("Hello world", "this is a nested list"), simplify=FALSE)

  ## set the first value to NULL
  ans[which.to.null] <- list(NULL)
  return(ans)
}

DT <- data.table(ID=1:3, key="ID")

COMPARE:

DT[, SomeFunction(ID, which.to.null=1L)]
## Empty data.table (0 rows) of 3 cols: V1,V2,V3

DT[, SomeFunction(ID, which.to.null=2L)]
#                       V1   V2                    V3
#1:           Hello world NULL           Hello world
#2: this is a nested list NULL this is a nested list

SomeFunction(DT$ID, which.to.null=1L)
#  [[1]]
#  NULL
#  
#  [[2]]
#  [[2]][[1]]
#  [1] "Hello world"
#  
#  [[2]][[2]]
#  [1] "this is a nested list"
#  
#  
#  [[3]]
#  [[3]][[1]]
#  [1] "Hello world"
#  
#  [[3]][[2]]
#  [1] "this is a nested list"

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions