19

I want to visualize some of my statistical caluclations in Ruby.

My problem is, that I can't find the right gem for that.

rsruby doesn't seem to be up-to-date and I can't install it in Ruby 1.9.2.

Do you know a way how to run the R commands in Ruby?

jeffrey
  • 2,026
  • 5
  • 28
  • 42

2 Answers2

40

I just saw this post and thought I should comment since I use R pretty extensively. If you are coming from an R background the best gem I have found is Rinruby. The reason it is fantastic is because you don't interpret the commands in ruby, you use actual R code. For example:

require "rinruby"      
#Set all your variables in Ruby
n = 10
beta_0 = 1
beta_1 = 0.25
alpha = 0.05
seed = 23423
R.x = (1..n).entries
#Use actual R code to perform the analysis
R.eval <<EOF
  set.seed(#{seed})
  y <- #{beta_0} + #{beta_1}*x + rnorm(#{n})
  fit <- lm( y ~ x )
  est <- round(coef(fit),3)
  pvalue <- summary(fit)$coefficients[2,4]
EOF

On the Rinruby website I listed above there are some fantastic examples to get you started. Hope this helped.

-Sean

Community
  • 1
  • 1
Sean
  • 2,891
  • 3
  • 29
  • 39
  • 6
    Sean, don't mince your words. Do you think it RinRuby is fantastic or not? – Cary Swoveland Oct 05 '14 at 18:08
  • @sean, is there a way to generate the graphs through the webpage instead of having them display in a R window? I'm using the Gettysburg example from the library's website for testing this currently... – daveomcd Jun 13 '15 at 16:27
  • @daveomcd It's been awhile..., but can't you just just render to PNG as your graphic device instead of an R window and then display the PNG? – bigtunacan Jul 12 '15 at 03:48
  • @bigtunacan yes I ended up saving it as a temporary png and the embedding in the page thanks! – daveomcd Jul 12 '15 at 03:49
  • What is the benefit of using R in ruby? Is it faster to execute this code using Rinruby versus just running it in R? – lrthistlethwaite Apr 25 '16 at 21:55
  • Definitely faster to just use R if you are doing stats stuff. You use r in ruby when you need to combine the two. My use case is that I execute the R code from inside ruby a ruby program to generate some visualizations that I then use/display elsewhere. – Sean Apr 26 '16 at 18:09
3

As @Hansi mentioned, RServe is the best way I've found to run R remotely. If you're using Ruby from a web-context especially, RServe can offer some nice benefits.

Best of all (in my mind), you don't get locked into any one programming framework, as there are RServe clients for a variety of languages including Java and C++. When using web-accessible platforms, you can even keep Rserve running on a separate host and route traffic over TCP/IP for added security.

Jeff Allen
  • 17,277
  • 8
  • 49
  • 70