stan-dev / rstan

RStan, the R interface to Stan
https://mc-stan.org
1.04k stars 268 forks source link

cpp_object_initializer failing in downstream packages #353

Open davharris opened 7 years ago

davharris commented 7 years ago

Summary:

Downstream packages can't use rstan without dependsing on Rcpp, which would pollute the user's namespace

Description:

This was previously discussed in #176.

The Rcpp developers have discussed improved solutions in the last week or so on https://github.com/RcppCore/Rcpp/issues/168; If I understand correctly, it might be solvable by adding importFrom(Rcpp, evalCpp) to the NAMESPACE.

Reproducible Steps:

(via a mini package I created to demonstrate the issue)

devtools::install_github("davharris/fail")
fail::test()

Current Output:

Error in .Object$initialize(...) : 
  could not find function "cpp_object_initializer"
failed to create the sampler; sampling not done

Expected Output:

an 8-schools stanfit

RStan Version:

2.12.1

R Version:

R version 3.3.2 (2016-10-31)

Operating System:

10.12.1

bgoodri commented 7 years ago

We already import all of Rcpp within the rstan NAMESPACE

https://github.com/stan-dev/rstan/blob/develop/rstan/rstan/NAMESPACE#L13

I agree this is annoying, but I don't know what to do.

davharris commented 7 years ago

Looks like importing isn't always sufficient (according to the thread I linked to above, "plain import(Rcpp) sometimes works", but not always).

From the little bit I understand, I think this boils down to how Stan uses (or doesn't use) modules. The vignette seems to make some suggestions in Section 3. Depending on how rstan is currently set up, it might just require a call to loadModule.

bgoodri commented 7 years ago

The rstan package only uses modules at runtime when the Stan program is compiled and dynamically loaded into the R session. Most of the documentation assumes Rcpp is being used at installation / build time. I think rstan only calls the Module function, which is pretty similar to loadModule in the sense that loadModule calls Module.

ftlbiped commented 7 years ago

Adding to davharris's intial summary of the issue, it seems this occurs whenever rstan functions are called without attaching rstan first, ie. via rstan::, not just from within packages. I.e., replacing the calls to stan_model and sampling with rstan::stan_model and rstan::sampling and in his test function and simply defining and running test() in interactive mode causes the same error to appear.