Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>body</code> is for the body of the function, you want to look at the <code>includes</code> argument of <code>cxxfunction</code>: </p> <pre><code>library(inline) library(Rcpp) a = 1:10 cpp.fun = cxxfunction(signature(data1="numeric"), plugin="Rcpp", body=' IntegerVector fun_data = data1; int n = fun_data.size(); for(int i=0;i&lt;n;i++){ fun_data[i] = fun1(fun_data[i]); } return(fun_data); ', includes = ' int fun1( int a1){ int b1 = a1; b1 = b1*b1; return(b1); } ' ) cpp.fun( a ) </code></pre> <p><code>?cxxfunction</code> has detailed documentation about its <code>includes</code> argument. </p> <p>But note that this version will make in place modifications in your input vector, which is probably not what you want. Another version that also takes advantage of <code>Rcpp</code> version of <code>sapply</code>: </p> <pre><code>library(inline) library(Rcpp) a = 1:10 cpp.fun = cxxfunction(signature(data1="numeric"), plugin="Rcpp", body=' IntegerVector fun_data = data1; IntegerVector out = sapply( fun_data, fun1 ) ; return(out); ', includes = ' int fun1( int a1){ int b1 = a1; b1 = b1*b1; return(b1); } ' ) cpp.fun( a ) a </code></pre> <p>Finally, you definitely should have a look at <code>sourceCpp</code>. With it you would write your code in a <code>.cpp</code> file, containing: </p> <pre><code>#include &lt;Rcpp.h&gt; using namespace Rcpp ; int fun1( int a1){ int b1 = a1; b1 = b1*b1; return(b1); } // [[Rcpp::export]] IntegerVector fun(IntegerVector fun_data){ IntegerVector out = sapply( fun_data, fun1 ) ; return(out); } </code></pre> <p>And then, you can just <code>sourceCpp</code> your file and invoke the function :</p> <pre><code>sourceCpp( "file.cpp" ) fun( 1:10 ) # [1] 1 4 9 16 25 36 49 64 81 100 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload