class R::Object

Attributes

r_interop[R]
statement[RW]

Public Class Methods

build(r_interop) click to toggle source
# File lib/R_interface/robject.rb, line 73
def self.build(r_interop)

  # if the value is actually not an r_interop, then just return it: native Ruby
  # object
  if (!Truffle::Interop.foreign?(r_interop))
    # puts "I'm native"
    return r_interop
  # a matrix is also a vector... test should come before
  elsif (R::Support.eval("is.matrix").call(r_interop) == true)
    # puts "1"
    R::Matrix.new(r_interop)
  elsif (R::Support.eval("is.atomic").call(r_interop) == true)
    # puts "2"
    Vector.new(r_interop)
  elsif (R::Support.eval("is.function").call(r_interop) == true)
    # puts "3"
    Closure.new(r_interop)
  elsif (R::Support.eval("is.data.frame").call(r_interop) == true)
    # puts "4"
    DataFrame.new(r_interop)
  elsif (R::Support.eval("is.list").call(r_interop) == true)
    # puts "5"
    List.new(r_interop)
  elsif (R::Support.eval("typeof").call(r_interop) == "language")
    # @TODO: tests are passing both when we use Language.new
    # and RExpression.new.  Check what should be and write
    # discriminating tests.
    # print "robject buid: language\n"
    # R::Support.print_foreign(r_interop)
    # puts "6"
    Language.new(r_interop)
    # RExpression.new(r_interop)
  elsif (R::Support.eval("typeof").call(r_interop) == "expression")
    # puts "7"
    RExpression.new(r_interop)
  elsif (R::Support.eval("typeof").call(r_interop) == "name")
    # puts "8"
    p "i'm of type name"
    Name.new(r_interop)
  elsif (R::Support.eval("typeof").call(r_interop) == "symbol")
    # puts "9"
    RSymbol.new(r_interop)
  elsif (R::Support.eval("typeof").call(r_interop) == "environment")
    # puts "10"
    Environment.new(r_interop)
  else # Generic type
    # puts "11"
    p "Generic type: #{R::Support.eval("typeof").call(r_interop).to_s}"
    r_interop
  end

end
new(r_interop) click to toggle source
# File lib/R_interface/robject.rb, line 40
def initialize(r_interop)
  @r_interop = r_interop
end

Public Instance Methods

==(other_object) click to toggle source
# File lib/R_interface/robject.rb, line 50
def ==(other_object)
  res =
    R::Support.exec_function_name('identical', @r_interop,
                                  R::Support.parse_arg(other_object))
  return nil if (res.length >> 0) == 0
  res >> 0
end
_(*args) click to toggle source
# File lib/R_interface/robject.rb, line 181
def _(*args)
  name = "`%#{args.shift.to_s}%`"
  args.unshift(@r_interop)
  R::Support.exec_function_name(name, *args)
end
attr=(which: w, value: v) click to toggle source
# File lib/R_interface/robject.rb, line 304
def attr=(which: w, value: v)
  value = (R::Support.interop(value) ? value.r_interop : value)
  # setR(@@set_attr, which, value)
  setR_name("`attr<-`", which, value)
end
comment() click to toggle source
# File lib/R_interface/robject.rb, line 245
def comment
  R::Support.exec_function_name("comment", @r_interop)
end
comment=(comment_text) click to toggle source
# File lib/R_interface/robject.rb, line 241
def comment=(comment_text)
  setR_name("`comment<-`", comment_text)
end
dim() click to toggle source
# File lib/R_interface/robject.rb, line 257
def dim
  R::Support.exec_function_name("dim", @r_interop)
end
dim=(numeric_vector) click to toggle source
# File lib/R_interface/robject.rb, line 253
def dim=(numeric_vector)
  setR_name("`dim<-`", numeric_vector)
end
dimnames() click to toggle source
# File lib/R_interface/robject.rb, line 269
def dimnames
  R::Support.exec_function_name("dimnames", @r_interop)
end
dimnames=(names_vector) click to toggle source
# File lib/R_interface/robject.rb, line 265
def dimnames=(names_vector)
  setR_name("`dimnames<-`", names_vector)
end
eql(other_object) click to toggle source
# File lib/R_interface/robject.rb, line 62
def eql(other_object)
  # exec_bin_oper("`==`", other_object)
  R::Support.exec_function_name("`==`", @r_interop,
                                R::Support.parse_arg(other_object))
end
method_missing(symbol, *args, &block) click to toggle source
# File lib/R_interface/robject.rb, line 130
def method_missing(symbol, *args, &block)
  name = R::Support.convert_symbol2r(symbol)

  # Need to raise a NoMethodError when method_missing is called by an implicit
  # call to "to_ary".  The explanation for that is:
  # Okay, I've found the source of the behaviour. IOOperations.puts will attempt to
  # coerce an argument to an array and print its contents, and R::Vector responds to
  # to_ary but returns the empty array which results in no output.
  #
  # Previously IOOperations.puts only checked the type of the argument, and did not
  # attempt coercion, but that meant we didn't match MRI's behaviour and had some test
  # failures in other gems. I'd suggest either removing the to_ary methods on
  # R::Object and R::Vector or implementing them more fully. I see from the comments on
  # those methods that you needed them for RSpec, it might worth seeing if those problems
  # still occur and we can look a better way to resolve those.
  raise NoMethodError if name == "to_ary"

  case
  when block_given?
    R::Support.new_scope(symbol, self, *args, &block)
  when name =~ /(.*)=$/
    method_missing_assign($1, args[0])
  when name == "eval"
    # R function 'eval' needs to be called in a special way, since it expects
    # the second argument to be an environment.  If the arguments are packed
    # into a list, then there is no second argument and the function fails to
    # use the second argument as environment
    R::Support.r_evaluate(@r_interop, *args)
  when args.length == 0
    # no arguments: 2 options: either a named item of the object or apply the function
    # to the object
    # if name is a named item of the object, then return the named item
    named = R::Support.eval("`%in%`").
              call(name, R::Support.eval("names").call(@r_interop))
    (false === named || !(true === named || named[0])) ?
      R::Support.exec_function_name(name, @r_interop) :
      R::Support.exec_function_name("`[[`", @r_interop, name)
  else
    args.unshift(@r_interop)
    R::Support.exec_function_name(name, *args)
  end
    
end
names(*args) click to toggle source
# File lib/R_interface/robject.rb, line 219
def names(*args)
  return R::Support.exec_function_name("names", @r_interop) if (args.length == 0)
  setR_name("`names<-`", *args)
  self
end
names=(names_vector) click to toggle source
# File lib/R_interface/robject.rb, line 215
def names=(names_vector)
  setR_name("`names<-`", names_vector)
end
pp() click to toggle source
# File lib/R_interface/robject.rb, line 314
def pp
  R.print(@r_interop)
end
pretty_print(obj) click to toggle source
# File lib/R_interface/robject.rb, line 322
def pretty_print(obj)
  puts self
end
rclass() click to toggle source
# File lib/R_interface/robject.rb, line 233
def rclass
  R::Support.exec_function_name("class", @r_interop)
end
rclass=(class_name) click to toggle source
# File lib/R_interface/robject.rb, line 229
def rclass=(class_name)
  setR_name("`class<-`", class_name)
end
row__names() click to toggle source
# File lib/R_interface/robject.rb, line 284
def row__names
  R::Support.exec_function_name("row.names", @r_interop)
end
row__names=(names_vector) click to toggle source

since we need to call a method and the method changes the object, then we need to change our internal pointer also @r_interop. Ideally, just setting the row.names should work.

# File lib/R_interface/robject.rb, line 280
def row__names=(names_vector)
  setR_name("`row.names<-`", names_vector)
end
setR(method, *args) click to toggle source
# File lib/R_interface/robject.rb, line 195
def setR(method, *args)
  @r_interop = R::Support.exec_function_i(method, @r_interop, *args)
  self
end
setR_name(method_name, *args) click to toggle source
# File lib/R_interface/robject.rb, line 204
def setR_name(method_name, *args)
  method = R::Support.eval(method_name)
  setR(method, *args)
  self
end
to_s() click to toggle source
# File lib/R_interface/robject.rb, line 330
def to_s

  begin
    cap = nil
    cap = R::Support.capture2.call(r_interop)
    str = String.new
    (0...(cap.size - 1)).each do |i|
      str << cap[i] << "\n"
    end
    str << cap[cap.size - 1] if cap.size >= 1
    str
  rescue StandardError => e
    puts e
  end
  
end
tsp() click to toggle source
# File lib/R_interface/robject.rb, line 296
def tsp
  R::Support.exec_function_name("tsp", @r_interop)
end
tsp=(numeric_vector) click to toggle source
# File lib/R_interface/robject.rb, line 292
def tsp=(numeric_vector)
  setR_name("`tsp<-`", numeric_vector)
end