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),
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),
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.