New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem exporting Rcpp functions #13

Open
lukas-rokka opened this Issue Sep 27, 2017 · 12 comments

Comments

Projects
None yet
9 participants
@lukas-rokka
Copy link

lukas-rokka commented Sep 27, 2017

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

This comment has been minimized.

Copy link

jtipton25 commented May 30, 2018

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

This comment has been minimized.

Copy link
Collaborator

bgoodri commented May 30, 2018

@jsilve24

This comment has been minimized.

Copy link

jsilve24 commented Jun 27, 2018

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

@lucasnell

This comment has been minimized.

Copy link

lucasnell commented Sep 27, 2018

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

This comment has been minimized.

Copy link

jtimonen commented Oct 4, 2018

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

This comment has been minimized.

Copy link

helske commented Oct 12, 2018

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

This comment has been minimized.

Copy link
Collaborator

bgoodri commented Oct 12, 2018

@helske

This comment has been minimized.

Copy link

helske commented Oct 12, 2018

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

This comment has been minimized.

Copy link
Collaborator

bgoodri commented Oct 12, 2018

@const-ae

This comment has been minimized.

Copy link

const-ae commented Oct 23, 2018

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

This comment has been minimized.

Copy link
Collaborator

bgoodri commented Oct 23, 2018

@bart1

This comment has been minimized.

Copy link

bart1 commented Oct 31, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment