Error expose_stan_functions

stanc

#1

When I run the not_run example in the docs of expose_stan_function, I get

Error in get_model_strcode(file, NULL) : 
  file must be a character string or connection

Same for my own external files, but I tried to boil it down.

Operating System:
R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Output of writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars"))):

CXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-deprecated-declarations -Wno-unused-local-typedefs -Wno-unused-function
CXX_STD = CXX11

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

Session info --------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.2 (2017-09-28)
 system   x86_64, mingw32             
 ui       RStudio (1.1.372)           
 language (EN)                        
 collate  German_Germany.1252         
 tz       Europe/Berlin               
 date     2017-10-05                  

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.9000 2017-09-18 Github (hadley/ggplot2@c592e32) 
 graphics     * 3.4.2      2017-09-28 local                           
 grDevices    * 3.4.2      2017-09-28 local                           
 grid           3.4.2      2017-09-28 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.2)                  
 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.2)                  
 Matrix         1.2-11     2017-08-21 CRAN (R 3.4.2)                  
 methods      * 3.4.2      2017-09-28 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.2)                  
 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.17.1     2017-09-21 Github (stan-dev/rstan@39f9c1a) 
 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.2      2017-09-28 local                           
 stats4         3.4.2      2017-09-28 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.2      2017-09-28 local                           
 utils        * 3.4.2      2017-09-28 local                           
 viridisLite    0.2.0      2017-03-24 CRAN (R 3.4.0)                  
 withr          2.0.0      2017-09-18 Github (jimhester/withr@d1f0957)

#2

Yeah, the expose_stan_function is in a bit of a state of flux on GitHub. It might work now.


#3

It’s always a bit of a puzzle to install rstan from github. Maybe add some section “Install from github” in the readme? Or did I miss it? Could only find build instruction, which are less convenient. I used

devtools::install_github("stan-dev/rstan/rstan/rstan@develop")

which seems to work, but fails the message below. Looks like a memory problem, but I have 32 GB. I have CXX_STD = CXX11 from previous experimentation, is that ok?

from 'stan::lang::indexes_grammar<Iterator>::indexes_grammar(stan::lang::variable_map&, std::stringstream&, stan::lang::expression_grammar<Iterator>&) [with Iterator = boost::spirit::line_pos_iterator<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> > >; std::stringstream = std::basic_stringstream<char>]'
C:/R/library/StanHeaders/include/src/stan/lang/grammars/indexes_grammar_inst.cpp:7:33:   required from here
C:/R/library/BH/include/boost/spirit/home/qi/nonterminal/rule.hpp:185:9: internal compiler error: Segmentation fault
         }
         ^
Please submit a full bug report,


#4

That line works for me with much less than 32 GB of RAM. Maybe your configuration is including debug information in the shared object?


#5

Build now works for me after I removed the CXX_STD = CXX11 line which was required during the transition phase from 2.16 to 2.17.

rstan (Version 2.17.1, packaged: , GitRev: 2e1f913d3ca3)

Not success with expose_stan_functions yet:

Error in get_model_strcode(file, NULL) :
file must be a character string or connection


#6

Found it. Your version works when the filename is given (checked), but does does not retrieve the mc-string you use in the example. Alternatively, remove the example


function (file, ...) 
{
    model_code <- get_model_strcode(file, NULL)
    model_cppname <- legitimate_model_name(basename(file), obfuscate_name = TRUE)

Something like this, probably

function (file, mc_code, ...) 
{
    model_code <- get_model_strcode(file, mc_code)


#7

Precondition: Running stan the usual way works in my setup.

Next step - tried with RStudio on Ubuntu, and on Windows as documented above

– Put fibonacci example in a file “a.stan” (since passing text currently fails)

library(rstan)
st = expose_stan_functions("a.stan")
---------------------------------------------------------

In file included from file37284ff74b42.cpp:6:
In file included from /usr/local/lib/R/site-library/StanHeaders/include/src/stan/model/standalone_functions_header.hpp:4:
In file included from /usr/local/lib/R/site-library/StanHeaders/include/stan/math.hpp:4:
In file included from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core.hpp:12:
In file included from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5:
In file included from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/var.hpp:7:
In file included from /usr/local/lib/R/site-library/BH/include/boost/math/tools/config.hpp:13:
In file included from /usr/local/lib/R/site-library/BH/include/boost/config.hpp:39:
/usr/local/lib/R/site-library/BH/include/boost/config/compiler/clang.hpp:200:11: warning: 'BOOST_NO_CXX11_RVALUE_REFERENCES' macro redefined [-Wmacro-redefined]
#  define BOOST_NO_CXX11_RVALUE_REFERENCES
          ^
<command line>:6:9: note: previous definition is here
#define BOOST_NO_CXX11_RVALUE_REFERENCES 1
        ^
file37284ff74b42.cpp:13:19: error: target of using declaration conflicts with declaration already in scope
using stan::math::lgamma;
                  ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/prim/scal/fun/lgamma.hpp:36:19: note: target of using declaration
    inline double lgamma(double x) {
                  ^
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:261:13: note: conflicting declaration
__MATHCALL (lgamma,, (_Mdouble_));
            ^
/usr/include/math.h:66:25: note: expanded from macro '__MATHCALL'
  __MATHDECL (_Mdouble_,function,suffix, args)
                        ^
/usr/include/math.h:68:22: note: expanded from macro '__MATHDECL'
  __MATHDECL_1(type, function,suffix, args); \
                     ^
/usr/include/math.h:76:31: note: expanded from macro '__MATHDECL_1'
  extern type __MATH_PRECNAME(function,suffix) args __THROW
                              ^
/usr/include/math.h:79:42: note: expanded from macro '__MATH_PRECNAME'
#define __MATH_PRECNAME(name,r) __CONCAT(name,r)
                                         ^
/usr/include/x86_64-linux-gnu/sys/cdefs.h:88:23: note: expanded from macro '__CONCAT'
#define __CONCAT(x,y)   x ## y
                        ^
file37284ff74b42.cpp:34:46: error: parameter declarator cannot be qualified
fibonacci(const int& x, std::ostream* &Rcpp::Rcout);
                                       ~~~~~~^
file37284ff74b42.cpp:58:91: error: call to 'fibonacci' is ambiguous
        return stan::math::promote_scalar<fun_return_scalar_t__>((logical_lte(x,2) ? 1 : (fibonacci((x - 1), pstream__) + fibonacci((x - 2), pstream__)) ));
                                                                                          ^~~~~~~~~
file37284ff74b42.cpp:34:1: note: candidate function
fibonacci(const int& x, std::ostream* &Rcpp::Rcout);
^
file37284ff74b42.cpp:38:1: note: candidate function
fibonacci(const int& x, std::ostream* pstream__) {
^
file37284ff74b42.cpp:58:123: error: call to 'fibonacci' is ambiguous
        return stan::math::promote_scalar<fun_return_scalar_t__>((logical_lte(x,2) ? 1 : (fibonacci((x - 1), pstream__) + fibonacci((x - 2), pstream__)) ));
                                                                                                                          ^~~~~~~~~
file37284ff74b42.cpp:34:1: note: candidate function
fibonacci(const int& x, std::ostream* &Rcpp::Rcout);
^
file37284ff74b42.cpp:38:1: note: candidate function
fibonacci(const int& x, std::ostream* pstream__) {
^
file37284ff74b42.cpp:80:50: error: parameter declarator cannot be qualified
int fibonacci(const int& x, std::ostream*& Rcpp::Rcout);
                                           ~~~~~~^
file37284ff74b42.cpp:81:62: error: parameter declarator cannot be qualified
RcppExport SEXP sourceCpp_1_fibonacci(SEXP xSEXP, SEXP Rcpp::RcoutSEXP) {
                                                       ~~~~~~^
file37284ff74b42.cpp:86:77: error: no type named 'RcoutSEXP' in namespace 'Rcpp'
    Rcpp::traits::input_parameter< std::ostream*& >::type Rcpp::Rcout(Rcpp::RcoutSEXP);
                                                                      ~~~~~~^
file37284ff74b42.cpp:86:65: error: definition or redeclaration of 'Rcout' not allowed inside a function
    Rcpp::traits::input_parameter< std::ostream*& >::type Rcpp::Rcout(Rcpp::RcoutSEXP);
                                                          ~~~~~~^
file37284ff74b42.cpp:86:70: warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
    Rcpp::traits::input_parameter< std::ostream*& >::type Rcpp::Rcout(Rcpp::RcoutSEXP);
                                                                     ^~~~~~~~~~~~~~~~~
file37284ff74b42.cpp:86:71: note: add a pair of parentheses to declare a variable
    Rcpp::traits::input_parameter< std::ostream*& >::type Rcpp::Rcout(Rcpp::RcoutSEXP);
                                                                      ^
                                                                      (
file37284ff74b42.cpp:87:53: error: reference to 'Rcout' is ambiguous
    rcpp_result_gen = Rcpp::wrap(fibonacci(x, Rcpp::Rcout));
                                              ~~~~~~^
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:92:28: note: candidate found by name lookup is 'Rcpp::Rcout'
    static Rostream<true>  Rcout;
                           ^
file37284ff74b42.cpp:86:65: note: candidate found by name lookup is 'Rcpp::Rcout'
    Rcpp::traits::input_parameter< std::ostream*& >::type Rcpp::Rcout(Rcpp::RcoutSEXP);
                                                                ^
file37284ff74b42.cpp:93:17: error: redefinition of 'sourceCpp_1_fibonacci'
RcppExport SEXP sourceCpp_1_fibonacci(SEXP xSEXP, SEXP pstream__SEXP) {
                ^
file37284ff74b42.cpp:81:17: note: previous definition is here
RcppExport SEXP sourceCpp_1_fibonacci(SEXP xSEXP, SEXP Rcpp::RcoutSEXP) {
                ^
In file included from file37284ff74b42.cpp:4:
In file included from /usr/local/lib/R/site-library/Rcpp/include/Rcpp.h:27:
In file included from /usr/local/lib/R/site-library/Rcpp/include/RcppCommon.h:160:
In file included from /usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:25:
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/internal/Exporter.h:31:28: error: cannot initialize a member subobject of type 'std::basic_ostream<char> *' with an lvalue of type 'SEXP' (aka 'SEXPREC *')
                    Exporter( SEXP x ) : t(x){}
                                         ^ ~
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:87:41: note: in instantiation of member function 'Rcpp::traits::Exporter<std::basic_ostream<char> *>::Exporter' requested here
            ::Rcpp::traits::Exporter<T> exporter(x);
                                        ^
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:152:26: note: in instantiation of function template specialization 'Rcpp::internal::as<std::basic_ostream<char> *>' requested here
        return internal::as<T>(x, typename traits::r_type_traits<T>::r_category());
                         ^
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/InputParameter.h:34:38: note: in instantiation of function template specialization 'Rcpp::as<std::basic_ostream<char> *>' requested here
        inline operator T() { return as<T>(x) ; }
                                     ^
file37284ff74b42.cpp:99:47: note: in instantiation of member function 'Rcpp::InputParameter<std::basic_ostream<char> *>::operator std::basic_ostream<char> *' requested here
    rcpp_result_gen = Rcpp::wrap(fibonacci(x, pstream__));
                                              ^
2 warnings and 11 errors generated.
make: *** [file37284ff74b42.o] Fehler 1
clang++ -I/usr/share/R/include -DNDEBUG   -I"/usr/local/lib/R/site-library/Rcpp/include/"  -I"/usr/local/lib/R/site-library/RcppEigen/include/"  -I"/usr/local/lib/R/site-library/RcppEigen/include/unsupported"  -I"/usr/local/lib/R/site-library/rstan/include/boost_not_in_BH" -I"/usr/local/lib/R/site-library/BH/include" -I"/usr/local/lib/R/site-library/StanHeaders/include/src/"  -I"/usr/local/lib/R/site-library/StanHeaders/include/"  -I"/usr/local/lib/R/site-library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DBOOST_NO_CXX11_RVALUE_REFERENCES  -I"/usr/local/lib/R/site-library/Rcpp/include" -I"/usr/local/lib/R/site-library/rstan/include" -I"/tmp/Rtmp3pDkcc/sourceCpp-x86_64-pc-linux-gnu-0.12.13"    -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c file37284ff74b42.cpp -o file37284ff74b42.o
/usr/lib/R/etc/Makeconf:168: die Regel für Ziel „file37284ff74b42.o“ scheiterte
Fehler in Rcpp::sourceCpp(code = paste(code, collapse = "\n"), ...) : 
  Error 1 occurred building shared library.


#8

Summarized this on

https://github.com/stan-dev/rstan/issues/458