Module: R

Defined in:
lib/R.rb

Defined Under Namespace

Classes: Receiver, TimeSeries

Constant Summary collapse

CONVERSION_TABLE =
{
  "data.frame" => lambda{ |x| CADataFrame.from_R_data_frame(x) },
  "ts" => lambda{ |x| R::TimeSeries.new(x) },
}
CONVERTER =
lambda{|x| 
  case x
  when RObj
    klass = @r.eval_R("class").call(x)
    if CONVERSION_TABLE.has_key?(klass)
      CONVERSION_TABLE[klass][x]
    else
      case val = x.to_ruby
      when Numeric, String
        val
      when Hash, NilClass
        Receiver.new(klass, x)
      when Array
        val = val.to_ca
        case klass
        when "character", "factor"
          val = val.maskout!(R.NA_character_)
        when "integer"
          val = val.maskout!(R.NA_integer_).int32
        when "numeric"
          val = val.maskout!(R.NA_real_).double
        end
        val
      else
        val
      end
    end
  else 
    x
  end
}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.NA_character_Object (readonly)

Returns the value of attribute NA_character_.



177
178
179
# File 'lib/R.rb', line 177

def NA_character_
  @NA_character_
end

.NA_integer_Object (readonly)

Returns the value of attribute NA_integer_.



177
178
179
# File 'lib/R.rb', line 177

def NA_integer_
  @NA_integer_
end

.NA_real_Object (readonly)

Returns the value of attribute NA_real_.



177
178
179
# File 'lib/R.rb', line 177

def NA_real_
  @NA_real_
end

Class Method Details

.__converter__(arg) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/R.rb', line 202

def self.__converter__ (arg)
  case arg
  when Symbol
    return arg.to_s
  when CArray
    return __converter__(arg.as_r)
  when CADataFrame
    return arg.as_r
  when Array
    return arg.map{|v| __converter__(v) }
  when Hash
    new_hash = {}
    arg.each do |k,v|
      new_hash[k] = __converter__(v)
    end
    return new_hash
  else
    return arg
  end
end

.call(expr, hash = {}) ⇒ Object



191
192
193
194
195
196
197
198
199
200
# File 'lib/R.rb', line 191

def self.call (expr, hash = {})
  names = ["DU33Y"]
  args  = [0]
  hash.each do |name, value|
    names.push(name.to_s)
    args.push(__converter__(value))
  end
  expr = "function (#{names.join(",")}) {" + expr + "}"
  return @r.eval_R(expr).call(*args)
end

.exec(expr, hash = {}) ⇒ Object



184
185
186
187
188
189
# File 'lib/R.rb', line 184

def self.exec (expr, hash = {})
  hash.each do |name, value|
    @r.assign(name.to_s, __converter__(value))
  end
  return @r.eval_R(expr)
end

.instanceObject



180
181
182
# File 'lib/R.rb', line 180

def self.instance
  return @r
end

.method_missing(sym, *args) ⇒ Object



223
224
225
226
227
228
229
230
231
# File 'lib/R.rb', line 223

def self.method_missing (sym, *args)
  if args.empty? and sym.to_s[-1] == "!"
    return @r.send(sym.to_s[0..-2].intern).call()
  elsif args.size == 1 and sym.to_s[-1] == "="
    return @r.assign(sym.to_s[0..-2], __converter__(args[0]))
  else
    return @r.send(sym, *args.map{|v| __converter__(v)})
  end
end

.runObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/R.rb', line 153

def self.run
  if @r
    return nil
  end
  ENV["LANG"]   = "en_US.UTF-8"
  ENV["LC_ALL"] = "en_US.UTF-8"
  @r = RSRuby.instance
  RSRuby.set_default_mode(RSRuby::PROC_CONVERSION)
  @r.class_table['data.frame'] = lambda{|x| ERObj.new(x) }
  @r.class_table['matrix'] = lambda{|x| ERObj.new(x) }
  @r.proc_table[lambda{|x| true }] = CONVERTER
  @NA_integer_   = R %{ NA_integer_ }
  @NA_real_      = R %{ NA_real_ }
  @NA_character_ = R %{ NA_character_ }
  ObjectSpace.define_finalizer(self, proc{ @r.shutdown })
  return nil
end

.stopObject



171
172
173
174
# File 'lib/R.rb', line 171

def self.stop
  @r.shutdown
  @r = nil
end