1 #import "REnvironment.h"
2 #include <Rinternals.h>
3 #include <R_ext/Callbacks.h>
7 @implementation REnvironment
8 + (REnvironment*)globalEnv {
9 static REnvironment *globalEnv = nil;
10 if(nil == R_GlobalEnv) return nil;
11 if(nil == globalEnv) globalEnv = [[self objectWithSEXP:R_GlobalEnv preserve:NO] retain];
16 NSMutableArray *arr = [[NSMutableArray alloc] init];
18 PROTECT(ls = lang2(install("ls"),(SEXP)obj_ptr));
19 SET_TAG(CDR(ls),install("envir"));
22 PROTECT(res = R_tryEval(ls,R_GlobalEnv,&err));
23 if(err == 0 && res != R_NilValue) {
25 for(i=0;i<length(res);i++) [arr addObject:[NSString stringWithUTF8String:CHAR(STRING_ELT(res,i))]];
28 return [arr autorelease];
31 - (RObject*)objectWithName:(NSString*)aName {
32 SEXP var = findVarInFrame3((SEXP)obj_ptr,install([aName UTF8String]),TRUE);
33 return (var == R_UnboundValue) ? nil : [RObject objectWithSEXP:var];
37 - (NSArray*)objectDescriptions {
38 NSMutableArray *arr = [[self names] retain];
40 for(i=0;i<[arr count];i++) {
41 NSString *name = [arr objectAtIndex:i];
42 [arr replaceObjectAtIndex:i withObject:[[self objectWithName:name] describeWithName:name]];
44 return [arr autorelease];
47 - (BOOL)unserializeObject:(NSData*)aData toName:(NSString*)aName overwrite:(BOOL)shouldOverwrite {
50 SEXP sym = install([aName UTF8String]);
51 PROTECT(val = [aData unserialize]);
52 SEXP check = findVarInFrame3((SEXP)obj_ptr,sym,TRUE);
53 if(YES == shouldOverwrite || R_UnboundValue == check) {
54 setVar(sym,val,(SEXP)obj_ptr);
61 - (RServerConnection*)connectToServer:(NSString*)aServer host:(NSString*)aHost {
62 return [RServerConnection serverConnectionToServer:aServer host:aHost environment:self];