[R] Rcpp, dyn.load and C++ problems

Martin Møller Skarbiniks Pedersen traxplayer at gmail.com
Sun Dec 3 02:06:08 CET 2017


  I have written a small C++ function and compile it.
  However in R I can't see the function I have defined in C++.
  I have read some web-pages about Rcpp and C++ but it is a bit confusion
for me.

  This is the C++-code:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
List compute_values_cpp(int totalPoints = 1e5, double angle_increment =
0.01, int radius = 400, double grow = 3.64) {
  double xn = 0.5;
  double angle = 0.1;
  double xn_plus_one, yn_plus_one;
  NumericVector x(totalPoints);
  NumericVector y(totalPoints);

  for (int i=0; i<totalPoints; i++) {
    xn_plus_one = xn*cos(angle)*radius;
    yn_plus_one = xn*sin(angle)*radius;
    angle += angle_increment;
    xn = grow*xn*(1-xn);
    x[i] = xn_plus_one;
    y[i] = yn_plus_one;
  return List::create(Rcpp::Named("x") = x, Rcpp::Named("y") = y);

And I compile it like this:
PKG_CXXFLAGS=$(Rscript -e 'Rcpp:::CxxFlags()') \
PKG_LIBS=$(Rscript -e 'Rcpp:::LdFlags()')  \
R CMD SHLIB logistic_map.cpp
without problems and I get a logistic_map.so file as expected.

However in R:
R> dyn.load("logistic_map.so")
R> compute_values_cpp()
Error in compute_values_cpp() :
  could not find function "compute_values_cpp"

Please advise,
  What piece of the puzzle is missing?

Martin M. S. Pedersen

	[[alternative HTML version deleted]]

More information about the R-help mailing list