[graalvm-users] Integration Ruby x R

Chris Seaton chris.seaton at oracle.com
Fri May 11 13:36:00 PDT 2018


Here’s a more fun way to use typeof though:

  module R
    def self.eval(string)
      Polyglot.eval("R", string)
    end
    
    def self.method_missing(name, *args)
      eval(name.to_s).call(*args)
    end
  end

var = R.eval("4")
val = R.c(1, 2, 3, 4)
p R.typeof(val)
p val
p val[1]
val = R.list(1, 2, R.c('a', 'b', 'c'))
p R.typeof(val)
p val
p val[2][1]


> On 11 May 2018, at 21:26, Rodrigo Botafogo <rodrigo.a.botafogo at gmail.com> wrote:
> 
> Hi Chris,
> 
> Thanks:
> 
> 1) You're right
> 2) Works perfectly
> 3) Will check with the FastR team
> 
> 
> 
> On Fri, May 11, 2018 at 5:17 PM, Chris Seaton <chris.seaton at oracle.com <mailto:chris.seaton at oracle.com>> wrote:
> I guess your R.eval definition looks something like this?
> 
>   module R
>     def self.eval(string)
>       Polyglot.eval("R", string)
>     end
>   end
> 
> 1) I don’t know anything about R personally, but running the standard version of R and experimenting, it looks like it does return the value.
> 
>   > x = print(4)
>   [1] 4
>   > x
>   [1] 4
> 
> 2) Does R have a way to ask values what type they are? If so you can call that from interop.
> 
>   module 
>     def self.typeof(object)
>       eval("typeof").call(object)
>     end
>   end
> 
>   ...
> 
>   p R.typeof(val)
> 
> 3) That’s something you could debate, but it looks like this is a specific design decision on the part of FastR, so it isn’t a mistake but you could open an issue with them if you think it’s wrong.
> 
> https://github.com/oracle/fastr/blob/acc680f0d1168aa42d19174130f67c40ccc811b6/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/interop/R2Foreign.java#L70-L73 <https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_oracle_fastr_blob_acc680f0d1168aa42d19174130f67c40ccc811b6_com.oracle.truffle.r.runtime_src_com_oracle_truffle_r_runtime_interop_R2Foreign.java-23L70-2DL73&d=DwMFaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=GeKqXC_6SKmJdrXPk9-xeQQoxGJV5kRmq9tMEP11foo&m=uTNrIujt6kNfAXmIXeWj0kHDX14Wnh7esjI_DxcCAvA&s=Y3R_JsdqrFOQBLD1zlOzeAtOX8qLsDVHAAY-V5CYliA&e=>
> 
>> On 11 May 2018, at 21:05, Oleg Šelajev <oleg.selajev at oracle.com <mailto:oleg.selajev at oracle.com>> wrote:
>> 
>> ---------- Forwarded message ----------
>> From: Rodrigo Botafogo <rodrigo.a.botafogo at gmail.com <mailto:rodrigo.a.botafogo at gmail.com>>
>> Date: 11 May 2018 22:11
>> Subject: [graalvm-users] Integration Ruby x R
>> To: graalvm-users at oss.oracle.com <mailto:graalvm-users at oss.oracle.com>
>> Cc: 
>> 
>> Hello Graalers...
>> 
>> I'm trying to test and implement an integration between Ruby and R.  For that I have implemented this simple code where R.eval is a call to the R eval function.
>> 
>>     p R.eval("var = 4")
>>     p R.eval("print(var)")
>>     val = R.eval("var = 4")
>>     p val
>>     val = R.eval("var = c(1, 2, 3, 4)")
>>     p val
>>     p val[1]
>>     val = R.eval("list(1, 2, c('a', 'b', 'c'))")
>>     p val
>>     p val[2][1]
>> 
>> I get the following results for this code:
>> 
>> 4.0           # result of R.eval("var = 4")
>> [1] 4         # the print(var) in R
>> 4.0           # result of R.eval("print(var)")
>> 4.0           # p val
>> #<Truffle::Interop::Foreign at 46994f26>  # pointer to the c(1, 2, 3, 4) op
>> 2.0           # p val[1]
>> #<Truffle::Interop::Foreign at 2b8cf049>  # pointer to the list
>> "b"           # p val[2][1]
>> 
>> My questions:
>> 
>> 1) In R, if I'm not mistaken print(var) has no return value.  Should we get 4.0 as return in this case?
>> 2) The first pointer a foreign object points to a vector while the second pointer points to a list.  In this simple case, we know beforehand the type of the object, but in general this might not be true.  Is there any way to know the type of the object and to what messages it responds?  This seems critical to me to be able to really integrate both languages;
>> 3) The result of R.eval("var = 4") in R is a vector, but we get back a float in Ruby.  For consistency, shouldn't this be a Ruby vector with 1 element? 
>> 
>> Thanks
>> 
>> 
>> 
>> -- 
>> Rodrigo Botafogo
>> 
>> 
> 
> 
> 
> 
> -- 
> Rodrigo Botafogo
> Integrando TI ao seu negócio
> 21-3010-4802/11-3010-1802

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://oss.oracle.com/pipermail/graalvm-users/attachments/20180511/c8083af0/attachment.html 


More information about the GraalVM-Users mailing list