You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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"
The text was updated successfully, but these errors were encountered:
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
COMPARE:
The text was updated successfully, but these errors were encountered: