[R-SIG-Mac] Using Obj-C from R

Simon Urbanek simon.urbanek at math.uni-augsburg.de
Sat Oct 30 17:03:37 CEST 2004


I have put together a small and crude package that allows you to send 
messages to Obj-C objects and classes and create references to objects, 
classes and selectors. For historical reasons it's called Cocoa 
(although Obj-C is strictly speaking not Cocoa) and you can get it from 
my nightly builds page ( http://www.rosuda.org/R/nightly/ ).

It is interesting only for developers as it's only the first step in a 
complete Obj-C/R suite, but since some people seemed to be interested 
in this, I thought I share it here. Some (admittedly stupid) examples:

# Class sc = [NSString class]
 > sc<-.MCall("NSString","class")
 > sc
Obj-C class: NSString

# NSString *s = [NSString stringWithString:@"hello"]
 > s <- .MCall("NSString","stringWithString:","hello")
 > s
hello

# [s stringByAppendingString: @" World!")
 > s2 <- .MCall(s, "stringByAppendingString:", " World!")
 > s2
hello World!

# [s respondsToSelector: @selector(length)]
 > .MCall(s, "respondsToSelector:", .MSelector("length"))
[1] TRUE

(the M is front of the calls was motivated by the .m extension of Obj-C 
code)
and if you use Cocoa inside R.app you can do many stupid things like:

c<-.MCall("RController", "getRController")
.MCall(c, "toggleHistory:", c)

Currently only boolean, id and Class results are supported and only 
strings are automatically converted to NSString as arguments. Main work 
to be done now is the support for scalar types, especially Cocoa-native 
types like NSRange and so on. Anyone interested in this is welcome to 
enhance the code ... Comments are very welcome, too. But again, this is 
just a draft - no documentation, no guarantees ;).

Cheers,
Simon



More information about the R-SIG-Mac mailing list