Bug 17174

Summary: Wishlist: 'vapply' could support long vectors
Product: R Reporter: Suharto Anggono <suharto_anggono>
Component: WishlistAssignee: R-core <R-core>
Status: CLOSED FIXED    
Severity: enhancement CC: maechler
Priority: P5    
Version: R 3.3.*   
Hardware: Other   
OS: Linux-Debian   
Attachments: apply.c: do_vapply: for 'ind', make 'allocVector' like in 'do_lapply'

Description Suharto Anggono 2016-10-30 03:05:16 UTC
Created attachment 2177 [details]
apply.c: do_vapply: for 'ind', make 'allocVector' like in 'do_lapply'

In RStudio in Data Scientist Workbench:

R> res <- vapply(raw(2^31), function(x) x, raw(1))
Error in X[[i]] : 
  attempt to select less than one element in integerOneIndex
R> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux stretch/sid

 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] SparkR_1.6.1

loaded via a namespace (and not attached):
[1] tools_3.3.1

For completeness, it is nice if 'vapply' works on long vectors.

Looking at the code that implements 'vapply', function 'do_vapply' in apply.c, supporting long vectors for vector results (value of length 1) seems to be intended.

For the example above, the culprit is likely that the object stored in variable 'ind' in C, that is linked to symbol 'i' in R side, is treated by R as a vector of storage mode "integer" because it is made by allocVector(INTSXP, 1), but assignment of value is done by
REAL(ind)[0] = (double)(i + 1);

The attached patch modifies 'allocVector' for 'ind' in function 'do_vapply' to be like the corresponding part in function 'do_lapply'.
Comment 1 Martin Maechler 2016-10-31 09:20:49 UTC
Thank you, Suharto for checking these things and for the patch.
Comment 2 Martin Maechler 2016-11-01 08:32:29 UTC
I've committed the patch .. and tested it (needs ~ 20 min!) to R-devel  
and will port to R-patched.