Bugzilla – Bug 15535
unlist(list(as.raw(11), TRUE)) produces corrupt logical vector
Last modified: 2013-11-14 21:39:06 UTC
Created attachment 1509 [details]
Correction and unit test
unlist() promotes raws to logicals with a simple (int) cast, which means that subsequent logical equality checks will fail.
a <- list(as.raw(11), TRUE)
b <- unlist(a, recursive=FALSE)
print(b) # c(TRUE,TRUE)
stopifnot(all(b)) # OK
stopifnot(b == TRUE) # ERROR
stopifnot(identical(b, c(TRUE,TRUE))) #ERROR
as.integer(b) # c(11,1)
N.B. This also seems to be the case for integer vectors in LogicalAnswer, but as far as I can tell this code is unreachable.
This isn't limited to unlist. You get the same effects from
b <- c(as.raw(11), TRUE)
I'll look into it.
Thanks for the report and fix.
I also fixed the integer case, just in case someone calls it sometime.
Great, I also included a regression test in the patch, could be good to include:
--- tests/unlist.R (revision 0)
+++ tests/unlist.R (working copy)
@@ -0,0 +1,9 @@
+# Raws need to get cast properly to logicals, otherwise subtle inconsistencies arise
+a <- list(as.raw(11), TRUE)
+b <- unlist(a, recursive=FALSE)
+stopifnot(b == TRUE)