class 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 MismatchError
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
- MaxDebugItems
specifies how many items of a vector or list will be displayed in {@link toDebugString}
- MismatchError
Attributes
Public Class Methods
# 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
basic accessor methods
↑ topPublic Instance Methods
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 MismatchError , "byte" end
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 MismatchError,"double" end
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 MismatchError,"factor" end
On Ruby, Float are stored in double precision.
@return [Array]
# File lib/rserve/rexp.rb, line 149 def as_floats as_doubles end
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 MismatchError, "int" end
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 MismatchError,"list" end
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 MismatchError, "String" end
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 MismatchError, "vector" end
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 MismatchError, "vector" end
complex convenience methods
↑ topPublic Instance Methods
returns the content of the REXP
as a ruby matrix of doubles (2D-array: m[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[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 MismatchError, "matrix (dim attribute missing)" if dim.nil? ds = dim.as_integers raise MismatchError, "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
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
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 MismatchError, "array (dim attribute missing" if dim.nil? ds = dim.as_integers.reverse split_array(ct,ds) end
convenience accessor methods
↑ topPublic Instance Methods
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
Alias for as_double
()
@return [Float]
# File lib/rserve/rexp.rb, line 213 def as_float as_double end
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
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
Alias for as_integer
().
@return [Integer]
# File lib/rserve/rexp.rb, line 201 def to_i as_integers[0] end
helper methods common to all REXPs
↑ topPublic Instance Methods
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 MismatchError # nothing to do end nil end
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 MismatchError end false end
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
methods common to all REXPs
↑ topPublic Instance Methods
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
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
tools
↑ topPublic Class Methods
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(MismatchError, "data frame (must have dim>0)") if l.nil? or l.size<1 raise MismatchError, "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
Public Instance Methods
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 v=to_ruby_internal if !v.nil? and !v.is_a? Numeric 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 if v.attributes and v.attributes.has_name? 'dim' and v.attributes.has_name? 'dimnames' and v.attributes['dim'].size == 2 if v.is_a? Array v.extend Rserve::With2DSizes v.sizes = v.attributes['dim'] end v.extend Rserve::With2DNames v.names = v.attributes['dimnames'].map{|dimension_names| (dimension_names.nil? or dimension_names.is_a?(Array)) ? dimension_names : [dimension_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
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 420 def to_ruby_internal raise "You should implement to_ruby_internal for #{self.class}" end
type checks
↑ topPublic Instance Methods
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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