[Rd] Problems embedding R.
Simon Knapp
sleepingwell at gmail.com
Sun Jun 18 03:32:58 CEST 2006
Hi List,
I am trying to come to terms with embedding R. I have looked in
writing R extensions and a pdf slideshow (by Simon Urbanek) I found on
the web and by my reckoning the following should almost work (I use
function Rf_initEmbeddedR in the file winembed.c from the Rserv source
to initialise R):
int main(int argc, char* argv[]){
char *cmd = "rnorm(10)";
SEXP cmdSexp, cmdexpr, ans;
int i, Rerror
ParseStatus status;
if(Rf_initEmbeddedR(argc, argv)){
printf("R failed to initialise\n");
return 1;
}
PROTECT(cmdSexp = allocVector(STRSXP, 1));
SET_STRING_ELT(cmdSexp, 0, mkChar(cmd));
PROTECT(cmdexpr = R_ParseVector(cmdSexp, -1, &status));
if (status != PARSE_OK) {
printf("error in parsing command\n");
UNPROTECT(2);
return 1;
}
R_tryEval(VECTOR_ELT(cmdexpr, 1), R_GlobalEnv, &Rerror);
if(Rerror){
printf("error in evaluation attempt\n");
UNPROTECT(2);
return 1;
}
PROTECT(ans = eval(VECTOR_ELT(cmdexpr, 1), R_GlobalEnv));
for(i=0; i<10; ++i) printf("ret %i = %f\n", i, REAL(ans)[i]);
UNPROTECT(3);
return 0;
}
when I run this program I get output:
ret 0 = 0.000000
ret 1 = 0.000000
ret 2 = 0.000000
ret 3 = 0.000000
ret 4 = 0.000000
ret 5 = 0.000000
ret 6 = 0.000000
ret 7 = 0.000000
ret 8 = 0.000000
ret 9 = 0.000000
The reason I say "should almost work" is that I would have thought
that the lines:
R_tryEval(VECTOR_ELT(cmdexpr, 1), R_GlobalEnv, &Rerror), and
PROTECT(ans = eval(VECTOR_ELT(cmdexpr, 1), R_GlobalEnv));
should be:
R_tryEval(VECTOR_ELT(cmdexpr, 0), R_GlobalEnv, &Rerror), and
PROTECT(ans = eval(VECTOR_ELT(cmdexpr, 0), R_GlobalEnv));
respectively... but these give me access violations (Unhandled
exception at 0x10073d34 in R_interface.exe: 0xC0000005: Access
violation reading location 0x515c25ff.)
Can anybody help?
Simon.
R version: 2.2.0
platform: windows
dev env: MS VS 2003
More information about the R-devel
mailing list