Parent

Rserve::REXP

Basic class representing an object of any type in R. Each type in R in represented by a specific subclass. This class defines basic accessor methods (as_xxx), type check methods (XXX?), gives access to attributes (REXP.get_attribute, REXP.has_attribute?) as well as several convenience methods. If a given method is not applicable to a particular type, it will throw the MismatchException exception.

This root class will throw on any accessor call and returns false for all type methods. This allows subclasses to override accessor and type methods selectively.

Constants

MismatchException
MaxDebugItems

specifies how many items of a vector or list will be displayed in {@link #toDebugString}

Attributes

attr[R]

Public Class Methods

create_data_frame(l) click to toggle source

creates a data frame object from a list object using integer row names.

  • @param [Rlist] a (named) list of vectors (REXP::Vector subclasses), each element corresponds to a column and all elements must have the same length.

  • @return [GenericVector] a data frame object representation.

# File lib/rserve/rexp.rb, line 368
def self.create_data_frame(l)
  raise(MismatchException, "data frame (must have dim>0)") if l.nil? or l.size<1
  raise MismatchException, "data frame (contents must be vectors)" if (!(l[0].is_a? REXP::Vector))
  fe = l[0]
  return REXP::GenericVector.new(l,
  REXP::List.new(
  Rlist.new(
  [
  REXP::String.new("data.frame"),
  REXP::String.new(l.keys()),
  REXP::Integer.new([REXP::Integer::NA, -fe.length()])
  ],
  ["class", "names", "row.names" ])
  )
  )
end
new(attr=nil) click to toggle source
# File lib/rserve/rexp.rb, line 10
def initialize(attr=nil)
  # Sorry for this, but I think is necessary to maintain sanity of attributes
  raise ArgumentError, "Attribute should be a REXP::List, #{attr.class} provided" unless attr.nil? or attr.is_a? REXP::List
  @attr=attr
end

Public Instance Methods

as_bytes() click to toggle source

returns the contents as an array of bytes (if supported by the represented object).

@return [Array]

# File lib/rserve/rexp.rb, line 157
def as_bytes
  raise MismatchException , "byte"
end
as_double() click to toggle source

convenience method corresponding to as_floats[0].

@return [Float] first entry returned by as_doubles()

# File lib/rserve/rexp.rb, line 207
def as_double
  as_doubles[0]
end
as_double_matrix() click to toggle source

returns the content of the REXP as a ruby matrix of doubles (2D-array: m[rows][cols]). You could use Matrix.rows(result) to create a ruby matrix. Matrix(c.eval(“matrix(c(1,2,3,4,5,6),2,3)”).as_double_matrix());</code>

@return [Array] 2D array of doubles in the form double[rows][cols] or nil if the contents is no 2-dimensional matrix of doubles

# File lib/rserve/rexp.rb, line 297
def as_double_matrix
  ct = as_doubles()
  dim = get_attribute "dim"
  raise MismatchException, "matrix (dim attribute missing)" if dim.nil?
  ds = dim.as_integers
  raise MismatchException, "matrix (wrong dimensionality)"     if (ds.length!=2)
  as_nested_array
  
  #m,n = ds[0], ds[1]
  # R stores matrices as matrix(c(1,2,3,4),2,2) = col1:(1,2), col2:(3,4)
  # we need to copy everything, since we create 2d array from 1d array
  #r=m.times.map {|i| n.times.map {|j| ct[j*n+i]}}
end
as_doubles() click to toggle source

returns the contents as an array of floats (C double precision) (if supported by the represented object).

@return [Array]

# File lib/rserve/rexp.rb, line 142
def as_doubles
   raise MismatchException,"double"
end
as_factor() click to toggle source

returns the contents as a factor (if supported by the represented object).

@return [RFactor]

# File lib/rserve/rexp.rb, line 169
def as_factor
  raise MismatchException,"factor"
end
as_float() click to toggle source

Alias for as_double()

@return [Float]

# File lib/rserve/rexp.rb, line 213
def as_float
  as_double
end
as_floats() click to toggle source

On Ruby, Float are stored in double precision.

@return [Array]

# File lib/rserve/rexp.rb, line 149
def as_floats
  as_doubles
end
as_integer() click to toggle source

convenience method corresponding to as_integer()[0]

@return [Integer] first entry returned by as_integers()

# File lib/rserve/rexp.rb, line 194
def as_integer
  as_integers[0]
end
as_integers() click to toggle source

returns the contents as an array of integers (if supported by the represented object)

@return [Array]

# File lib/rserve/rexp.rb, line 134
def as_integers
  raise MismatchException, "int"
end
as_list() click to toggle source

returns the contents as a (named) list (if supported by the represented object)

@return [Array]

# File lib/rserve/rexp.rb, line 163
def as_list
  raise MismatchException,"list"
end
as_matrix() click to toggle source

Returns a standard library’s matrix.

@return [Matrix]

# File lib/rserve/rexp.rb, line 313
def as_matrix
  require 'matrix'
  Matrix.rows(as_double_matrix)
end
as_nested_array() click to toggle source

Returns the content of the REXP as a serie of nested arrays of X dimensions

@return [Array]

# File lib/rserve/rexp.rb, line 320
def as_nested_array
  ct=as_doubles
  dim = get_attribute "dim"
  raise MismatchException, "array (dim attribute missing" if dim.nil?
  ds = dim.as_integers.reverse
  split_array(ct,ds)
end
as_string() click to toggle source

convenience method corresponding to as_strings[0].

@return [String] first entry returned by REXP.as_strings

# File lib/rserve/rexp.rb, line 225
def as_string
  as_strings[0]
end
as_strings() click to toggle source

returns the contents as an array of Strings (if supported by the represented object).

@return [Array]

# File lib/rserve/rexp.rb, line 127
def as_strings
  raise MismatchException, "String"
end
complex?() click to toggle source

check whether the REXP object is a complex vector @return [boolean] true if the receiver is a complex vector, false otherwise

# File lib/rserve/rexp.rb, line 108
def complex?
  false
end
dim() click to toggle source

Returns dimensions of the object (as determined by the REXP::dim() attribute).

@return [Array] an array of integers with corresponding dimensions or <code>nil</code> if the object has no dimension attribute
# File lib/rserve/rexp.rb, line 254
def dim
  begin
    return has_attribute?("dim") ? @attr.as_list['dim'].as_integers :  nil;
    rescue MismatchException
  # nothing to do
  end
  nil
end
environment?() click to toggle source

check whether the REXP object is an environment.

@return [boolean] true if the receiver is an environment, false otherwise

# File lib/rserve/rexp.rb, line 74
def environment?
  false
end
expression?() click to toggle source

check whether the REXP object is an expression vector.

@return [boolean] true if the receiver is an expression vector, false otherwise

# File lib/rserve/rexp.rb, line 86
def expression?
  false
end
factor?() click to toggle source

check whether the REXP object is a factor.

@return [boolean] true if the receiver is a factor, false otherwise

# File lib/rserve/rexp.rb, line 49
def factor?
  false
end
get_attribute(name) click to toggle source

Retrieve an attribute of the given name from this object.

@param [String] attribute name. @return [Rlist, nil] attribute value or nil if the attribute does not exist

# File lib/rserve/rexp.rb, line 236
def get_attribute(name)
  has_attribute?(name)  ? @attr.as_list[name] : nil
end
has_attribute?(name) click to toggle source

checks whether this object has a given attribute.

@param [String] attribute name. @return [boolean] true if the attribute exists, false otherwise

# File lib/rserve/rexp.rb, line 244
def has_attribute? (name)
  !@attr.nil? and @attr.list? and !@attr.as_list[name].nil?
end
inherits?(klass) click to toggle source

determines whether this object inherits from a given class in the same fashion as the inherits() function in R does (i.e. ignoring S4 inheritance).

@param [String] klass class name. @return [boolean] true if this object is of the class klass, false otherwise.

# File lib/rserve/rexp.rb, line 267
def inherits?(klass)
  return false if (!has_attribute? "class")
  begin
    c = get_attribute("class").as_strings;
    if (!c.nil?)
      return c.any? {|v| v.equals klass}
    end
  rescue MismatchException
  end
  false
end
integer?() click to toggle source

check whether the REXP object is an integer vector.

@return [boolean] true if the receiver is an integer vector, false otherwise

# File lib/rserve/rexp.rb, line 36
def integer?
  false
end
language?() click to toggle source

check whether the REXP object is a language object.

@return [boolean] true if the receiver is a language object, false otherwise

# File lib/rserve/rexp.rb, line 80
def language?
  false
end
length() click to toggle source

returns the length of a vector object. Note that we use R semantics here, i.e. a matrix will have a length of m * n since it is represented by a single vector (see REXP.dim) for retrieving matrix and multidimentional-array dimensions).

@return [Integer] length (number of elements) in a vector object.

# File lib/rserve/rexp.rb, line 176
def length
  raise MismatchException, "vector"
end
list?() click to toggle source

check whether the REXP object is a list (either generic vector or a pairlist - i.e. REXP.asList() will succeed).

@return [boolean] true if the receiver is a generic vector or a pair-list, false otherwise

# File lib/rserve/rexp.rb, line 56
def list?
  false
end
logical?() click to toggle source

check whether the REXP object is a logical vector.

@return [boolean] true if the receiver is a logical vector, false otherwise */

# File lib/rserve/rexp.rb, line 68
def logical?
  false
end
na?() click to toggle source

returns a boolean vector of the same length as this vector with true for NA values and false for any other values.

@return [boolean] a boolean vector of the same length as this vector with true for NA values and false for any other values.

# File lib/rserve/rexp.rb, line 184
def na?
  raise MismatchException, "vector"
end
null?() click to toggle source

check whether the REXP object is NULL.

@return [boolean] true if the receiver is NULL, false otherwise

# File lib/rserve/rexp.rb, line 42
def null?
  false
end
numeric?() click to toggle source

check whether the REXP object is a numeric vector.

@return [boolean] true if the receiver is a numeric vector, false otherwise

# File lib/rserve/rexp.rb, line 30
def numeric?
  false
end
pair_list?() click to toggle source

check whether the REXP object is a pair-list.

@return [boolean] true if the receiver is a pair-list, false otherwise

# File lib/rserve/rexp.rb, line 62
def pair_list?
  false
end
raw?() click to toggle source

check whether the REXP object is a raw vector @return [boolean] true if the receiver is a raw vector, false otherwise

# File lib/rserve/rexp.rb, line 103
def raw?
  false
end
recursive?() click to toggle source

check whether the REXP object is a recursive obejct @return [boolean] true if the receiver is a recursive object, false otherwise

# File lib/rserve/rexp.rb, line 113
def recursive?
  false
end
reference?() click to toggle source

check whether the REXP object is a reference to an R object @return [boolean] true if the receiver is a reference, false otherwise

# File lib/rserve/rexp.rb, line 118
def reference?
  false
end
string?() click to toggle source

check whether the REXP object is a character vector (string).

@return [boolean] true if the receiver is a character vector, false otherwise

# File lib/rserve/rexp.rb, line 22
def string?
  false
end
symbol?() click to toggle source

check whether the REXP object is a symbol.

@return [boolean] true if the receiver is a symbol, false otherwise

# File lib/rserve/rexp.rb, line 92
def symbol?
  false
end
to_debug_string() click to toggle source

returns representation that it useful for debugging (e.g. it includes attributes and may include vector values)

@return [String] extended description of the obejct – it may include vector values

# File lib/rserve/rexp.rb, line 284
def to_debug_string
  (!@attr.nil?) ? (("<"+@attr.to_debug_string()+">")+to_s()) : to_s
end
to_f() click to toggle source

Alias for as_float()

@return [Float]

# File lib/rserve/rexp.rb, line 219
def to_f
  as_double
end
to_i() click to toggle source

Alias for as_integer().

@return [Integer]

# File lib/rserve/rexp.rb, line 201
def to_i
  as_integers[0]
end
to_ruby() click to toggle source

Retrieves the best Ruby representation of data

If R object has attributes, the Ruby object is extended with Rserve::WithAttributes. If R object have names, the Ruby object is extended with Rserve::WithNames and their elements can be accessed with [] using numbers and literals.

@return [Object] Ruby object.

# File lib/rserve/rexp.rb, line 390
def to_ruby
  #pp self
  v=to_ruby_internal
  #p v
  if !v.nil? and !v.is_a? Fixnum and !v.is_a? TrueClass and !v.is_a? FalseClass
    v.extend Rserve::WithAttributes
    v.attributes=attr.to_ruby unless attr.nil?
    if !v.attributes.nil? and v.attributes.has_name? 'names'
      v.attributes['names']=[v.attributes['names']] unless v.attributes['names'].is_a? Array or v.attributes['names'].nil?
      
      v.extend Rserve::WithNames
      
      v.names=v.attributes['names']
    end
  end
  
  # Hack: change attribute row.names according to spec 
  if !attr.nil? and attr.as_list.has_name? 'class' and attr.as_list['class'].as_string=='data.frame' and (attr.as_list['row.names'].is_a?(REXP::Integer)) and attr.as_list['row.names'].as_integers[0]==REXP::Integer::NA
    v.attributes['row.names']=(1..(-attr.as_list['row.names'].as_integers[1])).to_a
  end
  
  v
end
to_ruby_internal() click to toggle source

Return the bare-bone representation of REXP as a Ruby Object. Called by REXP.to_ruby, so shouldn’t be used directly by developers.

# File lib/rserve/rexp.rb, line 416
def to_ruby_internal
  raise "You should implement to_ruby_internal for #{self.class}"
end
vector?() click to toggle source

check whether the REXP object is a vector.

@return [boolean] true if the receiver is a vector, false otherwise

# File lib/rserve/rexp.rb, line 98
def vector?
  false
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.