[R] Rcpp, dyn.load and C++ problems
Dirk Eddelbuettel
edd at debian.org
Sun Dec 3 20:19:52 CET 2017
Martin,
You are making your life way too complicated.
There are a number of things I would do differently:
0) Wrong list. Rcpp has its down, rcpp-devel, and I basically do not read
this and would have missed this were it not for luck.
On 3 December 2017 at 02:06, Martin Møller Skarbiniks Pedersen wrote:
| I have read some web-pages about Rcpp and C++ but it is a bit confusion
| for me.
1) Keep reading.
| 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.
2) Possible but too complicated. Read on.
| However in R:
| R> dyn.load("logistic_map.so")
3) You never ever need that with Rcpp and its tool, unless you insist on
redoing thing by hand in which case you _must_ use SEXP .Call(SEXP a, ...)
| Please advise,
| What piece of the puzzle is missing?
4) Keep reading. I will pivot to you other mails. Solution below.
On 3 December 2017 at 20:00, Martin Møller Skarbiniks Pedersen wrote:
| Hi,
| It is still not working.
| $ ./compile.sh
5) Still wrong.
On 3 December 2017 at 20:04, Martin Møller Skarbiniks Pedersen wrote:
| Thanks. However search for "Rcpp calling C++ functions from R" gives a lot
| of result but I think
| some of them are outdated and others don't agree with each other.
|
| Can you point to a specific good on-line guide for me?
6) Call me crazy but maybe the nine vignettes included with the package?
In essence you _complitely_ missed what Rcpp Attributes does and shows, as
does the (newer) Rcpp Introduction vignette.
More crazy, your file was actually 100% correct. I just added three lines to
_also_ execute R code (and I indented just for clarity)
/*** R
res <- compute_values_cpp()
str(res)
*/
Then in R:
R> library(Rcpp)
R> sourceCpp("/tmp/mmsp.cpp")
R> res <- compute_values_cpp()
R> str(res)
List of 2
$ x: num [1:100000] 199 362 118 302 262 ...
$ y: num [1:100000] 20 40 14.3 39.5 36.9 ...
R>
_One call_ of sourceCpp() compiles AND links AND loads AND runs the example R
code (which is optional).
For reference, the current mmsp.cpp follows.
Dirk
#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);
}
/*** R
res <- compute_values_cpp()
str(res)
*/
--
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
More information about the R-help
mailing list