-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathr_test.c
76 lines (63 loc) · 1.56 KB
/
r_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Example of how to call an R function from C using an embdeed R
* environment.
*
* Author: Parker Abercrombie <[email protected]>
*/
#include <stdio.h>
#include <string.h>
#include <Rinternals.h>
#include <Rembedded.h>
/**
* Invokes the command source("foo.R").
*/
void source(const char *name)
{
SEXP e;
PROTECT(e = lang2(install("source"), mkString(name)));
R_tryEval(e, R_GlobalEnv, NULL);
UNPROTECT(1);
}
/**
* Wrapper for R function add1, defined in func.R.
*/
void R_add1(int alen, int a[])
{
// Allocate an R vector and copy the C array into it.
SEXP arg;
PROTECT(arg = allocVector(INTSXP, alen));
memcpy(INTEGER(arg), a, alen * sizeof(int));
// Setup a call to the R function
SEXP add1_call;
PROTECT(add1_call = lang2(install("add1"), arg));
// Execute the function
int errorOccurred;
SEXP ret = R_tryEval(add1_call, R_GlobalEnv, &errorOccurred);
if (!errorOccurred)
{
printf("R returned: ");
double *val = REAL(ret);
for (int i = 0; i < LENGTH(ret); i++)
printf("%0.1f, ", val[i]);
printf("\n");
}
else
{
printf("Error occurred calling R\n");
}
UNPROTECT(2);
}
int main(int argc, char *argv[])
{
// Intialize the R environment.
int r_argc = 2;
char *r_argv[] = { "R", "--silent" };
Rf_initEmbeddedR(r_argc, r_argv);
int arg[] = { 1, 2, 3, 4, 5 };
// Invoke a function in R
source("func.R");
R_add1(5, arg);
// Release R environment
Rf_endEmbeddedR(0);
return(0);
}