[R-SIG-Mac] Interacting with R GUI app from another Cocoa app

Simon Urbanek simon.urbanek at r-project.org
Mon Nov 30 15:37:18 CET 2009


Guillaume,

On Nov 29, 2009, at 10:29 , Guillaume Chapron wrote:

> Hello,
>
> I have a standalone simulation model written in Objective C/Cocoa.  
> Once the simulations are done, I would like to send the output (a  
> large C array) to R.app for further analysis and plotting. I'm able  
> to launch R.app from a Cocoa app by this:
>
> 	[[NSWorkspace sharedWorkspace] launchApplication:@"R.app"];
>
> But I'm not able to send the C array or in fact any kind of  
> instructions. I have tried a modified code:
>
> 	NSBundle *bundle = [NSBundle bundleWithPath:@"/Applications/R.app"];
> 	NSString *path = [bundle executablePath];
> 	NSTask *task = [[NSTask alloc] init];
> 	
> 	[task setLaunchPath:path];
> 	[task setArguments:[NSArray arrayWithObject:@"2+3"]];
> 	[task launch];
>
> But it seems that R.app does not understand the arguments of the  
> NSTask.

R.app is a a LS app so it doesn't process command line arguments. You  
can talk to R.app using AppleScript (see the FAQ).


> How from my Cocoa app, can I tell R.app to read a pointer to a C  
> array and convert it to a R object, and how can I send R instructions?
>

I think you're approaching this form the wrong end. You can load  
arbitrary C/Obj-C code into R (see R-ext) and that is how you can  
create R objects from C arrays (or rather you allocate the R object  
instead of using malloc). You can use the RController object to talk  
to the R.app GUI if you wish - e.g. you may be able to use the Cocoa  
package
http://rforge.net/Cocoa
to call Obj-C code such as issuing Obj-C messages from R.


> Before I wrote the Objective C model as a dyn library and I could  
> call it from R and run the simulations, but because simulations last  
> hours, I cannot use R.app during that time, so I'm trying to do the  
> reverse, interact with R from a Cocoa app once the simulations are  
> done. Has anyone done something like that?
>

There are two issues depending how you define "cannot use R.app".  
First, you should be calling R_CheckUserInterrupt() periodically to  
make sure your simulation doesn't block R and the GUI.

Second, if you really want to simulation to run entirely in parallel,  
you can simply create a separate thread in your simulation code such  
that your initialization function returns back to R as soon as all  
objects are preserved or copied so R can continue to work while your  
simulation is running on a separate thread. Then you can always check  
the objects from the R code to see if it has finished and yet you can  
safely work in parallel. The only restriction is that your threaded  
code may not call any R API functions as soon as it dispatches the new  
thread, but that's usually ok if you allocate the R objects before that.

Cheers,
Simon



More information about the R-SIG-Mac mailing list