Class: R::List

Inherits:
Object show all
Includes:
Enumerable, IndexedObject
Defined in:
lib/R_interface/rlist.rb

Overview



Instance Attribute Summary

Attributes inherited from Object

#r_interop, #statement

Instance Method Summary collapse

Methods included from IndexedObject

#[], #[]=, #size

Methods inherited from Object

#==, #_, #attr=, build, #comment, #comment=, #dim, #dim=, #dimnames, #dimnames=, #eql, #initialize, #method_missing, #names, #names=, #pp, #pretty_print, #rclass, #rclass=, #row__names, #row__names=, #setR, #setR_name, #to_s, #tsp, #tsp=

Constructor Details

This class inherits a constructor from R::Object

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class R::Object

Instance Method Details

#+(other_object) ⇒ Object





38
39
40
# File 'lib/R_interface/rlist.rb', line 38

def +(other_object)
  R::Support.exec_function_name("`+`", @r_interop, other_object.r_interop)
end

#>>(index) ⇒ Object


When indexing with ‘[’ or ‘[[’ an R object is returned. Sometimes we need to have access to an umboxed Ruby element, for instance, in an numeric array, we might want to receive the actual number that can be used in a Ruby method. In this case, we use the ‘<<’ operator.


Returns:

  • the Ruby element at the given index in the vector

Raises:

  • (IndexError)


58
59
60
61
62
63
64
65
# File 'lib/R_interface/rlist.rb', line 58

def >>(index)
  raise IndexError.new("index #{index} out of list bounds: 0...#{index - 1}") if
    (index > (length - 1) >> 0)
  raise ArgumentError.new("Indexed element is not a vector") if
    !self[[index + 1]].is_a? R::Vector
  return nil if (self[[index + 1]].is__null >> 0)
  self[[index + 1]] >> 0
end

#eachObject


Each cannot return a Enumerator because R is single threaded. When this restriction is removed, make each return self.to_enum




72
73
74
75
76
77
78
79
80
# File 'lib/R_interface/rlist.rb', line 72

def each

  # length is a R::Vector, in order to extract its size as a Ruby number we need to
  # use the >> operator
  (1..length >> 0).each do |i|
    yield self[[i]]
  end
  
end

#each_with_indexObject


Need to override each_with_index, as R indexing starts at 1




86
87
88
89
90
91
92
# File 'lib/R_interface/rlist.rb', line 86

def each_with_index
  
  (1..length >> 0).each do |i|
    yield self[[i]], i
  end
  
end

#method_missing_assign(elmt_name, arg) ⇒ Object





46
47
48
# File 'lib/R_interface/rlist.rb', line 46

def method_missing_assign(elmt_name, arg)
  setR_name("`[[<-`", elmt_name, arg)
end