Best Way to Use R in Ruby

Best way to use R in Ruby

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

Can Ruby interface with r?

See RSRuby for accessing R functionality through Ruby.

As for a beginner's tutorial, try looking at "R For Beginners". I found it helpful when I had to learn some basic R for a statistics course.

R Statistical Package Gem For A Rails Application

RSRuby takes the approach of embedding the R interpreter into Ruby as a C extension. It only works on specific versions of ruby, so if you are using JRuby or Rubinius this is not really an option for you. It is definitely the fastest, although some class conversions get a little weird.

RinRuby and Rserve Ruby both use TCP/IP sockets, although Rserve claims to be 5-10 times faster.

I would giving RSRuby a try, and if you encounter problems with your ruby version or such, switching to Rserve. I am not familiar with Gauss.

Heads up- as far as I know, none of these solutions support multi threading, largely because R doesn't play nice with other instances of itself.

Best way to store user settings

I prefer https://github.com/ledermann/rails-settings for storing model specific settings with optional default values.
This stores the settings in a simple has_many association. The data is stored in a Proc.
I assume that a simple join will not have a big impact on performance.

Script a ruby command-line app; best way to do this?

I'd just use a combination of the command line, and convention.

If a filter is specified, use it to filter data

I'm assuming you'd specify a filter on the command line? So you'd invoke the application like this?

ruby dataprocessor.rb custom_filter

If so, you could define an "api" wherein a class name would have to match what was passed in - pretty much exactly how you've described in your example.

To take it one step further though, you could have some logic which looked for the CustomFilter class using ruby's defined?, and if it was not found, go looking for custom_filter.rb (or any suitable variations) and attempt to load that file, then retry.

This gives you great extensibility, as you can write as many filter classes as you like, chuck them in their own .rb files, and put them anywhere that ruby can find them. You won't have to have an pre-defined constants either, the only constraints will be

  1. The class name must match (a variant of) the file name - This is convention in ruby so you're probably already doing it anyway.
  2. it must have some predefined method, such as your do_filter method

Incidentally, this is pretty similar to what rails does for requiring your models, and is why you can just use SomeModel without having to always do require app/models/some_model first :-)`

Best way to store a user token in Ruby script

Compose all approaches. Something like this:

def get_my_token()
if ENV["MY_TOKEN"]
return ENV["MY_TOKEN"]
end

token_path = File.expand_path("~/.my_token") # will be expanded to user's home (Documents or smth) in windows, check it yourself as I don't have running windows around here
if File.exists?(token_path)
return File.read(token_path).strip
end

# resort to asking user for token here
end

ENV should go first - so you'll be able to override your config if needed for some testing purpose. Also note that you can run your script as MY_TOKEN=xxxx ruby my_app.rb as well.

Ruby: How can I dynamically build a hash from user input

h = {a: 1, b: 2, c: 3, d: 4.....z: 26}

user_input = 1

Hash[h.to_a[((user_input - 1) * 5 )..( (user_input * 5) - 1)]]
#=> {:a=>1, :b=>2, :c=>3, :d=>4, :e=>5}

Best way to split an array of strings in ruby?

You need map{...}, not map(...) for correct syntax in Ruby here:

array = ["string_apple", "string_banana", "string_orange"]

# Assign to a different array:
split_array = array.map{ |s| s.split(/_/) }

# Or modify the original array in place:
array.map!{ |s| s.split(/_/) }

# [["string", "apple"], ["string", "banana"], ["string", "orange"]]


Related Topics



Leave a reply



Submit