stan-dev / rstantools

Tools for Developing R Packages Interfacing with Stan
https://mc-stan.org/rstantools
GNU General Public License v3.0
51 stars 21 forks source link

problem exporting Rcpp functions #13

Open lukas-rokka opened 7 years ago

lukas-rokka commented 7 years ago

I want to make a C++ function aviliable to R with the // [[Rcpp::export]] command. This does not work when using the rstan.package.skeleton. I get the error "object '[name of the rcpp function]' not found". It appears to be some problem with registering the routine.

However, if I remove lines 16:20 from src\init.cpp the registration works. Here is the code from those lines:

void attribute_visible R_init_sem5r1c(DllInfo *dll) {
  // next line is necessary to avoid a NOTE from R CMD check
  R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
  R_useDynamicSymbols(dll, TRUE); // necessary for .onLoad() to work
}

What do these lines of code actually do and are they safe to remove?

jtipton25 commented 6 years ago

I'm having the same issue but deleting this block of code doesn't fix it. I used rstanstools::rstan_package_skeleton to setup a package and added the default timesTwo.cpp to the package. I'm getting the error

Error in timesTwo(rnorm(10)) : object '_test_timesTwo' not found

I'd like to have a package to fit a model using RStan and then use an RcppArmadillo function to post-process the data. I can't find any documentation referring to implementing stand-alone Rcpp functions within an RStan package. Any help would be appreciated.

bgoodri commented 6 years ago

There isn't documentation for that in rstantools because no one else (that I know of) has wanted to try, it is usually a better idea to put stuff like that into the generated quantities block of your Stan programs, and doing so presumably just involves adding the usual stuff for a package that uses RcppArmadillo on top of what is generated by rstantools::rstan_package_skeleton.

On Wed, May 30, 2018 at 1:26 PM, jtipton25 notifications@github.com wrote:

I'm having the same issue but deleting this block of code doesn't fix it. I used rstanstools::rstan_package_skeleton to setup a package and added the default timesTwo.cpp to the package. I'm getting the error

Error in timesTwo(rnorm(10)) : object '_test_timesTwo' not found

I'd like to have a package to fit a model using RStan and then use an RcppArmadillo function to post-process the data. I can't find any documentation referring to implementing stand-alone Rcpp functions within an RStan package. Any help would be appreciated.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/stan-dev/rstantools/issues/13#issuecomment-393248329, or mute the thread https://github.com/notifications/unsubscribe-auth/ADOrqpLfco802dlh8kwj6KtWYD-HmgL7ks5t3tZDgaJpZM4PlwYA .

jsilve24 commented 6 years ago

This is a problem that should probably be fixed. Putting code in generated quantities block is very limiting.

lucasnell commented 6 years ago

I agree with above. I'm trying to make a package that includes simulations using Rcpp, and having those functions in generated quantities blocks instead of their own .cpp files is a real problem.

jtimonen commented 6 years ago

I would also need RcppArmadillo to work in a package created using Rstantools. I have the same problem: object [name of function] not found

helske commented 6 years ago

I have successfully combined external C++ and Stan models in the same package in the past (https://github.com/helske/walker), but I just started to update some stuff and something has changed so that I new I get an infinite recompilation loops... And if I switch to newer package skeleton structure, I also face the issue that either Stan models or other C++ functions are not getting exported (depending on how I tweak Makevars etc).

bgoodri commented 6 years ago

I think roxygen2 changed its behavior, so you should not have your package rebuild your manpages inside the cleanup function.

On Fri, Oct 12, 2018 at 4:52 PM Jouni Helske notifications@github.com wrote:

I have successfully combined external C++ and Stan models in the same package in the past (https://github.com/helske/walker), but I just started to update some stuff and something has changed so that I new I get an infinite recompilation loops... And if I switch to newer package skeleton structure, I also face the issue that either Stan models or other C++ functions are not getting exported (depending on how I tweak Makevars etc).

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/stan-dev/rstantools/issues/13#issuecomment-429458220, or mute the thread https://github.com/notifications/unsubscribe-auth/ADOrqgG2DxnLGbjZNaZ4kq6QNoWPk9xsks5ukQD6gaJpZM4PlwYA .

helske commented 6 years ago

Yes I just realized that as well, removing the roxygen line fixed the infinite loop issue and the package compiles. But the Stan models are not getting exported. Investigation continues...

edit: Oops, I had a wrong version of the stanmodels.R file. Everything works now: https://github.com/helske/walker.

bgoodri commented 6 years ago

Do you have something like this in your zzz.R? https://github.com/stan-dev/rstanarm/blob/master/R/zzz.R#L18

On Fri, Oct 12, 2018 at 5:11 PM Jouni Helske notifications@github.com wrote:

Yes I just realized that as well, removing the roxygen line fixed the infinite loop issue and the package compiles. But the Stan models are not getting exported. Investigation continues...

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/stan-dev/rstantools/issues/13#issuecomment-429463444, or mute the thread https://github.com/notifications/unsubscribe-auth/ADOrqoAlaNeg901_KZg5_HaQqbw2ddefks5ukQVlgaJpZM4PlwYA .

const-ae commented 6 years ago

I encountered the same problem today, that I couldn't use Rcpp functions in a package generated with rstanstools::rstan_package_skeleton.

I found that removing the R_init_mypackage function fixes the problem of calling the Rcpp functions, because the function is then generated by Rcpp in RcppExports.cpp. But I got another error when I loaded the package that "object 'm' not found" as described in issue #43.

I solved this issue by adding

// [[Rcpp::init]]
void rstan_additional_init(DllInfo *dll){
  R_useDynamicSymbols(dll, TRUE); // necessary for .onLoad() to work
}

which makes sure that the dynamic symbol is accessible in the .onLoad function in zzz.R.

So in conclusion, replacing the R_init_mypackage function with the code above fixed my problem.

bgoodri commented 6 years ago

Nice. There may be other solutions but I am glad you found one. Be sure to read https://discourse.mc-stan.org/t/how-to-update-a-package-that-has-stanheaders-and-rstan-in-its-linkingto/6041?u=bgoodri for how to get it to work with rstan 2.18.1.

On Tue, Oct 23, 2018 at 12:56 PM Constantin notifications@github.com wrote:

I encountered the same problem today, that I couldn't use Rcpp functions in a package generated with rstanstools::rstan_package_skeleton.

I found that removing the R_init_mypackage function fixes the problem of calling the Rcpp functions, because the function is then generated by Rcpp in RcppExports.cpp. But I got another error when I loaded the package that "object 'm' not found" as described in issue #43 https://github.com/stan-dev/rstantools/issues/43.

I solved this issue by adding

// [[Rcpp::init]] void rstan_additional_init(DllInfo *dll){ R_useDynamicSymbols(dll, TRUE); // necessary for .onLoad() to work }

which makes sure that the dynamic symbol is accessible in the .onLoad function in zzz.R.

So in conclusion, replacing the R_init_mypackage function with the code above fixed my problem.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/stan-dev/rstantools/issues/13#issuecomment-432329653, or mute the thread https://github.com/notifications/unsubscribe-auth/ADOrqjXMmZWvWiv1UtjUaPz54wn-ESpXks5un0osgaJpZM4PlwYA .

bart1 commented 6 years ago

Just a quick comment to add to @const-ae, encountered the same issue and had some trouble getting it to work, in addition to @const-ae suggestion that were required. A few quick notes that would have helped me. You also need to include RcppExports.o and the other Rcpp source files in the sources variable of Makevars (and I assume Makevars.win but have not tested that) I was also having troubles with including RcppArmadillo, it turns out I needed to import it before importing RcppEigen in the DESCRIPTION file.