// // REngine.m // Rgui // // Created by Simon Urbanek on Wed Dec 10 2003. // Copyright (c) 2003 __MyCompanyName__. All rights reserved. // #import "REngine.h" #include #include #include #include #include extern void (*ptr_R_Suicide)(char *); extern void (*ptr_R_ShowMessage)(); extern int (*ptr_R_ReadConsole)(char *, unsigned char *, int, int); extern void (*ptr_R_WriteConsole)(char *, int); extern void (*ptr_R_ResetConsole)(); extern void (*ptr_R_FlushConsole)(); extern void (*ptr_R_ClearerrConsole)(); extern Rboolean R_Interactive; extern FILE* R_Outputfile; extern FILE* R_Consolefile; id RE_consoleReceiver=nil; void RE_WriteConsole(char *buf, int len) { NSString *str=[NSString stringWithCString:buf length:len]; if (RE_consoleReceiver!=nil) [RE_consoleReceiver consoleWrite: str]; } static REngine* mainRengine=nil; @implementation REngine int state=0; + (REngine*) mainEngine { if (mainRengine==nil) mainRengine=[[REngine alloc] init]; return mainRengine; } - (REngine*) init { char *Rargv[4], *c; CFBundleRef requestedBundle; CFURLRef rdir; CFStringRef rhome; char buf[512]; // Look for a bundle using its identifier requestedBundle = CFBundleGetBundleWithIdentifier(CFSTR("org.r-project.R-framework") ); if (!requestedBundle) return nil; rdir=CFBundleCopyBundleURL(requestedBundle); CFRelease(requestedBundle); rhome=CFURLCopyFileSystemPath(rdir,kCFURLPOSIXPathStyle); CFRelease(rdir); CFStringGetCString(rhome,buf,512,kCFStringEncodingUTF8); CFRelease(rhome); strcat(buf,"/Resources"); printf("R_HOME: %s\n",buf); setenv("R_HOME",buf,1); strcat(buf,"/bin:"); /* add $_RHOME/bin to LD..PATH just in case something is linked w/o path */ c=getenv("DYLD_LIBRARY_PATH"); if (c) strcat(buf,c); setenv("DYLD_LIBRARY_PATH",buf,1); Rargv[0]="Rgui"; Rargv[1]="--vanilla"; Rargv[2]=0; printf("starting R ...\n"); Rf_initialize_R(2, Rargv); printf("done.\n"); state=1; ptr_R_WriteConsole = RE_WriteConsole; R_Interactive=-1; R_Outputfile=0; R_Consolefile=0; /* printf("wc.orig=%x\n",ptr_R_WriteConsole); ptr_R_WriteConsole = Rgui_WriteConsole; printf("wc.new=%x\n",ptr_R_WriteConsole); ptr_R_ShowMessage = Rgui_ShowMessage; (*ptr_R_ShowMessage)("Testing ShowMessage\n"); setup_Rmainloop(); */ return self; } - (void) setupLoop { if (state==0) [self init]; if (state==1) { setup_Rmainloop(); state=2; } } - (RSEXP*) parse: (NSString*) str { return [self parse: str withParts: 1]; } - (RSEXP*) parse: (NSString*) str withParts: (int) count { ParseStatus ps; SEXP pstr, cv; if (state<2) [self setupLoop]; PROTECT(cv=allocVector(STRSXP, 1)); SET_VECTOR_ELT(cv, 0, mkChar([str cString])); pstr=R_ParseVector(cv, count, &ps); UNPROTECT(1); printf("parse status: %d, SEXP: %x, type: %d\n",ps, pstr, TYPEOF(pstr)); return [[RSEXP alloc] initWithSEXP: pstr]; } - (RSEXP*) evaluateExpressions: (RSEXP*) expr { SEXP es; int er=0; int i=0,l; /* printf("to eval: SEXP %x,length=%d, type=%d\n",[expr directSEXP], [expr length], [expr type]); es=R_tryEval([expr directSEXP], R_GlobalEnv, &er); printf("er=%d\n",er); return [[RSEXP alloc] initWithSEXP: es]; */ if ([expr type]==EXPRSXP) { printf("to eval: SEXP length=%d, type=%d\n",[expr length], [expr type]); l=[expr length]; while (i