Calling compiled C from R with .C ()

I am trying to call a program (function getNBDensitiesin a C executable measurementDensities_out) from R. The function is passed in multiple arrays and a variable double runsum. Right now, the function getNBDensitiesbasically does nothing: it displays the values ​​of the passed parameters. My problem is the syntax of the function call:

array(.C("getNBDensities",
            hr = as.double(hosp.rate), # a vector (s x 1)
            sp = as.double(samplingProbabilities), # another vector (s x 1)
            odh = as.double(odh), # another vector (s x 1)
            simCases = as.integer(x[c("xC1","xC2","xC3")]), # another vector (s x 1)
            obsCases = as.integer(y[c("yC1","yC2","yC3")]), # another vector (s x 1)
            runsum = as.double(runsum), # double
            DUP = TRUE, NAOK = TRUE, PACKAGE = "measurementDensities_out")$f,
            dim = length(y[c("yC1","yC2","yC3")]),
            dimnames = c("yC1","yC2","yC3"))

The error I get after the function is executed correctly (i.e. the right output is printed to the screen),

Error in dim(data) <- dim : attempt to set an attribute on NULL

I don’t understand what are the dimensions that I should pass the function: should it be sx 5 + 1 (five vectors of length sand one double)? I tried all kinds of combinations (including sx5 + 1) and found only seemingly contradictory descriptions / examples online about what should happen here.

For those interested, the C code is below:

#include <R.h>
#include <Rmath.h>
#include <math.h>
#include <Rdefines.h>
#include <R_ext/PrtUtil.h>
#define NUM_STRAINS 3
#define DEBUG
void getNBDensities(  double *hr, double *sp, double *odh, int *simCases, int *obsCases, double *runsum );
void getNBDensities( double *hr, double *sp, double *odh, int *simCases, int *obsCases, double *runsum ) {
#ifdef DEBUG
  for ( int s = 0; s < NUM_STRAINS; s++ ) {
    Rprintf("\nFor strain %d",s); 
    Rprintf("\n\tHospitalization rate = %lg", hr[s]);
    Rprintf("\n\tSimulation probability = %lg",sp[s]);
    Rprintf("\n\tSimulated cases = %d",simCases[s]);
    Rprintf("\n\tObserved cases = %d",obsCases[s]);
    Rprintf("\n\tOverdispersion parameter = %lg",odh[s]);
}
  Rprintf("\nRunning sum = %lg",runsum[0]);
#endif
}
code>

naive decision

Although better (i.e. potentially faster or syntactically understandable) solutions may exist (see Dirk's answer below), the following code simplification works:

     out<-.C("getNBDensities",
            hr = as.double(hosp.rate),
            sp = as.double(samplingProbabilities),
            odh = as.double(odh),
            simCases = as.integer(x[c("xC1","xC2","xC3")]),
            obsCases = as.integer(y[c("yC1","yC2","yC3")]),
            runsum = as.double(runsum))

Access to variables can be obtained at >out.

+5
source share
1 answer

You might want to look into the inline package , which makes compiling, linking, and loading C, C ++, or Fortran code an absolute breeze.

However, if C ++ is another option for you, also look at the Rcpp package , which allows you to pass both R and C ++ Objects back and forth with ease.

+4
source

All Articles