Skip to content

Instantly share code, notes, and snippets.

@DavisVaughan
Created September 24, 2017 22:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavisVaughan/6597eac027303443bd69809b74646372 to your computer and use it in GitHub Desktop.
Save DavisVaughan/6597eac027303443bd69809b74646372 to your computer and use it in GitHub Desktop.
// [[Rcpp::depends(xtensor)]]
#include <numeric>
#include "xtensor/xmath.hpp"
#include "xtensor-r/rarray.hpp"
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::plugins(cpp14)]]
// [[Rcpp::export]]
double sum_of_sines(xt::rarray<double>& m)
{
auto sines = xt::sin(m);
return std::accumulate(sines.cbegin(), sines.cend(), 0.0);
}
// You can include R code blocks in C++ files processed with sourceCpp
// (useful for testing and development). The R code will be automatically
// run after the compilation.
//
/*** R
sum_of_sines(42)
*/
@DavisVaughan
Copy link
Author

Running sourceCpp (or clicking Source in RStudio) gives:

> Rcpp::sourceCpp('~/Desktop/Untitled.cpp')

> sum_of_sines(42)
[1] -0.9165215

@DavisVaughan
Copy link
Author

With the "attribute" defined at the top of the code, RStudio actually is able to recognize that I want to use xtensor and code completes things for me.

screen shot 2017-09-24 at 6 35 14 pm

Without it, that code completion is not recognized

screen shot 2017-09-24 at 6 35 35 pm

@alexpghayes
Copy link

Ugh I think it's a windows/C++ thing blergh

image

@DavisVaughan
Copy link
Author

DavisVaughan commented Sep 24, 2017

@alexpghayes

I think this is heavily related to your closed issue here. I am absolutely no expert but maybe they need to use PKG_CPPFLAGS instead of PKG_CXXFLAGS in this file. I only say that because that seems to be what Rcpp does here?

It just looks like it's either not finding your C++14 or it was called incorrectly or something.

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