rsruby 0.4.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +1,18 @@
1
+ require 'rsruby'
2
+ require 'rsruby/erobj'
3
+
1
4
  #== Synopsis
2
5
  #
3
- #This is an extended #ERObj class inspired by the example given in the RPy
6
+ #This is an extended ERObj class inspired by the example given in the RPy
4
7
  #manual used for R data frames.
5
- #As with ERobj, methods caught by method_missing are converted into attribute
8
+ #As with ERObj, methods caught by method_missing are converted into attribute
6
9
  #calls on the R dataframe it represents. The rows and columns methods give
7
10
  #access to the column and row names.
8
11
  #
9
12
  #== Usage
10
13
  #
11
- #See examples/dataframe.rb for examples of usage.
14
+ #See examples/dataframe.rb[link:files/examples/dataframe_rb.html] for
15
+ #examples of usage.
12
16
  #
13
17
  #--
14
18
  #== Author
@@ -41,9 +45,6 @@
41
45
  #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
42
46
  #++
43
47
 
44
- require 'rsruby'
45
- require 'rsruby/erobj'
46
-
47
48
  class DataFrame < ERObj
48
49
 
49
50
  #Returns an array of the row names used in the R data frame.
@@ -58,6 +59,10 @@ class DataFrame < ERObj
58
59
  return cols
59
60
  end
60
61
 
62
+ def[](col)
63
+ return @r['$'].call(@robj,col.to_s)
64
+ end
65
+
61
66
  def method_missing(attr)
62
67
  attr = attr.to_s
63
68
  mode = RSRuby.get_default_mode
data/lib/rsruby/erobj.rb CHANGED
@@ -1,13 +1,16 @@
1
+ require 'rsruby'
2
+
1
3
  #== Synopsis
2
4
  #
3
- #This is an extended #RObj class inspired by the example given in the RPy
5
+ #This is an extended RObj class inspired by the example given in the RPy
4
6
  #manual. Methods caught by method_missing are converted into attribute calls
5
7
  #on the R object it represents. Also to_s is redefined to print exactly the
6
- #representation used in R
8
+ #representation used in R.
7
9
  #
8
10
  #== Usage
9
11
  #
10
- #See examples/erobj.rb for examples of usage.
12
+ #See examples/erobj.rb[link:files/examples/erobj_rb.html] for examples of
13
+ #usage.
11
14
  #
12
15
  #--
13
16
  # == Author
@@ -40,25 +43,28 @@
40
43
  #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
41
44
  #++
42
45
 
43
- require 'rsruby'
44
-
45
46
  class ERObj
46
47
 
47
48
  @@x = 1
48
49
 
49
- #Requires an RObj for intialization.
50
+ #The ERObj is intialised by passing it an RObj instance which it then stores
50
51
  def initialize(robj)
51
52
  @robj = robj
52
53
  @r = RSRuby.instance
53
54
  end
54
55
 
55
- #Returns the wrapped RObj when RSRuby attempts to convert it to
56
- #R.
56
+ #Returns the storred RObj.
57
57
  def as_r
58
58
  @robj.as_r
59
59
  end
60
60
 
61
- #Calls the wrapped RObj.
61
+ #Returns the Ruby representation of the object according to the basic
62
+ #conversion mode.
63
+ def to_ruby
64
+ @robj.to_ruby(RSRuby::BASIC_CONVERSION)
65
+ end
66
+
67
+ #Calls the storred RObj.
62
68
  def lcall(args)
63
69
  @robj.lcall(args)
64
70
  end
@@ -72,7 +78,7 @@ class ERObj
72
78
  RSRuby.set_default_mode(RSRuby::NO_CONVERSION)
73
79
  a = @r.textConnection("tmpobj#{@@x}",'w')
74
80
 
75
- RSRuby.set_default_mode(RSRuby::BASIC_CONVERSION)
81
+ RSRuby.set_default_mode(RSRuby::VECTOR_CONVERSION)
76
82
  @r.sink(:file => a, :type => 'output')
77
83
  @r.print_(@robj)
78
84
  @r.sink.call()
@@ -86,6 +92,8 @@ class ERObj
86
92
 
87
93
  end
88
94
 
95
+ #Methods caught by method_missing are converted into attribute calls on
96
+ #the R object it represents.
89
97
  def method_missing(attr)
90
98
  mode = RSRuby.get_default_mode
91
99
  RSRuby.set_default_mode(RSRuby::BASIC_CONVERSION)
data/lib/rsruby/robj.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  #This class represents a reference to an object in the R interpreter. It
4
4
  #also holds a conversion mode used if the RObj represents a callable function.
5
5
  #RObj objects can be passed to R functions called from Ruby and are the
6
- #default return type if rsruby cannot convert the returned results of an R
6
+ #default return type if RSRuby cannot convert the returned results of an R
7
7
  #function.
8
8
  #
9
9
  #--
@@ -32,7 +32,9 @@ class RObj
32
32
 
33
33
  attr_accessor :conversion, :wrap
34
34
 
35
- :private
35
+ #Attempts to call the RObj with the arguments given. Returns the result
36
+ #of calling the R object. Only use this method if the RObj represents an
37
+ #R function.
36
38
  def call(*args)
37
39
  if @wrap
38
40
  e = RSRuby.get_default_mode
@@ -45,6 +47,9 @@ class RObj
45
47
  return ret
46
48
  end
47
49
 
50
+ #Sets the conversion mode for this RObj (only relevant if the RObj
51
+ #represents a function). See the constants in RSRuby for valid modes.
52
+ #Returns the current conversion mode if called with no argument.
48
53
  def autoconvert(m=false)
49
54
  if m
50
55
  raise ArgumentError if m < -1 or m > RSRuby::TOP_CONVERSION
data/test/table.txt ADDED
@@ -0,0 +1,4 @@
1
+ A B C D
2
+ 'X1' 4.0 5 '6'
3
+ 'X2' 7.0 8 '9'
4
+ 'X3' 6.0 2 'Foo'
data/test/tc_array.rb CHANGED
@@ -1,22 +1,19 @@
1
1
  require 'test/unit'
2
2
  require 'rsruby'
3
3
 
4
- unless $r
5
- $r = RSRuby.instance
6
- end
7
-
8
4
  class TestArray < Test::Unit::TestCase
9
5
 
10
6
  def setup
11
7
 
8
+ @r = RSRuby.instance
12
9
  @ruby_AoA = [[[0,6,12,18],[2,8,14,20],[4,10,16,22]],
13
10
  [[1,7,13,19],[3,9,15,21],[5,11,17,23]]]
14
11
 
15
12
  RSRuby.set_default_mode(RSRuby::NO_DEFAULT)
16
- $r.array.autoconvert(RSRuby::NO_CONVERSION)
17
- @r_array = $r.array({ :data => (0..24).to_a,
13
+ @r.array.autoconvert(RSRuby::NO_CONVERSION)
14
+ @r_array = @r.array({ :data => (0..24).to_a,
18
15
  :dim => [2,3,4]})
19
- $r.array.autoconvert(RSRuby::BASIC_CONVERSION)
16
+ @r.array.autoconvert(RSRuby::BASIC_CONVERSION)
20
17
  end
21
18
 
22
19
  def test_convert_to_ruby
@@ -25,22 +22,22 @@ class TestArray < Test::Unit::TestCase
25
22
 
26
23
  #I suspect this only works in RPy with Numeric?
27
24
  def test_convert_to_R
28
- $r.list.autoconvert(RSRuby::NO_CONVERSION)
29
- $r['[['].autoconvert(RSRuby::NO_CONVERSION)
30
- o = $r['[['].call($r.list(@ruby_AoA),1)
31
- $r['[['].autoconvert(RSRuby::BASIC_CONVERSION)
32
- #assert_equal($r.all_equal(o,@r_array),true)
25
+ @r.list.autoconvert(RSRuby::NO_CONVERSION)
26
+ @r['[['].autoconvert(RSRuby::NO_CONVERSION)
27
+ o = @r['[['].call(@r.list(@ruby_AoA),1)
28
+ @r['[['].autoconvert(RSRuby::BASIC_CONVERSION)
29
+ #assert_equal(@r.all_equal(o,@r_array),true)
33
30
  end
34
31
 
35
32
  def test_dimensions
36
- assert_equal($r.dim(@r_array),[@ruby_AoA.length,
33
+ assert_equal(@r.dim(@r_array),[@ruby_AoA.length,
37
34
  @ruby_AoA[0].length,
38
35
  @ruby_AoA[0][0].length])
39
36
  end
40
37
 
41
38
  def test_elements
42
- assert_equal(@ruby_AoA[0][0][0],$r['[['].call(@r_array, 1,1,1))
43
- assert_equal(@ruby_AoA[1][1][1],$r['[['].call(@r_array, 2,2,2))
39
+ assert_equal(@ruby_AoA[0][0][0],@r['[['].call(@r_array, 1,1,1))
40
+ assert_equal(@ruby_AoA[1][1][1],@r['[['].call(@r_array, 2,2,2))
44
41
  end
45
42
 
46
43
  def test_ruby_out_of_bounds
@@ -51,7 +48,7 @@ class TestArray < Test::Unit::TestCase
51
48
 
52
49
  def test_R_out_of_bounds
53
50
  assert_raise RException do
54
- $r['[['].call(@r_array, 5,5,5)
51
+ @r['[['].call(@r_array, 5,5,5)
55
52
  end
56
53
  end
57
54
 
data/test/tc_boolean.rb CHANGED
@@ -1,27 +1,24 @@
1
1
  require 'test/unit'
2
2
  require 'rsruby'
3
3
 
4
- unless $r
5
- $r = RSRuby.instance
6
- end
7
-
8
4
  class TestBoolean < Test::Unit::TestCase
9
5
 
10
6
  def setup
7
+ @r = RSRuby.instance
11
8
  RSRuby.set_default_mode(RSRuby::NO_DEFAULT)
12
9
  end
13
10
 
14
11
  def test_true
15
12
  assert_block "r.TRUE not working" do
16
- ($r.typeof($r.FALSE) == 'logical' and
17
- $r.as_logical($r.TRUE))
13
+ (@r.typeof(@r.FALSE) == 'logical' and
14
+ @r.as_logical(@r.TRUE))
18
15
  end
19
16
  end
20
17
 
21
18
  def test_false
22
19
  assert_block "r.FALSE not working" do
23
- ($r.typeof($r.FALSE) == 'logical' and not
24
- $r.as_logical($r.FALSE))
20
+ (@r.typeof(@r.FALSE) == 'logical' and not
21
+ @r.as_logical(@r.FALSE))
25
22
  end
26
23
  end
27
24
  end
data/test/tc_cleanup.rb CHANGED
@@ -1,22 +1,20 @@
1
1
  require 'test/unit'
2
2
  require 'rsruby'
3
3
 
4
- unless $r
5
- $r = RSRuby.instance
6
- end
7
-
8
4
  class TestCleanup < Test::Unit::TestCase
9
5
 
10
- def test_shutdown
11
- #TODO - shutdown doesn't really seem to do anything???
12
- $r.shutdown
13
- assert $r.nil?
14
- $r = RSRuby.instance()
6
+ def setup
7
+ @r = RSRuby.instance
15
8
  end
16
9
 
17
- def test_restart
18
- $r.shutdown
19
- $r = RSRuby.instance()
20
- assert $r
10
+ def test_shutdown
11
+ tempdir = @r.tempdir.call
12
+ @r.postscript(tempdir+"/foo.ps")
13
+ @r.plot(1,1)
14
+ @r.dev_off.call
15
+ assert(File.exists?(tempdir))
16
+ @r.shutdown
17
+ assert(!File.exists?(tempdir))
21
18
  end
19
+
22
20
  end
data/test/tc_eval.rb CHANGED
@@ -3,13 +3,17 @@ require 'rsruby'
3
3
 
4
4
  class TestEval < Test::Unit::TestCase
5
5
 
6
+ def setup
7
+ @r = RSRuby.instance
8
+ end
9
+
6
10
  def test_eval_R
7
11
  #Test integer, Float, String and Boolean return values
8
- assert_equal($r.eval_R("sum(1,2,3)"),6)
9
- assert_equal($r.eval_R("sum(1.5,2.5,3.5)"),7.5)
10
- assert_equal($r.eval_R("eval('R')"),"R")
11
- assert_equal($r.eval_R("is(1,'numeric')"),true)
12
- assert_equal($r.eval_R("is(1,'madeup')"),false)
12
+ assert_equal(@r.eval_R("sum(1,2,3)"),6)
13
+ assert_equal(@r.eval_R("sum(1.5,2.5,3.5)"),7.5)
14
+ assert_equal(@r.eval_R("eval('R')"),"R")
15
+ assert_equal(@r.eval_R("is(1,'numeric')"),true)
16
+ assert_equal(@r.eval_R("is(1,'madeup')"),false)
13
17
  end
14
18
 
15
19
  end
@@ -0,0 +1,26 @@
1
+ require 'test/unit'
2
+ require 'rsruby'
3
+
4
+ class TestNewCases < Test::Unit::TestCase
5
+
6
+ def test_erobj
7
+
8
+ require 'rsruby/erobj'
9
+ r = RSRuby.instance
10
+ r.proc_table[lambda{|x| true}] = lambda{|x| ERObj.new(x)}
11
+ RSRuby.set_default_mode(RSRuby::PROC_CONVERSION)
12
+
13
+ f = r.c(1,2,3)
14
+ assert_equal('[1] 1 2 3',f.to_s)
15
+ assert_equal([1,2,3],f.to_ruby)
16
+
17
+ end
18
+
19
+ def test_dataframe
20
+
21
+ r = RSRuby.instance
22
+ r.as_data_frame({'x' => [1,2,3], 'row.names' => ['a','b','c']})
23
+
24
+ end
25
+
26
+ end
data/test/tc_io.rb CHANGED
@@ -1,10 +1,6 @@
1
1
  require 'test/unit'
2
2
  require 'rsruby'
3
3
 
4
- unless $r
5
- $r = RSRuby.instance
6
- end
7
-
8
4
  class DummyIO
9
5
  def write
10
6
  end
@@ -13,6 +9,7 @@ end
13
9
  class TestIO < Test::Unit::TestCase
14
10
 
15
11
  def setup
12
+ @r = RSRuby.instance
16
13
  $stdout = $stderr = DummyIO.new
17
14
  end
18
15
 
@@ -23,15 +20,15 @@ class TestIO < Test::Unit::TestCase
23
20
 
24
21
  def test_io_stdin
25
22
  dummy = lambda{|prompt,n| prompt+'\n'}
26
- $r.set_rsruby_input(dummy)
27
- assert $r.readline('foo') == 'foo'
23
+ @r.set_rsruby_input(dummy)
24
+ assert @r.readline('foo') == 'foo'
28
25
  end
29
26
 
30
27
  def test_io_stdout
31
28
  out = []
32
29
  dummy = lambda{|string| out.push(string)}
33
- $r.set_rsruby_output(dummy)
34
- $r.print(5)
30
+ @r.set_rsruby_output(dummy)
31
+ @r.print(5)
35
32
  assert out == ['[1]','5','\n']
36
33
  end
37
34
 
@@ -40,8 +37,8 @@ class TestIO < Test::Unit::TestCase
40
37
  dummy = lambda{|files,headers,title,delete|
41
38
  out.push('foo')
42
39
  }
43
- $r.set_rsruby_showfiles(dummy)
44
- $r.help()
40
+ @r.set_rsruby_showfiles(dummy)
41
+ @r.help()
45
42
  assert out == ['foo']
46
43
  end
47
44
 
data/test/tc_library.rb CHANGED
@@ -1,20 +1,20 @@
1
1
  require 'test/unit'
2
2
  require 'rsruby'
3
3
 
4
- unless $r
5
- $r = RSRuby.instance
6
- end
7
-
8
4
  class TestLibrary < Test::Unit::TestCase
9
5
 
6
+ def setup
7
+ @r = RSRuby.instance
8
+ end
9
+
10
10
  def test_library
11
11
  #Test success
12
- assert_nothing_raised(){$r.library("boot")}
12
+ assert_nothing_raised(){@r.library("boot")}
13
13
  end
14
14
 
15
15
  def test_library_fail
16
16
  #Test failure
17
- assert_raises(RException){$r.library("Missing")}
17
+ assert_raises(RException){@r.library("Missing")}
18
18
  end
19
19
 
20
20
  end
data/test/tc_modes.rb CHANGED
@@ -1,38 +1,35 @@
1
1
  require 'test/unit'
2
2
  require 'rsruby'
3
3
 
4
- unless $r
5
- $r = RSRuby.instance
6
- end
7
-
8
4
  class TestModes < Test::Unit::TestCase
9
5
 
10
6
  def setup
7
+ @r = RSRuby.instance
11
8
  RSRuby.set_default_mode(RSRuby::NO_DEFAULT)
12
- $r.class_table.clear
13
- $r.proc_table.clear
9
+ @r.class_table.clear
10
+ @r.proc_table.clear
14
11
  end
15
12
 
16
13
  def test_to_ruby_args
17
- assert_raises(ArgumentError){$r.seq.to_ruby(RSRuby::TOP_CONVERSION+1)}
18
- assert_raises(ArgumentError){$r.seq.to_ruby(-2)}
19
- assert_raises(TypeError){$r.seq.to_ruby('foo')}
14
+ assert_raises(ArgumentError){@r.seq.to_ruby(RSRuby::TOP_CONVERSION+1)}
15
+ assert_raises(ArgumentError){@r.seq.to_ruby(-2)}
16
+ assert_raises(TypeError){@r.seq.to_ruby('foo')}
20
17
  end
21
18
 
22
19
  def test_to_ruby
23
- $r.c.autoconvert(RSRuby::NO_CONVERSION)
24
- four = $r.c(4)
20
+ @r.c.autoconvert(RSRuby::NO_CONVERSION)
21
+ four = @r.c(4)
25
22
  assert_equal(four.to_ruby, 4)
26
23
  assert_equal(four.to_ruby(RSRuby::PROC_CONVERSION), 4)
27
24
  assert_equal(four.to_ruby(RSRuby::BASIC_CONVERSION), 4)
28
25
  assert_equal(four.to_ruby(RSRuby::VECTOR_CONVERSION), [4])
29
- assert($r["=="].call(four.to_ruby(RSRuby::NO_CONVERSION),four))
26
+ assert(@r["=="].call(four.to_ruby(RSRuby::NO_CONVERSION),four))
30
27
  end
31
28
 
32
29
  def test_to_ruby_default_arg
33
30
  RSRuby.set_default_mode(RSRuby::NO_CONVERSION)
34
- sequence = $r.seq(1,3)
35
- t_test = $r.t_test([1,2,3])
31
+ sequence = @r.seq(1,3)
32
+ t_test = @r.t_test([1,2,3])
36
33
 
37
34
  assert_equal(sequence.to_ruby.class , RObj)
38
35
  assert_equal(sequence.to_ruby.class, sequence.class)
@@ -43,11 +40,11 @@ class TestModes < Test::Unit::TestCase
43
40
  RSRuby.set_default_mode(RSRuby::VECTOR_CONVERSION)
44
41
  assert_equal(sequence.to_ruby, [1,2,3])
45
42
 
46
- $r.class_table['htest'] = lambda{5}
43
+ @r.class_table['htest'] = lambda{5}
47
44
  RSRuby.set_default_mode(RSRuby::CLASS_CONVERSION)
48
45
  assert_equal(t_test.to_ruby, 5)
49
46
 
50
- $r.proc_table[lambda{true}] = lambda{return 6}
47
+ @r.proc_table[lambda{true}] = lambda{return 6}
51
48
  RSRuby.set_default_mode(RSRuby::PROC_CONVERSION)
52
49
  assert_equal(t_test.to_ruby, 6)
53
50
  end
@@ -73,50 +70,50 @@ class TestModes < Test::Unit::TestCase
73
70
  end
74
71
 
75
72
  def test_no_default_mode
76
- $r.t_test.autoconvert(RSRuby::CLASS_CONVERSION)
77
- $r.array.autoconvert(RSRuby::NO_CONVERSION)
78
- $r.seq.autoconvert(RSRuby::BASIC_CONVERSION)
73
+ @r.t_test.autoconvert(RSRuby::CLASS_CONVERSION)
74
+ @r.array.autoconvert(RSRuby::NO_CONVERSION)
75
+ @r.seq.autoconvert(RSRuby::BASIC_CONVERSION)
79
76
 
80
- assert_equal($r.array(1,3).class, $r.array.class)
77
+ assert_equal(@r.array(1,3).class, @r.array.class)
81
78
 
82
- assert_equal($r.seq(1,3), [1,2,3])
83
- $r.class_table['htest'] = lambda{5}
84
- assert_equal($r.t_test([1,2,3]), 5)
79
+ assert_equal(@r.seq(1,3), [1,2,3])
80
+ @r.class_table['htest'] = lambda{5}
81
+ assert_equal(@r.t_test([1,2,3]), 5)
85
82
  end
86
83
 
87
84
  def test_individual_conversions
88
- $r.c.autoconvert(RSRuby::BASIC_CONVERSION)
89
- $r.seq.autoconvert(RSRuby::PROC_CONVERSION)
90
- $r.min.autoconvert(RSRuby::VECTOR_CONVERSION)
85
+ @r.c.autoconvert(RSRuby::BASIC_CONVERSION)
86
+ @r.seq.autoconvert(RSRuby::PROC_CONVERSION)
87
+ @r.min.autoconvert(RSRuby::VECTOR_CONVERSION)
91
88
 
92
89
  RSRuby.set_default_mode(RSRuby::NO_CONVERSION)
93
- assert_equal($r.c(4).class, RObj)
94
- assert_equal($r.seq(1,3).class, RObj)
95
- assert_equal($r.min(1,3).class, RObj)
90
+ assert_equal(@r.c(4).class, RObj)
91
+ assert_equal(@r.seq(1,3).class, RObj)
92
+ assert_equal(@r.min(1,3).class, RObj)
96
93
 
97
94
  RSRuby.set_default_mode(RSRuby::NO_DEFAULT)
98
- assert_equal($r.c.autoconvert, RSRuby::BASIC_CONVERSION)
99
- assert_equal($r.seq.autoconvert, RSRuby::PROC_CONVERSION)
100
- assert_equal($r.min.autoconvert, RSRuby::VECTOR_CONVERSION)
101
- assert_equal($r.c(4), 4)
102
- assert_equal($r.seq(1,3), [1,2,3])
103
- assert_equal($r.min(1,3), [1])
95
+ assert_equal(@r.c.autoconvert, RSRuby::BASIC_CONVERSION)
96
+ assert_equal(@r.seq.autoconvert, RSRuby::PROC_CONVERSION)
97
+ assert_equal(@r.min.autoconvert, RSRuby::VECTOR_CONVERSION)
98
+ assert_equal(@r.c(4), 4)
99
+ assert_equal(@r.seq(1,3), [1,2,3])
100
+ assert_equal(@r.min(1,3), [1])
104
101
 
105
102
  RSRuby.set_default_mode(RSRuby::BASIC_CONVERSION)
106
- assert_equal($r.c.autoconvert, RSRuby::BASIC_CONVERSION)
107
- assert_equal($r.seq.autoconvert, RSRuby::PROC_CONVERSION)
108
- assert_equal($r.min.autoconvert, RSRuby::VECTOR_CONVERSION)
109
- assert_equal($r.c(4), 4)
110
- assert_equal($r.seq(1,3), [1,2,3])
111
- assert_equal($r.min(1,3), 1)
103
+ assert_equal(@r.c.autoconvert, RSRuby::BASIC_CONVERSION)
104
+ assert_equal(@r.seq.autoconvert, RSRuby::PROC_CONVERSION)
105
+ assert_equal(@r.min.autoconvert, RSRuby::VECTOR_CONVERSION)
106
+ assert_equal(@r.c(4), 4)
107
+ assert_equal(@r.seq(1,3), [1,2,3])
108
+ assert_equal(@r.min(1,3), 1)
112
109
 
113
110
  RSRuby.set_default_mode(RSRuby::VECTOR_CONVERSION)
114
- assert_equal($r.c.autoconvert, RSRuby::BASIC_CONVERSION)
115
- assert_equal($r.seq.autoconvert, RSRuby::PROC_CONVERSION)
116
- assert_equal($r.min.autoconvert, RSRuby::VECTOR_CONVERSION)
117
- assert_equal($r.c(4), [4])
118
- assert_equal($r.seq(1,3), [1,2,3])
119
- assert_equal($r.min(1,3), [1])
111
+ assert_equal(@r.c.autoconvert, RSRuby::BASIC_CONVERSION)
112
+ assert_equal(@r.seq.autoconvert, RSRuby::PROC_CONVERSION)
113
+ assert_equal(@r.min.autoconvert, RSRuby::VECTOR_CONVERSION)
114
+ assert_equal(@r.c(4), [4])
115
+ assert_equal(@r.seq(1,3), [1,2,3])
116
+ assert_equal(@r.min(1,3), [1])
120
117
 
121
118
  end
122
119
 
@@ -124,67 +121,67 @@ class TestModes < Test::Unit::TestCase
124
121
 
125
122
  RSRuby.set_default_mode(RSRuby::VECTOR_CONVERSION)
126
123
 
127
- assert_equal($r.c(true), [true])
128
- assert_equal($r.c(4) , [4])
129
- assert_equal($r.c('A') , ['A'])
124
+ assert_equal(@r.c(true), [true])
125
+ assert_equal(@r.c(4) , [4])
126
+ assert_equal(@r.c('A') , ['A'])
130
127
 
131
- assert_equal($r.c(1,'A',2), ['1','A','2'])
132
- assert_equal($r.c(:a => 1, :b => 'A', :c => 2), {'a' => '1', 'b' => 'A', 'c' => '2'})
133
- assert_equal($r.list(:a => 1, :b => 'A', :c => 2),
128
+ assert_equal(@r.c(1,'A',2), ['1','A','2'])
129
+ assert_equal(@r.c(:a => 1, :b => 'A', :c => 2), {'a' => '1', 'b' => 'A', 'c' => '2'})
130
+ assert_equal(@r.list(:a => 1, :b => 'A', :c => 2),
134
131
  {'a' => [1], 'b' => ['A'], 'c' => [2]})
135
- assert_equal($r.eval_R("x~y").class, RObj)
132
+ assert_equal(@r.eval_R("x~y").class, RObj)
136
133
  end
137
134
 
138
135
  def test_basic_conversion
139
136
 
140
137
  RSRuby.set_default_mode(RSRuby::BASIC_CONVERSION)
141
- assert_equal($r.c(true), true)
142
- assert_equal($r.c(4), 4)
143
- assert_equal($r.c('A') , 'A')
138
+ assert_equal(@r.c(true), true)
139
+ assert_equal(@r.c(4), 4)
140
+ assert_equal(@r.c('A') , 'A')
144
141
 
145
- assert_equal($r.c(1,'A',2), ['1','A','2'])
146
- assert_equal($r.c(:a => 1, :b => 'A', :c => 2), {'a' => '1', 'b' => 'A', 'c' => '2'})
147
- assert_equal($r.list(:a => 1, :b => 'A', :c => 2),
142
+ assert_equal(@r.c(1,'A',2), ['1','A','2'])
143
+ assert_equal(@r.c(:a => 1, :b => 'A', :c => 2), {'a' => '1', 'b' => 'A', 'c' => '2'})
144
+ assert_equal(@r.list(:a => 1, :b => 'A', :c => 2),
148
145
  {'a' => 1, 'b' => 'A', 'c' => 2})
149
- assert_equal($r.eval_R("x~y").class, RObj)
146
+ assert_equal(@r.eval_R("x~y").class, RObj)
150
147
 
151
148
  end
152
149
 
153
150
  def test_class_table
154
151
 
155
- $r.class_table['htest'] = lambda{'htest'}
156
- $r.class_table['data.frame'] = lambda{|x|
157
- if $r['[['].call(x,1).length > 2
152
+ @r.class_table['htest'] = lambda{'htest'}
153
+ @r.class_table['data.frame'] = lambda{|x|
154
+ if @r['[['].call(x,1).length > 2
158
155
  return 5
159
156
  else
160
157
  return 'bar'
161
158
  end
162
159
  }
163
160
  RSRuby.set_default_mode(RSRuby::CLASS_CONVERSION)
164
- assert_equal($r.t_test([1,2,3]), 'htest')
165
- assert_equal($r.as_data_frame([1,2,3]), 5)
166
- assert_equal($r.as_data_frame([1,2]), 'bar')
161
+ assert_equal(@r.t_test([1,2,3]), 'htest')
162
+ assert_equal(@r.as_data_frame([1,2,3]), 5)
163
+ assert_equal(@r.as_data_frame([1,2]), 'bar')
167
164
 
168
165
  end
169
166
 
170
167
  def test_multiple_class_table
171
168
 
172
169
  RSRuby.set_default_mode(RSRuby::NO_CONVERSION)
173
- f = $r.class__($r.c(4),'foo')
174
- g = $r.class__($r.c(4), ['bar','foo'])
170
+ f = @r.class__(@r.c(4),'foo')
171
+ g = @r.class__(@r.c(4), ['bar','foo'])
175
172
 
176
- $r.class_table['foo'] = lambda{'foo'}
177
- $r.class_table['bar'] = lambda{'bar'}
178
- $r.class_table[['bar','foo']] = lambda{5}
173
+ @r.class_table['foo'] = lambda{'foo'}
174
+ @r.class_table['bar'] = lambda{'bar'}
175
+ @r.class_table[['bar','foo']] = lambda{5}
179
176
 
180
177
  RSRuby.set_default_mode(RSRuby::CLASS_CONVERSION)
181
178
  assert_equal(f.to_ruby, 'foo')
182
179
  assert_equal(g.to_ruby, 5)
183
180
 
184
- $r.class_table.delete(['bar','foo'])
181
+ @r.class_table.delete(['bar','foo'])
185
182
  assert_equal(g.to_ruby, 'bar')
186
183
 
187
- $r.class_table.delete('bar')
184
+ @r.class_table.delete('bar')
188
185
  assert_equal(g.to_ruby, 'foo')
189
186
 
190
187
  end
@@ -192,7 +189,7 @@ class TestModes < Test::Unit::TestCase
192
189
  def test_proc_table
193
190
 
194
191
  t = lambda{|x|
195
- e = $r.attr(x,'names')
192
+ e = @r.attr(x,'names')
196
193
  return false if e.nil?
197
194
  if e == 'alternative' or e.include?('alternative')
198
195
  return true
@@ -200,12 +197,67 @@ class TestModes < Test::Unit::TestCase
200
197
  return false
201
198
  end
202
199
  }
203
- f = lambda{|x| $r['$'].call(x,'alternative')}
200
+ f = lambda{|x| @r['$'].call(x,'alternative')}
204
201
 
205
- $r.proc_table[t] = f
202
+ @r.proc_table[t] = f
206
203
  RSRuby.set_default_mode(RSRuby::NO_DEFAULT)
207
- $r.t_test.autoconvert(RSRuby::PROC_CONVERSION)
208
- assert_equal($r.t_test([1,2,3]), 'two.sided')
204
+ @r.t_test.autoconvert(RSRuby::PROC_CONVERSION)
205
+ assert_equal(@r.t_test([1,2,3]), 'two.sided')
206
+
207
+ end
208
+
209
+ def test_proc_convert
210
+
211
+ r = RSRuby.instance
212
+
213
+ check_str = lambda{|x| RSRuby.instance.is_character(x)}
214
+ f = lambda{|x|
215
+ x = x.to_ruby(RSRuby::BASIC_CONVERSION)
216
+ return "Cannot return 'foo'" if x == 'foo'
217
+ return x
218
+ }
219
+
220
+ r.proc_table[check_str] = f
221
+
222
+ RSRuby.set_default_mode(RSRuby::PROC_CONVERSION)
223
+
224
+ assert_equal('bar',r.c('bar'))
225
+ assert_equal("Cannot return 'foo'",r.c('foo'))
226
+ assert_equal(['bar','foo'],r.c('bar','foo'))
227
+
228
+ end
229
+
230
+ def test_restore_mode_after_exception_in_proc
231
+
232
+ r = RSRuby.instance
233
+
234
+ check_str = lambda{|x| RSRuby.instance.is_character(x)}
235
+ f = lambda{|x|
236
+ x.reverse
237
+ }
238
+
239
+ r.proc_table[check_str] = f
240
+
241
+ RSRuby.set_default_mode(RSRuby::PROC_CONVERSION)
242
+
243
+ assert_equal(6,r.sum(1,2,3))
244
+ assert_equal(RSRuby::PROC_CONVERSION,RSRuby.get_default_mode)
245
+ assert_raise(NoMethodError){r.paste("foo","bar")}
246
+ assert_raise(NoMethodError){r.paste("foo","bar")}
247
+ assert_equal(RSRuby::PROC_CONVERSION,RSRuby.get_default_mode)
248
+
249
+ end
250
+
251
+ def test_restore_mode_after_exception_in_class
252
+
253
+ r = RSRuby.instance
254
+ r.class_table['htest'] = lambda{|x| x.foo}
255
+
256
+ RSRuby.set_default_mode(RSRuby::CLASS_CONVERSION)
257
+ assert_equal(RSRuby::CLASS_CONVERSION,RSRuby.get_default_mode)
258
+ assert_raise(NoMethodError){r.t_test([1,2,3])}
259
+ assert_raise(NoMethodError){r.t_test([1,2,3])}
260
+ assert_equal(RSRuby::CLASS_CONVERSION,RSRuby.get_default_mode)
209
261
 
210
262
  end
211
263