<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/making-your-c-less-noteworthy/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://localhost:4000/2018/01/12/Making-Your-.C-Less-NOTEworthy.html"> MeanMean</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>

<p></p>
<p>
If you are a package maintainer, you may have noticed the following new notes from your code checks:<br>
<code>Found no calls to: ‘R_registerRoutines’, ‘R_useDynamicSymbols’</code> If you are using <code>Rcpp</code> you can easily fix this by refreshing the auto-generated function registration. However, if you have a lot of C code that uses the <code>.C()</code> interface, then you need to make a few changes. In this blog post, I’ll use my updated <a rel="nofollow" href="https://github.com/jlisic/meanShiftR" target="_blank"><i>meanShiftR</i> package</a> as an example of how to quickly fix your code.
</p>
<p>
The first thing you need to do is to examine your C code and determine your parameter types for each of the functions you call from R using <code>.C()</code>. In <i>meanShiftR</i> I have two C functions that I call from R. The first function <code>R_meanShift</code> has a function prototype that looks like this:
</p>
<pre><code>
/* mean shift function prototype*/
void R_meanShift(
  double * query,                /* data to query for, in row major form */
  double * train,                /* reference data, in row major form */
  int * assignment,              /* assignment */
  int * queryRowPtr,             /* number of rows of query data */
  int * trainRowPtr,             /* number of rows of reference data */
  int * queryColPtr,             /* number of columns of query and reference*/
  int * nNeighborsPtr,           /* number of neighbors */
  int * iterationsPtr,           /* number of iterations */
  double * bandwidth,            /* bandwidth */
  double * alphaPtr,             /* ms to newton ajustment parameter */
  double * epsilonPtr,           /* min l2 dist for convergence  */
  double * epsilonClusterPtr,    /* min l2 dist for clustering */
  int * kernelEnumPtr,           /* kernel type */
  int * algorithmEnumPtr,        /* algorithm type */
  int * intParameters,           /* kernel and alg dependent parameters */
  int * dblParameters            /* kernel and alg dependent parameters */
  );
</code></pre>
<p>
The second function <code>R_knn</code>has a function prototype that looks like this:
</p>
<pre><code>
/* R_knn function prototype */
void R_knn( 
  double * queryPoints,  /* point to query for */
  double * x,            /* data to reference for the query 
                            (what we build our tree from) */
  int * xnrowPtr,        /* number of rows of x */
  int * nrowPtr,         /* number of rows from query points */
  int * ncolPtr,         /* number of columns for both queryPoints and x */
  double * kDist,        /* distance vector (we return this) */
  int * indexInt,        /* index of neighbors (we return this) */
  int * kPtr,            /* number of nearest neighbors */
  double * weight,       /* used for weighted distance calculations */
  int * leafSizePtr,     /* number of nodes in the k-d tree leaf nodes */
  double * maxDist       /* maximum distance l2^2 to look for neighbors */
 ); 
</code></pre>
<p>
With this information, I can register these function in four pretty painless steps. (1) Include the R_ext/Rdynload.h in a header file. (2) Create <code>R_NativePrimitiveArgType</code>s for each C function that you call with <code>.C()</code>.<br>
(3) Register our C functions as an R_CMethodDef array. (4) Finally, we create a function that is run when we load our function through R; in this function we register our C functions using <code>R_registerRoutines</code>.
</p>
<p>
To detail this process, I have created an example C file identifying each of these steps. This isn’t necessary, but can simplify things if you call C functions from R that have prototypes defined in separate files. To make things easy to apply to your code, I have included a complete example below, and will then go over each of these steps.
</p>
<pre><code>
/* init.c */
#include &lt;stdio.h> 
#include &lt;stdlib.h>

#include &quot;R.h&quot;
#include &quot;Rinternals.h&quot;
#include &quot;Rmath.h&quot;

/*************************************/
/* STEP:  1. This header file is new */
/*************************************/
#include </code></pre>