Rstan error: VECTOR_ELT() can only be applied to a 'list', not a 'double'

I am running into the following error

VECTOR_ELT() can only be applied to a ‘list’, not a ‘double’

A brief google search seems like the function expose_stan_functions may not behave so well with arrays. Not sure if this is my problem but it would make sense given the function I am trying to expose.

Data can be downloaded from here: Dropbox - File Deleted - Simplify your life

I have included a minimal reproducible example in the scripts below as well.

  • I have already tried all the stupid things like updating my version of packages/R/Rstudio and restarting R.

Not exactly sure what is causing this issue but help would be most appreciated!

Thank you,
Justin

error_reproduce.R (318 Bytes)
utils_2.stan (580 Bytes)
tmp_driver.stan (38 Bytes)

Operating System: Mac OS X
Interface Version: 2.16.2
Output of writeLines(readLines(file.path(Sys.getenv(“HOME”), “.R/Makevars”))):

CC=clang
CXX=clang++

CXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function

CC=clang
CXX=clang++ -arch x86_64 -ftemplate-depth-256

CXXFLAGS += -Wno-ignored-attributes -Wno-deprecated-declarations

Output of devtools::session_info("rstan”):

Session info -----------------------------------------
 setting  value                       
 version  R version 3.4.1 (2017-06-30)
 system   x86_64, darwin15.6.0        
 ui       RStudio (1.0.153)           
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       America/New_York            
 date     2017-10-02                  

Packages ---------------------------------------------
 package      * version   date       source        
 BH             1.65.0-1  2017-08-24 CRAN (R 3.4.1)
 colorspace     1.3-2     2016-12-14 CRAN (R 3.4.0)
 dichromat      2.0-0     2013-01-24 CRAN (R 3.4.0)
 digest         0.6.12    2017-01-27 CRAN (R 3.4.0)
 ggplot2      * 2.2.1     2016-12-30 CRAN (R 3.4.0)
 graphics     * 3.4.1     2017-07-07 local         
 grDevices    * 3.4.1     2017-07-07 local         
 grid           3.4.1     2017-07-07 local         
 gridExtra      2.3       2017-09-09 CRAN (R 3.4.1)
 gtable         0.2.0     2016-02-26 CRAN (R 3.4.0)
 inline         0.3.14    2015-04-13 CRAN (R 3.4.0)
 labeling       0.3       2014-08-23 CRAN (R 3.4.0)
 lattice        0.20-35   2017-03-25 CRAN (R 3.4.1)
 lazyeval       0.2.0     2016-06-12 CRAN (R 3.4.0)
 magrittr       1.5       2014-11-22 CRAN (R 3.4.0)
 MASS           7.3-47    2017-02-26 CRAN (R 3.4.1)
 Matrix         1.2-11    2017-08-16 CRAN (R 3.4.1)
 methods      * 3.4.1     2017-07-07 local         
 munsell        0.4.3     2016-02-13 CRAN (R 3.4.0)
 plyr           1.8.4     2016-06-08 CRAN (R 3.4.0)
 R6             2.2.2     2017-06-17 CRAN (R 3.4.0)
 RColorBrewer   1.1-2     2014-12-07 CRAN (R 3.4.0)
 Rcpp           0.12.13   2017-09-28 CRAN (R 3.4.1)
 RcppEigen      0.3.3.3.0 2017-05-01 CRAN (R 3.4.0)
 reshape2       1.4.2     2016-10-22 CRAN (R 3.4.0)
 rlang          0.1.2     2017-08-09 CRAN (R 3.4.1)
 rstan        * 2.16.2    2017-07-03 CRAN (R 3.4.1)
 scales         0.5.0     2017-08-24 CRAN (R 3.4.1)
 StanHeaders  * 2.16.0-1  2017-07-03 CRAN (R 3.4.1)
 stats        * 3.4.1     2017-07-07 local         
 stats4         3.4.1     2017-07-07 local         
 stringi        1.1.5     2017-04-07 CRAN (R 3.4.0)
 stringr        1.2.0     2017-02-18 CRAN (R 3.4.0)
 tibble         1.3.4     2017-08-22 CRAN (R 3.4.1)
 tools          3.4.1     2017-07-07 local         
 utils        * 3.4.1     2017-07-07 local         
 viridisLite    0.2.0     2017-03-24 CRAN (R 3.4.0)

The first argument has to be a list of 249 matrices that are 4x4 rather than a 249 x 4 x 4 array. You can accomplish this via

lapply(1:nrow(br_dat$GG, FUN = function(i) br_dat$GG[i,,])
1 Like

Thank you for your quick reply! Is this standard for all functions exposed through expose_stan_functions? i.e., if I have defined an array type in my stan code then I need to pass it to an exposed function as a list of whatever the base type is? This seems different than the data declarations where I have been able to pass such an array of matrices as a multidimensional array.

Yes. This is an consequence that Rcpp deals with inputs differently than Stan. Stan can input multidimensional arrays, but I guess Rcpp wants lists of things.