Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I transition from using C++ with inline to making my own R package?
    primarykey
    data
    text
    <p>There are two things going on here: (1) I am just learning how to use Rcpp and friends to make my R code faster and (2) I have never built an R package before. </p> <p>I've got the use of <code>inline</code> down. Great package. Love it. However, I can't seem to use any code developed with <code>inline</code> with <code>package.skeleton</code> to make an R package. </p> <p>Here is a minimal example: </p> <h2>Step 1: Make the R script</h2> <pre><code>############################# # File : build-R-pacakge.R # ############################# require(inline) require(Rcpp) require(RcppArmadillo) # Define a simple cxxfunction plus.two.cpp.src &lt;- ' arma::mat U = Rcpp::as&lt;arma::mat&gt;(UmatrixR); return(Rcpp::wrap(U+2)); ' plus.two.cpp &lt;- cxxfunction( signature(UmatrixR="numeric"), body=plus.two.cpp.src, plugin="RcppArmadillo") # Define the analogous R function plus.two.r &lt;- function( x ) { return( x + 2 ) } </code></pre> <h2>Step 2: Build it as an R Package</h2> <p>In new R session run: </p> <pre><code>source('build-R-package.R') RcppArmadillo.package.skeleton(name='inlineExample', list=c('plus.two.cpp', 'plus.two.r'), code_files='build-R-package.R') </code></pre> <p>Then delete the <code>man/*.Rd</code> files since R doesn't put in defaults that 'just work'. </p> <pre><code>$ rm inlineExample/man/*.Rd </code></pre> <p>And run:</p> <pre><code>$ R CMD build inlineExample/ &lt;&lt; snip &gt;&gt; $ R CMD check inlineExample_1.0.tar.gz &lt;&lt; snip &gt;&gt; $ R CMD INSTALL inlineExample_1.0.tar.gz &lt;&lt; snip &gt;&gt; </code></pre> <p>Which all complete successfully, except for some complaining about the missing documentation. </p> <h2>Step 3: Try it out</h2> <p>And then try it out in a new R session: </p> <pre><code>&gt; require(inlineExample) Loading required package: inlineExample Loading required package: Rcpp Loading required package: RcppArmadillo Loading required package: inline &gt; &gt; plus.two.cpp( matrix(1:12, ncol=3)) Error in .Primitive(".Call")(&lt;pointer: (nil)&gt;, UmatrixR) : NULL value passed as symbol address &gt; &gt; plus.two.cpp An object of class "CFunc" function (UmatrixR) .Primitive(".Call")(&lt;pointer: (nil)&gt;, UmatrixR) &lt;environment: 0x2f28370&gt; Slot "code": [1] "\n// includes from the plugin\n#include &lt;RcppArmadillo.h&gt;\n#include &lt;Rcpp.h&gt;\n\n\n#ifndef BEGIN_RCPP\n#define BEGIN_RCPP\n#endif\n\n#ifndef END_RCPP\n#define END_RCPP\n#endif\n\nusing namespace Rcpp;\n\n\n// user includes\n\n\n// declarations\nextern \"C\" {\nSEXP file2f8c4cc10657( SEXP UmatrixR) ;\n}\n\n// definition\n\nSEXP file2f8c4cc10657( SEXP UmatrixR ){\nBEGIN_RCPP\n\n arma::mat U = Rcpp::as&lt;arma::mat&gt;(UmatrixR);\n return(Rcpp::wrap(U+2));\n\nEND_RCPP\n}\n\n\n" </code></pre> <p>It fails. To my untrained eyes it seems that:</p> <ol> <li>the code that <code>inline</code> compiled never got copied by *.package.skeleton to the "right" part of the directory skeleton for later compilation by the R build process, and </li> <li>failing that the 'code_files' argument does not cause R to source those *.R files when the package is loaded. </li> </ol> <p>Thinking about this further, it seems like (2) would be a really bad idea because it would force the user to compile the code every time they loaded their package. That would both exclude all users who require binary only packages and just simply be really inefficient. </p> <p>And (1) seems technically possible, but as <a href="http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2012-July/004026.html" rel="nofollow noreferrer">discussed on the Rcpp mailing list</a>, it is not worth implementing a set of auto-magic converter functions. </p> <p>So, the question: </p> <p><strong>Can someone please give a walk-through of the correct way to take the provided example code and turn it into an R package?</strong> </p> <p>There is just a bit too much newness for me to make sense of the existing documentation. FWIW, I'm sure it would be helpful for people in the future! </p>
    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.
 

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