<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/custom-templated-as-and-wrap-functions-within-rcpp/"></div></div><div><a data-url="http://www.r-bloggers.com/custom-templated-as-and-wrap-functions-within-rcpp/" data-counturl="http://www.r-bloggers.com/custom-templated-as-and-wrap-functions-within-rcpp/" data-count="horizontal" data-text="Custom Templated as and wrap Functions within Rcpp." class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://gallery.rcpp.org//articles/custom-templated-wrap-and-as-for-seamingless-interfaces/"> Rcpp Gallery</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Introduction</h2>
<p>Consider a need to be able to interface with a data type that is not presently supported by Rcpp. The data type might come from a new library or from within ones own program. In such cases, Rcpp is faced with an issue of consciousness as the new data type is not similar to known types so the autocoversion or seamless R to C++ integration cannot be applied correctly. The issue is two fold:</p>
<ol>
<li>Converting from R to C++ (<code>Rcpp::as&lt;T>(obj)</code>)</li>
<li>Converting from C++ to R (<code>Rcpp::wrap(obj)</code>)</li>
</ol>
<p>Luckily, there is a wonderful Rcpp vignette called <a target="_blank" href="https://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-extending.pdf">Extending Rcpp</a> that addresses custom objects. However, the details listed are more abstracted than one would like. So, I’m going to try to take you through the steps with a bit of commentary. Please note that the approach used is via <strong>Templates and partial specialization</strong> and will result in some nice automagic at the end.</p>
<p>The overview of the discussion will focus on:</p>
<ul>
<li>Stage 1 – Forward Declarations</li>
<li>Stage 2 – Including the Rcpp Header</li>
<li>Stage 3 – Implementation of Forward Declarations</li>
<li>Stage 4 – Testing Functionality</li>
<li>Stage 5 – All together</li>
</ul>
<h2>Explanation of Stages</h2>
<h3>Stage 1 – Forward Declarations</h3>
<p>In the first stage, we must declare our intent to the features we wish to use prior to engaging <code>Rcpp.h</code>. To do so, we will load a different header file and add some definitions to the <code>Rcpp::traits</code> namespace.</p>
<p>Principally, when we start writing the file, the first header that we must load is <code>RcppCommon.h</code> and <strong>not</strong> the usual <code>Rcpp.h</code>!! If we do not place the forward declaration prior to the <code>Rcpp.h</code> call, we will be unable to appropriately register our extension.</p>
<p>Then, we must add in the different plugin markup for <code>sourceCpp()</code> to set the appropriate flags during the compilation of the code. After the plugins, we will include the actual boost headers that we want to use. Lastly, we must add two special Rcpp function declaration, <code>Rcpp::as&lt;T>(obj)</code> and <code>Rcpp::wrap(obj)</code>, within <code>Rcpp::traits</code> namespace. To enable multiple types, we must create an <code>Exporter</code> class instead of a more direct call to <code>template &lt;> ClassName as( SEXP )</code>.</p>
<figure>
<pre><code data-lang="cpp"><span>// -------------- Stage 1: Forward Declarations with `RcppCommon.h`
</span>
<span>#include &lt;RcppCommon.h>
</span>
<span>// Flags for C++ compiler
</span>
<span>// [[Rcpp::depends(BH)]]
// [[Rcpp::plugins(&quot;cpp11&quot;)]]
</span>
<span>// Third party library includes that provide the template class of ublas
</span><span>#include &lt;boost/numeric/ublas/matrix_sparse.hpp>
#include &lt;boost/numeric/ublas/matrix.hpp>
</span>
<span>// Provide Forward Declarations
</span><span>namespace</span> <span>Rcpp</span> <span>{</span>

  <span>namespace</span> <span>traits</span><span>{</span>
  
    <span>// Setup non-intrusive extension via template specialization for
</span>    <span>// 'ublas' class boost::numeric::ublas
</span>    
    <span>// Support for wrap
</span>    <span>template</span> <span>&lt;</span><span>typename</span> <span>T</span><span>></span> <span>SEXP</span> <span>wrap</span><span>(</span><span>const</span> <span>boost</span><span>::</span><span>numeric</span><span>::</span><span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>T</span><span>></span> <span>&amp;</span> <span>obj</span><span>);</span>
    
    <span>// Support for as&lt;T>
</span>    <span>template</span> <span>&lt;</span><span>typename</span> <span>T</span><span>></span> <span>class</span> <span>Exporter</span><span>&lt;</span> <span>boost</span><span>::</span><span>numeric</span><span>::</span><span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>T</span><span>></span> <span>></span><span>;</span>
  
  <span>}</span>
<span>}</span></code></pre>
</figure>
<h3>Stage 2 – Include the <code>Rcpp.h</code></h3>
<p>It might seem frivolous to have a stage just to declare import order, but if <code>Rcpp.h</code> is included before the forward declaration then <code>Rcpp::traits</code> is not updated and we enter the abyss.</p>
<p>Thus:</p>
<figure>
<pre><code data-lang="cpp"><span>// -------------- Stage 2: Including Rcpp.h
</span>
<span>// ------ Place &lt;Rcpp.h> AFTER the Forward Declaration!!!!
</span>
<span>#include &lt;Rcpp.h>
</span>

<span>//</span> <span>------</span> <span>Place</span> <span>Implementations</span> <span>of</span> <span>Forward</span> <span>Declarations</span> <span>AFTER</span> <span>&lt;</span><span>Rcpp</span><span>.</span><span>h</span><span>>!</span></code></pre>
</figure>
<h3>Stage 3 – Implementing the Declarations</h3>
<p>Now, we must actually implement the forward declarations. In particular, the only implementation that will be slightly problematic is the <code>as&lt;></code> since the <code>wrap()</code> is straight forward.</p>
<h4><code>wrap()</code></h4>
<p>To implement <code>wrap()</code> we must appeal to a built in type conversion index within Rcpp called <a target="_blank" href="https://github.com/RcppCore/Rcpp/blob/master/inst/include/Rcpp/traits/r_sexptype_traits.h"><code>Rcpp::traits::r_sexptype_traits&lt;T>::rtype</code></a>. From this, we are able to obtain an <code>int</code> containing the <code>RTYPE</code> and then construct an <code>Rcpp::Vector</code>. For the construction of a matrix, the same ideas hold true.</p>
<h4><code>as()</code></h4>
<p>For <code>as&lt;>()</code>, we need to consider the template that will be passed in. Furthermore, we setup a <code>typedef</code> directly underneath the <code>Exporter</code> class definition to easily define an <code>OUT</code> object to be used within the <code>get()</code> method. Outside of that, we use the same trick to move back and forth from a C++ <code>T</code> type to an <code>R</code> type.</p>
<p>In order to accomplish the <code>as&lt;></code>, or the direct port from R to C++, I had to do something dirty: <em>I copied the vector contents</em>. The code that governs this output is given within the <code>get()</code> of the <code>Exporter</code> class. You may wish to spend some time looking into changing the assignment using pointers perhaps. I’m not very well versed with <code>ublas</code> so I did not see an easy approach to resolve the pointer pass.</p>
<figure>
<pre><code data-lang="cpp"><span>// -------------- Stage 3: Implementation the Declarations
</span>
<span>// Define template specializations for as&lt;> and wrap
</span><span>namespace</span> <span>Rcpp</span> <span>{</span>

  <span>namespace</span> <span>traits</span><span>{</span>
  
    <span>// Defined wrap case
</span>    <span>template</span> <span>&lt;</span><span>typename</span> <span>T</span><span>></span> <span>SEXP</span> <span>wrap</span><span>(</span><span>const</span> <span>boost</span><span>::</span><span>numeric</span><span>::</span><span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>T</span><span>></span> <span>&amp;</span> <span>obj</span><span>){</span>
      <span>const</span> <span>int</span> <span>RTYPE</span> <span>=</span> <span>Rcpp</span><span>::</span><span>traits</span><span>::</span><span>r_sexptype_traits</span><span>&lt;</span><span>T</span><span>>::</span><span>rtype</span> <span>;</span>
      
      <span>return</span> <span>Rcpp</span><span>::</span><span>Vector</span><span>&lt;</span> <span>RTYPE</span> <span>></span><span>(</span><span>obj</span><span>.</span><span>begin</span><span>(),</span> <span>obj</span><span>.</span><span>end</span><span>());</span>
    <span>};</span>
    
    
    <span>// Defined as&lt; > case
</span>    <span>template</span><span>&lt;</span><span>typename</span> <span>T</span><span>></span>
    <span>class</span> <span>Exporter</span><span>&lt;</span> <span>boost</span><span>::</span><span>numeric</span><span>::</span><span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>T</span><span>></span> <span>></span> <span>{</span>
      <span>typedef</span> <span>typename</span> <span>boost</span><span>::</span><span>numeric</span><span>::</span><span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>T</span><span>></span> <span>OUT</span> <span>;</span>
      
      <span>// Convert the type to a valid rtype. </span>      <span>const</span> <span>static</span> <span>int</span> <span>RTYPE</span> <span>=</span> <span>Rcpp</span><span>::</span><span>traits</span><span>::</span><span>r_sexptype_traits</span><span>&lt;</span> <span>T</span> <span>>::</span><span>rtype</span> <span>;</span>
      <span>Rcpp</span><span>::</span><span>Vector</span><span>&lt;</span><span>RTYPE</span><span>></span> <span>vec</span><span>;</span>
      
    <span>public</span><span>:</span>
      <span>Exporter</span><span>(</span><span>SEXP</span> <span>x</span><span>)</span> <span>:</span> <span>vec</span><span>(</span><span>x</span><span>)</span> <span>{</span>
        <span>if</span> <span>(</span><span>TYPEOF</span><span>(</span><span>x</span><span>)</span> <span>!=</span> <span>RTYPE</span><span>)</span>
          <span>throw</span> <span>std</span><span>::</span><span>invalid_argument</span><span>(</span><span>&quot;Wrong R type for mapped 1D array&quot;</span><span>);</span>
      <span>}</span>
      <span>OUT</span> <span>get</span><span>()</span> <span>{</span>
        
        <span>// Need to figure out a way to perhaps do a pointer pass?
</span>        <span>OUT</span> <span>x</span><span>(</span><span>vec</span><span>.</span><span>size</span><span>());</span>
        
        <span>std</span><span>::</span><span>copy</span><span>(</span><span>vec</span><span>.</span><span>begin</span><span>(),</span> <span>vec</span><span>.</span><span>end</span><span>(),</span> <span>x</span><span>.</span><span>begin</span><span>());</span> <span>// have to copy data
</span>        
        <span>return</span> <span>x</span><span>;</span>
      <span>}</span>
    <span>}</span> <span>;</span>
  
  <span>}</span>
<span>}</span></code></pre>
</figure>
<h3>Stage 4 – Testing</h3>
<p>Okay, let’s see if what we worked on paid off (<strong>spoiler</strong> It did! <strong>spoiler</strong>). To check, we should look at two different areas:</p>
<ol>
<li>Trace diagnostics within the function and;</li>
<li>An automagic test.</li>
</ol>
<p>Both of which are given below. Note that I’ve opted to shorten the <code>ublas</code> setup to just be:</p>
<figure>
<pre><code data-lang="cpp"><span>// -------------- Stage 4: Testing
</span>
<span>// Here we define a shortcut to the boost ublas class to enable multiple ublas types via a template.
// ublas::vector&lt;T> => ublas::vector&lt;double>, ... , ublas::vector&lt;int>
</span><span>namespace</span> <span>ublas</span> <span>=</span> <span>::</span><span>boost</span><span>::</span><span>numeric</span><span>::</span><span>ublas</span><span>;</span></code></pre>
</figure>
<h4>Trace Diagnostics</h4>
<figure>
<pre><code data-lang="cpp"><span>// [[Rcpp::export]]
</span><span>void</span> <span>containment_test</span><span>(</span><span>Rcpp</span><span>::</span><span>NumericVector</span> <span>x1</span><span>)</span> <span>{</span>
  
  <span>Rcpp</span><span>::</span><span>Rcout</span> <span>&lt;&lt;</span> <span>&quot;Converting from Rcpp::NumericVector to ublas::vector&lt;double>&quot;</span> <span>&lt;&lt;</span> <span>std</span><span>::</span><span>endl</span><span>;</span>
  
  <span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>double</span><span>></span> <span>x</span> <span>=</span> <span>Rcpp</span><span>::</span><span>as</span><span>&lt;</span> <span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>double</span><span>></span> <span>></span><span>(</span><span>x1</span><span>);</span> <span>// initialize the vector to all zero
</span>  
  <span>Rcpp</span><span>::</span><span>Rcout</span> <span>&lt;&lt;</span> <span>&quot;Running output test with ublas::vector&lt;double>&quot;</span> <span>&lt;&lt;</span> <span>std</span><span>::</span><span>endl</span><span>;</span>
  
  <span>for</span> <span>(</span><span>unsigned</span> <span>i</span> <span>=</span> <span>0</span><span>;</span> <span>i</span> <span>&lt;</span> <span>x</span><span>.</span><span>size</span> <span>();</span> <span>++</span> <span>i</span><span>)</span>
    <span>Rcpp</span><span>::</span><span>Rcout</span>  <span>&lt;&lt;</span> <span>x</span><span>(</span><span>i</span><span>)</span> <span>&lt;&lt;</span> <span>std</span><span>::</span><span>endl</span><span>;</span>
  
  <span>Rcpp</span><span>::</span><span>Rcout</span> <span>&lt;&lt;</span> <span>&quot;Converting from ublas::vector&lt;double> to Rcpp::NumericVector&quot;</span> <span>&lt;&lt;</span> <span>std</span><span>::</span><span>endl</span><span>;</span>
  
  <span>Rcpp</span><span>::</span><span>NumericVector</span> <span>test</span> <span>=</span> <span>Rcpp</span><span>::</span><span>wrap</span><span>(</span><span>x</span><span>);</span>
  
  <span>Rcpp</span><span>::</span><span>Rcout</span> <span>&lt;&lt;</span> <span>&quot;Running output test with Rcpp::NumericVector&quot;</span> <span>&lt;&lt;</span> <span>std</span><span>::</span><span>endl</span><span>;</span>
  
  <span>for</span> <span>(</span><span>unsigned</span> <span>i</span> <span>=</span> <span>0</span><span>;</span> <span>i</span> <span>&lt;</span> <span>test</span><span>.</span><span>size</span> <span>();</span> <span>++</span> <span>i</span><span>)</span>
    <span>Rcpp</span><span>::</span><span>Rcout</span>  <span>&lt;&lt;</span> <span>test</span><span>(</span><span>i</span><span>)</span> <span>&lt;&lt;</span> <span>std</span><span>::</span><span>endl</span><span>;</span>
  
<span>}</span></code></pre>
</figure>
<p>Test Call:</p>
<figure>
<pre><code data-lang="r"><span>containment_test</span><span>(</span><span>c</span><span>(</span><span>1</span><span>,</span><span>2</span><span>,</span><span>3</span><span>,</span><span>4</span><span>))</span></code></pre>
</figure>
<p>Results:</p>
<pre>
Converting from Rcpp::NumericVector to ublas::vector&lt;double>
Running output test with ublas::vector&lt;double>
1
2
3
4
Converting from ublas::vector&lt;double> to Rcpp::NumericVector
Running output test with Rcpp::NumericVector
1
2
3
4
</pre>
<p>This test performed as expected. Onto the next test!</p>
<h4>Automagic test</h4>
<figure>
<pre><code data-lang="cpp"><span>// [[Rcpp::export]]
</span><span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>double</span><span>></span> <span>automagic_ublas_rcpp</span><span>(</span><span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>double</span><span>></span> <span>x1</span><span>)</span> <span>{</span>
  <span>return</span> <span>x1</span><span>;</span>
<span>}</span></code></pre>
</figure>
<p>Test Call:</p>
<figure>
<pre><code data-lang="r"><span>automagic_ublas_rcpp</span><span>(</span><span>c</span><span>(</span><span>1</span><span>,</span><span>2</span><span>,</span><span>3.2</span><span>,</span><span>1.2</span><span>))</span></code></pre>
</figure>
<p>Results:</p>
<figure>
<pre><code data-lang="r"><span>automagic_ublas_rcpp</span><span>(</span><span>c</span><span>(</span><span>1</span><span>,</span><span>2</span><span>,</span><span>3.2</span><span>,</span><span>1.2</span><span>))</span></code></pre>
</figure>
<pre>
[1] 1.0 2.0 3.2 1.2
</pre>
<p>Success!</p>
<h3>Stage 5 – All together</h3>
<p>Here is the combination of the above code chunks given by stage. If you copy and paste this into your <code>.cpp</code> file, then everything <em>should</em> work. If not, let me know.</p>
<figure>
<pre><code data-lang="cpp"><span>// -------------- Stage 1: Forward Declarations with `RcppCommon.h`
</span>
<span>#include &lt;RcppCommon.h>
</span>
<span>// Flags for C++ compiler
</span>
<span>// [[Rcpp::depends(BH)]]
// [[Rcpp::plugins(&quot;cpp11&quot;)]]
</span>
<span>// Third party library includes that provide the template class of ublas
</span><span>#include &lt;boost/numeric/ublas/matrix_sparse.hpp>
#include &lt;boost/numeric/ublas/matrix.hpp>
</span>
<span>// Provide Forward Declarations
</span><span>namespace</span> <span>Rcpp</span> <span>{</span>

  <span>namespace</span> <span>traits</span><span>{</span>
  
    <span>// Setup non-intrusive extension via template specialization for
</span>    <span>// 'ublas' class boost::numeric::ublas
</span>    
    <span>// Support for wrap
</span>    <span>template</span> <span>&lt;</span><span>typename</span> <span>T</span><span>></span> <span>SEXP</span> <span>wrap</span><span>(</span><span>const</span> <span>boost</span><span>::</span><span>numeric</span><span>::</span><span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>T</span><span>></span> <span>&amp;</span> <span>obj</span><span>);</span>
    
    <span>// Support for as&lt;T>
</span>    <span>template</span> <span>&lt;</span><span>typename</span> <span>T</span><span>></span> <span>class</span> <span>Exporter</span><span>&lt;</span> <span>boost</span><span>::</span><span>numeric</span><span>::</span><span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>T</span><span>></span> <span>></span><span>;</span>
  
  <span>}</span>
<span>}</span>

<span>// -------------- Stage 2: Including Rcpp.h
</span>
<span>// ------ Place &lt;Rcpp.h> AFTER the Forward Declaration!!!!
</span>
<span>#include &lt;Rcpp.h>
</span>

<span>// ------ Place Implementations of Forward Declarations AFTER &lt;Rcpp.h>!
</span>
<span>// -------------- Stage 3: Implementation the Declarations
</span>
<span>// Define template specializations for as&lt;> and wrap
</span><span>namespace</span> <span>Rcpp</span> <span>{</span>

  <span>namespace</span> <span>traits</span><span>{</span>
  
    <span>// Defined wrap case
</span>    <span>template</span> <span>&lt;</span><span>typename</span> <span>T</span><span>></span> <span>SEXP</span> <span>wrap</span><span>(</span><span>const</span> <span>boost</span><span>::</span><span>numeric</span><span>::</span><span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>T</span><span>></span> <span>&amp;</span> <span>obj</span><span>){</span>
      <span>const</span> <span>int</span> <span>RTYPE</span> <span>=</span> <span>Rcpp</span><span>::</span><span>traits</span><span>::</span><span>r_sexptype_traits</span><span>&lt;</span><span>T</span><span>>::</span><span>rtype</span> <span>;</span>
      
      <span>return</span> <span>Rcpp</span><span>::</span><span>Vector</span><span>&lt;</span> <span>RTYPE</span> <span>></span><span>(</span><span>obj</span><span>.</span><span>begin</span><span>(),</span> <span>obj</span><span>.</span><span>end</span><span>());</span>
    <span>};</span>
    
    
    <span>// Defined as&lt; > case
</span>    <span>template</span><span>&lt;</span><span>typename</span> <span>T</span><span>></span>
    <span>class</span> <span>Exporter</span><span>&lt;</span> <span>boost</span><span>::</span><span>numeric</span><span>::</span><span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>T</span><span>></span> <span>></span> <span>{</span>
      <span>typedef</span> <span>typename</span> <span>boost</span><span>::</span><span>numeric</span><span>::</span><span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>T</span><span>></span> <span>OUT</span> <span>;</span>
      
      <span>// Convert the type to a valid rtype. </span>      <span>const</span> <span>static</span> <span>int</span> <span>RTYPE</span> <span>=</span> <span>Rcpp</span><span>::</span><span>traits</span><span>::</span><span>r_sexptype_traits</span><span>&lt;</span> <span>T</span> <span>>::</span><span>rtype</span> <span>;</span>
      <span>Rcpp</span><span>::</span><span>Vector</span><span>&lt;</span><span>RTYPE</span><span>></span> <span>vec</span><span>;</span>
      
    <span>public</span><span>:</span>
      <span>Exporter</span><span>(</span><span>SEXP</span> <span>x</span><span>)</span> <span>:</span> <span>vec</span><span>(</span><span>x</span><span>)</span> <span>{</span>
        <span>if</span> <span>(</span><span>TYPEOF</span><span>(</span><span>x</span><span>)</span> <span>!=</span> <span>RTYPE</span><span>)</span>
          <span>throw</span> <span>std</span><span>::</span><span>invalid_argument</span><span>(</span><span>&quot;Wrong R type for mapped 1D array&quot;</span><span>);</span>
      <span>}</span>
      <span>OUT</span> <span>get</span><span>()</span> <span>{</span>
        
        <span>// Need to figure out a way to perhaps do a pointer pass?
</span>        <span>OUT</span> <span>x</span><span>(</span><span>vec</span><span>.</span><span>size</span><span>());</span>
        
        <span>std</span><span>::</span><span>copy</span><span>(</span><span>vec</span><span>.</span><span>begin</span><span>(),</span> <span>vec</span><span>.</span><span>end</span><span>(),</span> <span>x</span><span>.</span><span>begin</span><span>());</span> <span>// have to copy data
</span>        
        <span>return</span> <span>x</span><span>;</span>
      <span>}</span>
    <span>}</span> <span>;</span>
  
  <span>}</span>
<span>}</span>

<span>// -------------- Stage 4: Testing
</span>
<span>// Here we define a shortcut to the boost ublas class to enable multiple ublas types via a template.
// ublas::vector&lt;T> => ublas::vector&lt;double>, ... , ublas::vector&lt;int>
</span><span>namespace</span> <span>ublas</span> <span>=</span> <span>::</span><span>boost</span><span>::</span><span>numeric</span><span>::</span><span>ublas</span><span>;</span>


<span>// [[Rcpp::export]]
</span><span>void</span> <span>containment_test</span><span>(</span><span>Rcpp</span><span>::</span><span>NumericVector</span> <span>x1</span><span>)</span> <span>{</span>
  
  <span>Rcpp</span><span>::</span><span>Rcout</span> <span>&lt;&lt;</span> <span>&quot;Converting from Rcpp::NumericVector to ublas::vector&lt;double>&quot;</span> <span>&lt;&lt;</span> <span>std</span><span>::</span><span>endl</span><span>;</span>
  
  <span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>double</span><span>></span> <span>x</span> <span>=</span> <span>Rcpp</span><span>::</span><span>as</span><span>&lt;</span> <span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>double</span><span>></span> <span>></span><span>(</span><span>x1</span><span>);</span> <span>// initialize the vector to all zero
</span>  
  <span>Rcpp</span><span>::</span><span>Rcout</span> <span>&lt;&lt;</span> <span>&quot;Running output test with ublas::vector&lt;double>&quot;</span> <span>&lt;&lt;</span> <span>std</span><span>::</span><span>endl</span><span>;</span>
  
  <span>for</span> <span>(</span><span>unsigned</span> <span>i</span> <span>=</span> <span>0</span><span>;</span> <span>i</span> <span>&lt;</span> <span>x</span><span>.</span><span>size</span> <span>();</span> <span>++</span> <span>i</span><span>)</span>
    <span>Rcpp</span><span>::</span><span>Rcout</span>  <span>&lt;&lt;</span> <span>x</span><span>(</span><span>i</span><span>)</span> <span>&lt;&lt;</span> <span>std</span><span>::</span><span>endl</span><span>;</span>
  
  <span>Rcpp</span><span>::</span><span>Rcout</span> <span>&lt;&lt;</span> <span>&quot;Converting from ublas::vector&lt;double> to Rcpp::NumericVector&quot;</span> <span>&lt;&lt;</span> <span>std</span><span>::</span><span>endl</span><span>;</span>
  
  <span>Rcpp</span><span>::</span><span>NumericVector</span> <span>test</span> <span>=</span> <span>Rcpp</span><span>::</span><span>wrap</span><span>(</span><span>x</span><span>);</span>
  
  <span>Rcpp</span><span>::</span><span>Rcout</span> <span>&lt;&lt;</span> <span>&quot;Running output test with Rcpp::NumericVector&quot;</span> <span>&lt;&lt;</span> <span>std</span><span>::</span><span>endl</span><span>;</span>
  
  <span>for</span> <span>(</span><span>unsigned</span> <span>i</span> <span>=</span> <span>0</span><span>;</span> <span>i</span> <span>&lt;</span> <span>test</span><span>.</span><span>size</span> <span>();</span> <span>++</span> <span>i</span><span>)</span>
    <span>Rcpp</span><span>::</span><span>Rcout</span>  <span>&lt;&lt;</span> <span>test</span><span>(</span><span>i</span><span>)</span> <span>&lt;&lt;</span> <span>std</span><span>::</span><span>endl</span><span>;</span>
  
<span>}</span>


<span>// [[Rcpp::export]]
</span><span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>double</span><span>></span> <span>automagic_ublas_rcpp</span><span>(</span><span>ublas</span><span>::</span><span>vector</span><span>&lt;</span><span>double</span><span>></span> <span>x1</span><span>)</span> <span>{</span>
  <span>return</span> <span>x1</span><span>;</span>
<span>}</span></code></pre>
</figure>
<h2>Closing Remarks</h2>
<p>Whew… That was a lot. Hopefully, the above provided enough information as you may want to extend this post’s content past <code>1D</code> vectors to perhaps a <code>ublas::matrix</code> and so on. In addition, the you now have the autoconvert magic of <code>Rcpp</code> for <code>ublas::vector&lt;double></code>! Moreover, all one needs to do is specify the either the parameters or return type of the function to be <code>ublas::vector&lt;double></code> and voila conversion!</p>
<p><img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/rcpp-gallery/~4/A-6rWzpBTbk"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/custom-templated-as-and-wrap-functions-within-rcpp/"></div></div><div><a data-url="http://www.r-bloggers.com/custom-templated-as-and-wrap-functions-within-rcpp/" data-counturl="http://www.r-bloggers.com/custom-templated-as-and-wrap-functions-within-rcpp/" data-count="vertical" data-text="Custom Templated as and wrap Functions within Rcpp." class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://gallery.rcpp.org//articles/custom-templated-wrap-and-as-for-seamingless-interfaces/"> Rcpp Gallery</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/gw401YK5Oo8"></a></div>