[R-SIG-Mac] Call R framework from my own Cocoa app?
Simon Urbanek
simon.urbanek at r-project.org
Tue Jun 28 16:40:49 CEST 2005
Hi Demitri,
On Jun 28, 2005, at 9:29 AM, Demitri Muna wrote:
> Basically, I was optimistically hoping to be able to write a Cocoa
> app that would construct small R scripts, pass them to a black
> box, and get the image back.
you can do that fairly easily just using REngine. I have put together
a tiny sample project, you can download it from
http://research.att.com/~urbanek/REngineTest.dmg
> clean as I'd like!). The biggest problem I've come across is speed. I
> have 10,000 data points I'd like to plot, so in the AppleScript I do
> something like this:
>
> y = c(0, 1, 2, ...., 9999) # not a sequence, but real data
This is no issue if you use REngine, from the above example (silly,
but should illustrate the point):
double a[10] = { 0.5, 1.3, 4.3, 5.3, 2.6, 1.7, 3.5, 3.3,
2.5, 1.6 };
RSEXP *ax = [[RSEXP alloc] initWithDoubleArray:a length:10];
[re assignTo:@"a" value:ax];
This assigns the array directly into R as variable "a", it doesn't
need to be converted to string or anything like that.
RSEXP *r = [re evaluateString:@"as.double(c(mean(a), var
(a)))"];
double *dr = [r doubleArray];
[textView insertText:[NSString stringWithFormat:@"mean=%f,
variance=%f\n", dr[0], dr[1]]];
This will just get and print some results.
REngine supports both - simple evaluation (as illustrated above) and
full REPL. For the latter look and the <REPLHandler> protocol - if
you implementa that, you get the full control over the console and
callbacks.
>> We can always use experienced Cocoa programmers - they are rather
>> rare so far ;).
>>
>
> Ah.. but the opportunities. :) Unfortunately, I don't have a lot of
> time to spend on another project (so many unfinished ones...plus
> grad school...). However, I would be curious to see how difficult
> it would be to make the R framework standalone.
It *is* stand-alone and any application can links against it. You can
use all C-level API that R offers and this is what all R packages do.
Only the Obj-C wrapper (REngine) is currently not part of the
framework for several reasons ranging from Obj-C support in R in
general to the fact that the Obj-C API is far from complete.
> I spent a few minutes trying to add REngine to my app, but the
> dependencies grew pretty quickly. :)
All you need now is R installed as framework in /Library/Frameworks.
The demo app above includes REngine that is independent of the R.app.
Quartz won't work with that, though - you'd have to get the Quartz
classes from R.app (but it's possible).
Cheers,
Simon
More information about the R-SIG-Mac
mailing list