Class: ERObj

Inherits:
Object
  • Object
show all
Defined in:
lib/rsruby/erobj.rb

Overview

Synopsis

This is an extended RObj class inspired by the example given in the RPy manual. Methods caught by method_missing are converted into attribute calls on the R object it represents. Also to_s is redefined to print exactly the representation used in R.

Usage

See examples/erobj.rb for examples of usage.

Author

Alex Gutteridge

Copyright © 2006 Alex Gutteridge

The Original Code is the RPy python module.

The Initial Developer of the Original Code is Walter Moreira. Portions created by the Initial Developer are Copyright © 2002 the Initial Developer. All Rights Reserved.

Contributor(s):

Gregory R. Warnes <[email protected]> (RPy Maintainer)

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ++

Direct Known Subclasses

DataFrame

Constant Summary collapse

@@x =
1

Instance Method Summary collapse

Constructor Details

#initialize(robj) ⇒ ERObj

The ERObj is intialised by passing it an RObj instance which it then stores



51
52
53
54
# File 'lib/rsruby/erobj.rb', line 51

def initialize(robj)
  @robj = robj
  @r    = RSRuby.instance
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(attr) ⇒ Object

Methods caught by method_missing are converted into attribute calls on the R object it represents.



97
98
99
100
101
102
103
# File 'lib/rsruby/erobj.rb', line 97

def method_missing(attr)
  mode = RSRuby.get_default_mode
  RSRuby.set_default_mode(RSRuby::BASIC_CONVERSION)
  e = @r['$'].call(@robj,attr.to_s)
  RSRuby.set_default_mode(mode)
  return e
end

Instance Method Details

#as_rObject

Returns the storred RObj.



57
58
59
# File 'lib/rsruby/erobj.rb', line 57

def as_r
  @robj.as_r
end

#lcall(args) ⇒ Object

Calls the storred RObj.



68
69
70
# File 'lib/rsruby/erobj.rb', line 68

def lcall(args)
  @robj.lcall(args)
end

#to_rubyObject

Returns the Ruby representation of the object according to the basic conversion mode.



63
64
65
# File 'lib/rsruby/erobj.rb', line 63

def to_ruby
  @robj.to_ruby(RSRuby::BASIC_CONVERSION)
end

#to_sObject

Outputs the string representation provided by R.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rsruby/erobj.rb', line 73

def to_s

  @@x += 1

  mode = RSRuby.get_default_mode
  RSRuby.set_default_mode(RSRuby::NO_CONVERSION)
  a = @r.textConnection("tmpobj#{@@x}",'w')

  RSRuby.set_default_mode(RSRuby::VECTOR_CONVERSION)
  @r.sink(:file => a, :type => 'output')
  @r.print_(@robj)
  @r.sink.call()
  @r.close_connection(a)

  str = @r["tmpobj#{@@x}"].join("\n")

  RSRuby.set_default_mode(mode)

  return str

end