[R-SIG-Mac] (1) Just ignore warning messages? and (2) Using graphics output from R in a Cocoa program

Simon Urbanek simon.urbanek at r-project.org
Mon Oct 15 03:59:48 CEST 2012


Paul,

On Oct 14, 2012, at 2:04 PM, Paul Johnson wrote:

> Simon, I wonder if you can clarify/expand on the following statement you
> made: "Probably the easiest way would be to simply let Cocoa Quartz create
> the view and grab it for yourself."
> 

What I meant is that if your app is embedding, then you could simply grab the QuartzCocoaView that R creates (it is the content view of the window that R creates over which your app has control) and plug it wherever you want it. That would be a bit of a hack, since you let R do the dirty work and grab the result, but it's the easiest to code. The only drawback is that you cannot subclass QuartzCocoaView in that case (because it is already created at that point). For a more clean solution (that allows arbitrary subclassing) see below.


> I have a simple test program that uses REngine and that calls R with a
> sequence of statements like
> s1 = [re executeString:@"setwd(\"~/Desktop/Test/\")"];
> 
> I'm having a problem with a 'plot' command:
> 
> s1 = [re executeString:@"plot(diff(log(ABCD)))"];
> 
> 
> The plot output appears in a new window, and I want it to appear in a view
> within the main program window.
> 
> 
> So, I need to tell R where I want the output to go.
> 
> 
> Can I establish in Interface Builder a connection to my NSView to
> accomplish this?
> 
> 
> Or perhaps I need to make some sort of call to R after activating it in
> the awakeFromNib method (to establish the output 're-routing')?
> 

The initiative comes from R because that is how the devices are created. 
You can use the Quartz API to create your own back-end, just like qdCocoa does - see
https://svn.r-project.org/R/trunk/src/library/grDevices/src/qdCocoa.m
You can modify quartzWindowWithRect: to use your window (the above code just creates a new NSWindow and a new QuartzCocoaView) or do whatever you want to - e.g, fetch an existing view or so. Note that the device creation is initiated from the R side, so if you use XIB to create a view, it won't become a device until R calls the a Quartz constructor (if you are embedding, you can use ptr_QuartzBackend to re-define the back-end to point to your code). 

I have put a small demo in 
http://svn.rforge.net/osx/trunk/RDemo/
which illustrates how to write your own Quartz backend. It's ugly and very bare-bones since I just hacked it quickly together from current R sources (don't even try to use multiple devices for example), but it should give you an idea.

Cheers,
Simon



More information about the R-SIG-Mac mailing list