rcpp

Rcpp: why I can not run the function in my defined package?

こ雲淡風輕ζ 提交于 2020-03-01 01:55:48
问题 I use the following steps to achieve my own package: 1)I try to write a very simple function as follows: #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] int foo() { return 6; } 2) I use skeleton to make it into a package: Rcpp.package.skeleton("newpackage",example_code=FALSE,cpp_files=c("New.cpp")) 3) I run cpp with command: source("~/newpackage/src/New.cpp") 4) run compileAttributes to load the package: compileAttributes(pkgdir="/home/tw72/newpackage",verbose = getOption("verbose

Simplified example using Rcpp to link an object file to a function [duplicate]

不问归期 提交于 2020-01-30 11:44:53
问题 This question already has answers here : Rcpp - sourceCpp - undefined symbol (2 answers) Closed 2 years ago . I have existing C code that consists of three files: a header file ( a ".h" file), a library file (a ".o" file), and a source file. They currently run under UNIX and as compiled "mex files" in Matlab. I would like to port them to R using Rcpp. They are all long and complex, so I made a minimal example to help me understand how to port them to R. The simplified header file (my_header.h

Rcpp sugar commands in armadillo

倾然丶 夕夏残阳落幕 提交于 2020-01-30 08:59:04
问题 I'm trying to use ifelse() command of Rcpp sugar with arma::vec . The code fails with error 'ifelse' was not declared in this scope I could not find a solution. A simple example code (resulted with error) is below. #include <RcppArmadillo.h> // [[Rcpp::depends(RcppArmadillo)]] // [[Rcpp::export]] arma::vec f(arma::vec x, arma::vec y) { arma::vec res1 = Rcpp::ifelse(x < y, x, y); arma::vec res = trans(res1)*y; return res; } /*** R f(c(1,2,3),c(3,2,1)) */ 回答1: Using Armadillo's advanced

Getting r function args from Rcpp c++ function

两盒软妹~` 提交于 2020-01-30 08:43:04
问题 I have defined a function on the R-side like this: foo <- function(arg1, arg2, arg3) { ... } and a function in c++ using Rcpp that gets the global environment and instantiates the R function to execute it from that function. Here is the code: namespace Rcpp; void myFunction() { ... Environment env = Environment::global_env(); Function funct = env["foo"]; ... } It works fine, but I would like to check that the R function has exactly 3 args. How can I get the number of args of the R function in

Rcpp parallelize functions that return XPtr? [duplicate]

房东的猫 提交于 2020-01-25 19:59:18
问题 This question already has answers here : Using Rcpp within parallel code via snow to make a cluster (3 answers) Closed 3 years ago . Taking an example XPtr function: test.cpp #include <Rcpp.h> // [[Rcpp::export]] SEXP funx() { /* creating a pointer to a vector<int> */ std::vector<int>* v = new std::vector<int> ; v->push_back( 1 ) ; v->push_back( 2 ) ; /* wrap the pointer as an external pointer */ /* this automatically protected the external pointer from R garbage collection until p goes out

Rcpp: cannot open shared object file

落花浮王杯 提交于 2020-01-25 03:59:27
问题 I'm trying to develop an R package, which makes use of Arrayfire, thanks to Rcpp library. I've started writing a sample code (let's name it hello_world.cpp ) which looks like this: #include <arrayfire.h> // [[Rcpp::export]] bool test_array_fire(){ af::randu(1, 4); return true; } Then, I tried to compile it using a sourceCpp function Rcpp::sourceCpp('src/hello_world.cpp') My first suprise was the fact I had to set some flags manually ( sourceCpp seems to ignore Makevars config when compiling a

Using list as an input in C++ code and calling using Rcpp(List inputs are extremely slow)

耗尽温柔 提交于 2020-01-24 01:33:10
问题 I am trying to use a list(R object) as an input for C++ function and later call it using Rcpp from R. This list contains large number of matrices. The code I provide is a toy example. I have a very complicated code that I have already written but very inefficient. In the following code, I want to know if there is an efficient way of extracting matrix from the list. Following is the code that I have tried. It works but it also tells me that the subscripted value is not an array, pointer or

Extending Rcpp function to input vector of any type

不想你离开。 提交于 2020-01-23 02:51:27
问题 I have following function which does a simple loop on NumericVector and returns int type value. Rcpp::cppFunction({' int calc_streak( NumericVector x, int i1, int i2){ int cur_streak=1; if (NumericVector::is_na(x[0])){ cur_streak = NumericVector::get_na(); } else { cur_streak = 1; } for(int j = i1; j <= i2 ; ++j) { if( x[ j ] == x[ j-1 ]){ cur_streak += 1; } else if(NumericVector::is_na( x[ j ] )){ cur_streak = NumericVector::get_na(); } else { cur_streak = 1; } } return cur_streak; } "})

returning a custom object from a wrapped method in Rcpp

不羁岁月 提交于 2020-01-22 05:24:04
问题 I have the following problem with the Rcpp module: let's assume I've two classes in a Rcpp module class A { public: int x; }; class B public: A get_an_a(){ A an_a(); an_a.x=3; return an_a; } }; RCPP_MODULE(mod){ using namespace Rcpp ; class_<A>("A") .constructor() .property("x",&A::get_x) ; class_<B>("B) .constructor() .method("get_an_A",&get_an_a) ; } . Right now compilation fails as it does not know what to do with the return type of A. I figured I could do something with Rcpp::Xptr,

Do I need to import RccpEigen in the DESCRIPTION file for an R package using it, or is “LinkingTo” enough?

≡放荡痞女 提交于 2020-01-21 08:36:11
问题 I used the RcppEigen.package.skeleton() as a template for adding a small function to an existing R package, so that my DESCRIPTION file now has the lines: Imports: Rcpp (>= 0.11.3), RcppEigen (>= 0.3.2.3.0) LinkingTo: Rcpp, RcppEigen However, doing R CMD check --as-cran <myPackageName_1.0.0>.tar.gz gives the following: "Package in Depends/Imports which should probably only be in LinkingTo: 'RcppEigen'" The Writing R Extensions page says: "Specifying a package in ‘LinkingTo’ suffices if these