// // REngine.m // Rgui // // Created by Simon Urbanek on Wed Dec 10 2003. // Copyright (c) 2003-4 Simon Urbanek. All rights reserved. // #import "REngine.h" #import #include "Rinit.h" #include #include #include static REngine* mainRengine=nil; extern void run_Rmainloop(); // run REPL (in libR.dylib) @implementation REngine + (REngine*) mainEngine { if (mainRengine==nil) mainRengine=[[REngine alloc] init]; return mainRengine; } - (REngine*) init { replHandler=nil; return self; } - (REngine*) initWithHandler: (id ) hand { // we should check whether R is already initialized ... somehow ... char *args[3]={ "R", "--no-save", 0 }; replHandler=hand; mainRengine=self; setenv("R_HOME","/Library/Frameworks/R.framework/Resources",1); setenv("DYLD_LIBRARY_PATH","/Library/Frameworks/R.framework/Resources/bin",1); initR(2, args); return self; } - (void) runREPL { run_Rmainloop(); } - (id) getHandler { return replHandler; } - (RSEXP*) parse: (NSString*) str { return [self parse: str withParts: 1]; } - (RSEXP*) parse: (NSString*) str withParts: (int) count { ParseStatus ps; SEXP pstr, cv; PROTECT(cv=allocVector(STRSXP, 1)); SET_STRING_ELT(cv, 0, mkChar([str cString])); pstr=R_ParseVector(cv, count, &ps); UNPROTECT(1); //NSLog(@"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; //NSLog(@"evaluateExpressions: %@", expr); // if we have an entire expression list, evaluate its contents one-by-one and return only the last one if ([expr type]==EXPRSXP) { l=[expr length]; while (i