Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rsaporta opened this issue Sep 29, 2014 · 0 comments
Assignees
Milestone

Comments

@rsaporta
Copy link
Contributor

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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants