r-lib / cpp11

cpp11 helps you to interact with R objects using C++ code.
https://cpp11.r-lib.org/
Other
193 stars 46 forks source link

How to pass parameters in .R file to cpp11 function in .cpp file #313

Closed Eunsolfs closed 1 year ago

Eunsolfs commented 1 year ago

I'm new to the R language and I'm trying to use the R11 package. But I have some problems: How to pass parameters in .R file to cpp function in .cpp file

cpp11 function:

// [[cpp11::register]]
double sum(cpp11::doubles x) {
   return std::accumulate(x. begin(), x. end(), 0.0);
}

So how should I write the R code so that I can pass parameters to the sum function

Currently the code I have written is

cpp11::doubles x = {1.0, 2.0, 3.0, 4.0, 5.0};//Editor reports an error
print(sum(x))

Looking forward to your help, thank you very much!!!

Eunsolfs commented 1 year ago

library(cpp11) cpp11::cpp_source("cpp11test.cpp") x = c(1,2,3.3,5.3) print(sum(x))