[graalvm-users] Tightly coupling Ruby and R

Chris Seaton chris.seaton at oracle.com
Mon Oct 1 08:11:03 PDT 2018


Thanks for the summary of your progress, Rodrigo.

Are you planning a blog post or something about this in the future? Let us know if you need any more support from us.

Chris

> On 28 Sep 2018, at 16:00, Rodrigo Botafogo <rodrigo.a.botafogo at gmail.com> wrote:
> 
> Hello...
> 
> I've being working on coupling Ruby and R for some time now (since rc1) and I think a have managed to advance quite a bit, thanks in great part to the help from people here, specially Chris and Stepan: thanks!!  So, now, instead of posting a question, this post is tho show where I'm at and see if you have any suggestions, opinions, feedback, etc.  
> 
> Bellow two examples of how the coupling works.  The first is a scatter plot extracted from http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html and the second a 5th degree polynomial regression. The idea of the coupling is to use Ruby syntax, but emulate R as closely as possible in order to make it trivial to use for an R developer.  
> 
> ====================================================================
> require 'cantata'
> require 'ggplot'
> 
> # set options
> R.options(scipen: 999)  # turn-off scientific notation like 1e+48
> R.theme_set(R.theme_bw)  # pre-set the bw theme.
> 
> # read the R 'midwest' dataset onto the 'midwest' variable 
> midwest = ~:midwest
> 
> R.awt
> 
> # Scatterplot
> gg = midwest.ggplot(E.aes(x: :area, y: :poptotal)) + 
>        R.geom_point(E.aes(col: :state, size: :popdensity)) + 
>        R.geom_smooth(method: "loess", se: false) + 
>        R.xlim(R.c(0, 0.1)) + 
>        R.ylim(R.c(0, 500000)) + 
>        R.labs(subtitle: "Area Vs Population", 
>                    y: "Population", 
>                    x: "Area", 
>                    title: "Scatterplot", 
>                    caption: "Source: midwest")
> 
> puts gg
> 
> <image.png>
> 
> 
> ===================================================================
> 
> As another example, here is how to get a polynomial regression using the Boston data from R package ISLR. 
> 
> require 'cantata'
> R.require 'MASS'
> R.require 'ISLR'
> 
> lm_fit5 = R.lm(R.formula("medv ~ poly(lstat, 5)"), data: :Boston)
> puts lm_fit5.summary
> 
> [This output was cut from the total print...]
> 
> Residuals:
>      Min       1Q   Median       3Q      Max 
> -13.5433  -3.1039  -0.7052   2.0844  27.1153 
> 
> Coefficients:
>                  Estimate Std. Error t value Pr(>|t|)    
> (Intercept)       22.5328     0.2318  97.197  < 2e-16 ***
> poly(lstat, 5)1 -152.4595     5.2148 -29.236  < 2e-16 ***
> poly(lstat, 5)2   64.2272     5.2148  12.316  < 2e-16 ***
> poly(lstat, 5)3  -27.0511     5.2148  -5.187 3.10e-07 ***
> poly(lstat, 5)4   25.4517     5.2148   4.881 1.42e-06 ***
> poly(lstat, 5)5  -19.2524     5.2148  -3.692 0.000247 ***
> ---
> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> 
> Residual standard error: 5.215 on 500 degrees of freedom
> Multiple R-squared:  0,6817,    Adjusted R-squared:  0,6785 
> F-statistic: 214,2 on 5 and 500 DF,  p-value: < 2.2e-16
> 
> ===================================================================
> 
> In order to test this:
> 
> * Install GraalVM (rc6)
> * Install TruffleRuby (follow the simple instructions)
> * Install FastR
> * Clone the git repo: https://github.com/rbotafogo/cantata.git
> * gem install rspec
> * Use rake to run the tests ('rake -T' shows all available tasks)
> 
> Doing:
> 
> * rake specs:all -- Runs all the specs.  Reading the specs shows much of how to use the language
> * rake sthda:all -- Runs a slideshow with over 80 plots
> * rake islr:all -- Runs some 'labs' from the Introduction to Statistical Learning book
> 
>  
> 
> -- 
> Rodrigo Botafogo
> 
> _______________________________________________
> GraalVM-Users mailing list
> GraalVM-Users at oss.oracle.com
> https://oss.oracle.com/mailman/listinfo/graalvm-users




More information about the GraalVM-Users mailing list