class RType::Matrix

Public Class Methods

match?(robj, type) click to toggle source
# File lib/r_type/type/matrix.rb, line 10
def self.match? robj, type
  type == 'matrix'
end
new(obj, *args) click to toggle source
# File lib/r_type/type/matrix.rb, line 14
def initialize obj, *args
  case obj
  when ::RObj
    @robj = obj
  when ::Matrix
    @ruby_obj = obj
  else
    raise "Not supported: #{obj} in RType::Matrix"
  end
end

Public Instance Methods

[](*args) click to toggle source
# File lib/r_type/type/matrix.rb, line 25
def [] *args
  args = convert_nil_args_to_vector(args)
  R['['].call self, *args
end
[]=(*args) click to toggle source
# File lib/r_type/type/matrix.rb, line 30
def []= *args
  if args[0].is_a?(RType::Matrix)
    self.robj = R['[<-'].call(self, args[0], args[1]).robj
  else
    args = convert_nil_args_to_vector(args)
    self.robj = R['[<-'].call(self, *args).robj
  end
end
column_size() click to toggle source
# File lib/r_type/type/matrix.rb, line 43
def column_size
  R.ncol(self)
end
row_size() click to toggle source
# File lib/r_type/type/matrix.rb, line 39
def row_size
  R.nrow(self)
end

Private Instance Methods

convert_nil_args_to_vector(args) click to toggle source
# File lib/r_type/type/matrix.rb, line 59
def convert_nil_args_to_vector args
  args[0] = (1..row_size).to_a    if args[0].nil? && args[1].is_a?(::Numeric)
  args[1] = (1..column_size).to_a if args[1].nil? && args[0].is_a?(::Numeric)
  args
end
convert_robj_to_ruby() click to toggle source
# File lib/r_type/type/matrix.rb, line 48
def convert_robj_to_ruby
  if R.mode(self) == 'logical'
    # We can't parse this R-matrix. So, we return robj instead of ruby_obj.
    # TODO: Parse R-matrix that have logical values
    nil
  else
    rows = super
    ::Matrix.rows(rows.first.is_a?(::Numeric) ? [rows] : rows)
  end
end