-
-
Notifications
You must be signed in to change notification settings - Fork 218
Closed
Description
Over in this StackOverflow post we see that a sequence like 1:10
get sum()
ed in R seemingly on SEXP header notion.
When Rcpp inserts a value, the sequence gets broken but the bit does not get unset. Thanks to @rstub to making it plain via .Internal(inspect)
. In short:
R> v <- 1:10
R> sum(v)
[1] 55
R> .Internal(inspect(v))
@559843d446c0 13 INTSXP g0c0 [NAM(3)] 1 : 10 (compact)
R>
R>
R> v[5] <- 100
R> sum(v)
[1] 150
R> .Internal(inspect(v))
@55984953b748 14 REALSXP g0c5 [NAM(1)] (len=10, tl=0) 1,2,3,4,100,...
R>
In short we do not make the transition from the 1 : 10 (compact)
representation to the enumerated materialized set -- yet sum()
in R only looks at that representation bit and then returns the wrong result.
So with Rcpp (and the changed code by the OP):
R> Rcpp::cppFunction("IntegerVector modvec(IntegerVector x) { x[4] = 100; return(x); }")
R> v <- 1:10; sum(v); .Internal(inspect(v))
[1] 55
@559846e37070 13 INTSXP g0c0 [NAM(3)] 1 : 10 (compact)
R>
R> modvec(v)
[1] 1 2 3 4 100 6 7 8 9 10
R>
R> sum(v); .Internal(inspect(v)) # so sum() now lies to us as we hide change
[1] 55
@559846e37070 13 INTSXP g0c0 [NAM(3)] 1 : 10 (expanded)
R>
R> v[5] <- 101
R> sum(v); .Internal(inspect(v))
[1] 151
@55984618c628 14 REALSXP g0c5 [NAM(1)] (len=10, tl=0) 1,2,3,4,101,...
R>
@gmbecker If you have quick pointers... I still have your 'well ALTREP should not require user packages changes' ringing in my ear...
jangorecki