alexgutteridge-rsruby 0.5

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.
data/test/tc_sigint.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'test/unit'
2
+ require 'rsruby'
3
+
4
+ class TestSigint < Test::Unit::TestCase
5
+
6
+ def test_sigint
7
+ assert_raises(KeyboardInterrupt){Process.kill('SIGINT',0)}
8
+ end
9
+
10
+ end
data/test/tc_to_r.rb ADDED
@@ -0,0 +1,145 @@
1
+ require 'test/unit'
2
+ require 'rsruby'
3
+
4
+ class Foo
5
+ end
6
+
7
+ class Bar
8
+ def initialize(x)
9
+ @x = x
10
+ end
11
+ def as_r
12
+ @x
13
+ end
14
+ end
15
+
16
+ class TestToR < Test::Unit::TestCase
17
+
18
+ def setup
19
+ @r = RSRuby.instance
20
+ #@r.gctorture(:on => true)
21
+ RSRuby.set_default_mode(RSRuby::NO_DEFAULT)
22
+ end
23
+
24
+ def test_robj_to_r
25
+
26
+ @r.c.autoconvert(RSRuby::NO_CONVERSION)
27
+
28
+ r1 = @r.c(4)
29
+ r2 = @r.c('foo')
30
+ r3 = @r.c(['a','b'])
31
+
32
+ assert(@r['=='].call(r1,4))
33
+ assert(@r['=='].call(r2,'foo'))
34
+ assert(@r['=='].call(r3,['a','b']))
35
+
36
+ assert_equal(@r.typeof(@r.eval),'closure')
37
+ assert_equal(@r.typeof(@r.eval(@r.eval)), 'closure')
38
+ assert_equal(@r.typeof(@r.eval([@r.eval,@r.eval])), 'list')
39
+
40
+ #Same tests as above in basic mode - should be identical
41
+ @r.c.autoconvert(RSRuby::BASIC_CONVERSION)
42
+
43
+ r1 = @r.c(4)
44
+ r2 = @r.c('foo')
45
+ r3 = @r.c(['a','b'])
46
+
47
+ assert(@r['=='].call(r1,4))
48
+ assert(@r['=='].call(r2,'foo'))
49
+ assert(@r['=='].call(r3,['a','b']))
50
+
51
+ assert_equal(@r.typeof(@r.eval),'closure')
52
+ assert_equal(@r.typeof(@r.eval(@r.eval)), 'closure')
53
+ assert_equal(@r.typeof(@r.eval([@r.eval,@r.eval])), 'list')
54
+
55
+ end
56
+
57
+ def test_empty_array_to_null
58
+ assert(@r.is_null([]))
59
+ end
60
+
61
+ def test_boolean_to_logical
62
+ assert_equal(@r.c(true),true)
63
+ assert_equal(@r.c(true).class,true.class)
64
+ assert_equal(@r.c(false),false)
65
+ assert_equal(@r.c(false).class,false.class)
66
+ end
67
+
68
+ def test_int_to_int
69
+ @r.c.autoconvert(RSRuby::NO_CONVERSION)
70
+ assert_equal(@r.typeof(@r.c(4)), 'integer')
71
+ @r.c.autoconvert(RSRuby::BASIC_CONVERSION)
72
+ assert_equal(@r.typeof(@r.c(4)), 'integer')
73
+ end
74
+
75
+ def test_float_to_float
76
+ @r.c.autoconvert(RSRuby::NO_CONVERSION)
77
+ assert_equal(@r.typeof(@r.c(4.5)), 'double')
78
+ @r.c.autoconvert(RSRuby::BASIC_CONVERSION)
79
+ assert_equal(@r.typeof(@r.c(4.5)), 'double')
80
+ end
81
+
82
+ def test_char_to_char
83
+ @r.c.autoconvert(RSRuby::NO_CONVERSION)
84
+ assert_equal(@r.typeof(@r.c('foo')), 'character')
85
+ @r.c.autoconvert(RSRuby::BASIC_CONVERSION)
86
+ assert_equal(@r.typeof(@r.c('foo')), 'character')
87
+ end
88
+
89
+ def test_hash_to_named_vector
90
+ @r.c.autoconvert(RSRuby::NO_CONVERSION)
91
+ assert_equal(@r.typeof(@r.c(:foo => 5, :bar => 7)),'integer')
92
+
93
+ assert(@r.attributes(@r.c(:foo => 5, :bar => 7))['names'].include?('foo'))
94
+ assert(@r.attributes(@r.c(:foo => 5, :bar => 7))['names'].include?('bar'))
95
+ #TODO - these fail because of the different calling semantics in
96
+ #RSRuby
97
+ #@r.c.autoconvert(RSRuby::BASIC_CONVERSION)
98
+ #assert_equal(@r.typeof(@r.c(:foo => 5, :bar => 7)),'integer')
99
+ #assert(@r.attributes(@r.c(:foo => 5, :bar => 7))['names'].include?('foo'))
100
+ #assert(@r.attributes(@r.c(:foo => 5, :bar => 7))['names'].include?('bar'))
101
+ end
102
+
103
+ def test_array_to_vector
104
+ @r.c.autoconvert(RSRuby::NO_CONVERSION)
105
+ assert_equal(@r.length(@r.c(1,2,3,4)),4)
106
+ @r.c.autoconvert(RSRuby::BASIC_CONVERSION)
107
+ assert_equal(@r.length(@r.c(1,2,3,4)),4)
108
+ end
109
+
110
+ def test_not_convertible
111
+ #TODO - range?
112
+ assert_raises(ArgumentError){@r.c(1..10)}
113
+ end
114
+
115
+ def test_instances_not_convertible
116
+ foo = Foo.new
117
+ assert_raises(ArgumentError){@r.c(foo)}
118
+ end
119
+
120
+ def test_as_r_method
121
+ @r.c.autoconvert(RSRuby::BASIC_CONVERSION)
122
+
123
+ a = Bar.new(3)
124
+ b = Bar.new('foo')
125
+ d = Bar.new(@r.seq)
126
+
127
+ assert_equal(@r.c(a),3)
128
+ assert_equal(@r.c(b),'foo')
129
+ assert_equal(@r.c(d).call(1,3),[1,2,3])
130
+
131
+ end
132
+
133
+ def test_max_int_to_r
134
+ #TODO
135
+ end
136
+
137
+ def test_inf_to_r
138
+ #TODO
139
+ end
140
+
141
+ def test_NaN_to_r
142
+ #TODO
143
+ end
144
+
145
+ end
@@ -0,0 +1,153 @@
1
+ require 'test/unit'
2
+ require 'rsruby'
3
+
4
+ class TestToRuby < Test::Unit::TestCase
5
+ @@test_dir = File.expand_path File.dirname(__FILE__)
6
+
7
+ def setup
8
+ @r = RSRuby.instance
9
+ RSRuby.set_default_mode(RSRuby::NO_DEFAULT)
10
+ @r.seq.autoconvert(RSRuby::BASIC_CONVERSION)
11
+ @r.c.autoconvert(RSRuby::BASIC_CONVERSION)
12
+ end
13
+
14
+ def test_null_to_nil
15
+ assert(@r.attributes(@r.seq).nil?)
16
+ end
17
+
18
+ def test_factor_to_list
19
+ assert_equal(@r.factor([1,2,3,4]), ['1','2','3','4'])
20
+ assert_equal(@r.factor([1,1,1,2]), ['1','1','1','2'])
21
+ assert_equal(@r.factor(['a','b','c']), ['a','b','c'])
22
+ end
23
+
24
+ def test_NA_int
25
+ #The formula on right should equal smallest Fixnum
26
+ #in current Ruby build (can vary)
27
+ assert_equal(@r.NA, (-1)*(2**((1.size*8)-1)))
28
+ assert(@r.is_na(@r.NA))
29
+ end
30
+
31
+ def test_NA_string
32
+ assert_equal(@r.eval_R('as.character(NA)'), 'NA')
33
+ assert_equal(@r.as_character(@r.NA), 'NA')
34
+ assert_equal(@r.as_character(@r.NaN), 'NaN')
35
+ end
36
+
37
+ def test_factor_NA
38
+ assert_equal(@r.factor(@r.NA) , 'NA')
39
+ assert_equal(@r.factor(@r.NaN), 'NaN')
40
+ assert_equal(@r.factor(@r.as_character('NA')), 'NA')
41
+
42
+ xi = [1,2,@r.NA,@r.NaN,4]
43
+ assert_equal(@r.factor(xi), ['1','2','NA','NaN','4'])
44
+
45
+ xd = [1.01,2.02,@r.NA,@r.NaN,4.04]
46
+ assert_equal(@r.factor(xd), ['1.01','2.02','NA','NaN','4.04'])
47
+
48
+ end
49
+
50
+ def test_NA_list
51
+
52
+ #TODO - RPy has commented out these tests as well.
53
+ #The conversion between NA/NaN and Ruby seems a little confused
54
+ #at the moment
55
+
56
+ xi = [1,2,@r.NA,@r.NaN,4]
57
+ assert_equal(@r.as_character(xi), ['1','2','NA','NaN','4'])
58
+ #assert_equal(@r.as_numeric(xi) , [1.0,2.0,@r.NA,@r.NaN,4.0])
59
+ #assert_equal(@r.as_integer(xi) , [1,2,@r.NA,@r.NaN,4])
60
+ assert_equal(@r.factor(xi) , ['1','2','NA','NaN','4'])
61
+ assert_equal(@r.is_na(xi) , [false, false, true, true, false])
62
+
63
+ xd = [1.01,2.02,@r.NA,@r.NaN,4.04]
64
+ assert_equal(@r.as_character(xd), ['1.01','2.02','NA','NaN','4.04'])
65
+ #assert_equal(@r.as_numeric(xi) , [1.01,2.01,@r.NA,@r.NaN,4.01])
66
+ assert_equal(@r.as_integer(xd) , [1,2,@r.NA,@r.NA,4])
67
+ assert_equal(@r.factor(xd) , ['1.01','2.02','NA','NaN','4.04'])
68
+ assert_equal(@r.is_na(xd) , [false, false, true, true, false])
69
+ end
70
+
71
+ #TODO - table.txt?????????
72
+ def test_dataframe_to_list
73
+ @r.read_table.autoconvert(RSRuby::BASIC_CONVERSION)
74
+ assert_equal(@r.read_table(@@test_dir+"/table.txt", {:header => 1}),
75
+ {
76
+ 'A' => ['X1','X2','X3'],
77
+ 'C' => [5,8,2],
78
+ 'B' => [4.0,7.0,6.0],
79
+ 'D' => ['6','9','Foo']
80
+ })
81
+ end
82
+
83
+ def test_logical_to_boolean
84
+ assert_equal(@r.TRUE, true)
85
+ assert_equal(@r.T, true)
86
+ assert_equal(@r.FALSE, false)
87
+ assert_equal(@r.F, false)
88
+ end
89
+
90
+ def test_int_to_int
91
+ assert_equal(@r.as_integer(5),5)
92
+ assert_equal(@r.as_integer(-3),-3)
93
+ end
94
+
95
+ def test_float_to_float
96
+ assert_equal(@r.as_real(5),5.0)
97
+ assert_equal(@r.as_real(3.1),3.1)
98
+ assert_equal(@r.as_real(-3.1), -3.1)
99
+ end
100
+
101
+ def test_complex_to_complex
102
+ #TODO - Think about Complex support
103
+ assert_equal(@r.as_complex(Complex(1,2)), Complex(1,2))
104
+ assert_equal(@r.as_complex(Complex(1.5,-3.4)), Complex(1.5,-3.4))
105
+ end
106
+
107
+ def test_str_to_str
108
+ @r.as_data_frame.autoconvert(RSRuby::NO_CONVERSION)
109
+ assert_equal(@r.class_(@r.as_data_frame([1,2,3])), 'data.frame')
110
+ end
111
+
112
+ def test_vector_length_one
113
+ assert_equal(@r.c(1),1)
114
+ assert_equal(@r.c('foo'),'foo')
115
+ end
116
+
117
+ def test_int_vector_to_array
118
+ assert_equal(@r.seq(10),[1,2,3,4,5,6,7,8,9,10])
119
+ end
120
+
121
+ def test_float_vector_to_array
122
+ assert_equal(@r.seq(1,2,{:by => 0.5}), [1.0,1.5,2.0])
123
+ end
124
+
125
+ def test_complex_vector_to_array
126
+ assert_equal(@r.c(Complex(1,2),Complex(2,-3)),[Complex(1,2),Complex(2,-3)])
127
+ end
128
+
129
+ def test_str_vector_to_array
130
+ assert_equal(@r.c('Foo','Bar'),['Foo','Bar'])
131
+ end
132
+
133
+ def test_list_to_array
134
+ @r.list.autoconvert(RSRuby::BASIC_CONVERSION)
135
+ assert_equal(@r.list(1,2.0,'foo'),[1,2.0,'foo'])
136
+ end
137
+
138
+ def test_named_vector_to_hash
139
+ @r.c.autoconvert(RSRuby::NO_CONVERSION)
140
+ a = @r.attr__(@r.c(1,2,3), 'names', ['foo','bar','baz'])
141
+ assert_equal(a,{'foo'=>1,'bar'=>2,'baz'=>3})
142
+ assert_equal(@r.list(:foo => 1, :bar => 2, :baz => 3),{'foo'=>1,'bar'=>2,'baz'=>3})
143
+ end
144
+
145
+ def test_vector_coercion
146
+ @r.c.autoconvert(RSRuby::NO_CONVERSION)
147
+ assert_equal(@r.typeof(@r.c(1,2,3)), 'integer')
148
+ assert_equal(@r.typeof(@r.c(1,2.0,3)), 'double')
149
+ assert_equal(@r.typeof(@r.c(1,Complex(2,3),3)), 'complex')
150
+ assert_equal(@r.typeof(@r.c(1,'foo',3)), 'character')
151
+ end
152
+
153
+ end
data/test/tc_util.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'test/unit'
2
+ require 'rsruby'
3
+
4
+ class TestUtil < Test::Unit::TestCase
5
+
6
+ def test_as_list
7
+ #TODO
8
+ #assert_equal($r.as_lis
9
+ end
10
+
11
+ def test_with_mode
12
+ #TODO
13
+ end
14
+
15
+ def test_r_code
16
+ #TODO - Not sure about this. I use r.eval_R('')
17
+ #Not really a util
18
+ end
19
+ end
data/test/tc_vars.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'test/unit'
2
+ require 'rsruby'
3
+
4
+ class TestVars < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @r = RSRuby.instance
8
+ end
9
+
10
+ def test_get_vars
11
+ @r.eval_R("f<-function(x) x+1")
12
+ @r.assign('x',100)
13
+ @r.assign('v',(1..10).to_a)
14
+ #There is a difference here between RPy and us
15
+ #a final hash argument is treated as named arguments
16
+ #to the original function call (assign) not as a list
17
+ #to be given to the function
18
+ @r.c.autoconvert(RSRuby::NO_CONVERSION)
19
+ @r.assign('d',@r.c({'a' => 1, 'b' => 2}))
20
+ @r.c.autoconvert(RSRuby::BASIC_CONVERSION)
21
+
22
+ assert_equal(@r.x, 100)
23
+ assert_equal(@r.v, (1..10).to_a)
24
+ assert_equal(@r.d, @r.c({'a' => 1, 'b' => 2}))
25
+ assert_equal(@r.f.class, @r.c.class)
26
+ end
27
+
28
+ end
data/test/test_all.rb ADDED
@@ -0,0 +1,22 @@
1
+ #Tests all test cases for rsruby
2
+
3
+ require 'test/unit'
4
+
5
+ require 'tc_array'
6
+ require 'tc_boolean'
7
+ require 'tc_cleanup'
8
+ require 'tc_eval'
9
+ require 'tc_extensions'
10
+ require 'tc_init'
11
+ #require 'tc_io'
12
+ require 'tc_library'
13
+ require 'tc_matrix'
14
+ require 'tc_modes'
15
+ require 'tc_robj'
16
+ #require 'tc_sigint'
17
+ require 'tc_to_ruby'
18
+ require 'tc_to_r'
19
+ require 'tc_util'
20
+ require 'tc_vars'
21
+
22
+
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alexgutteridge-rsruby
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.5"
5
+ platform: ruby
6
+ authors:
7
+ - Alex Gutteridge
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-19 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: RSRuby is a bridge library for Ruby giving Ruby developers access to the full R statistical programming environment. RSRuby embeds a full R interpreter inside the running Ruby script, allowing R methods to be called and data passed between the Ruby script and the R interpreter. Most data conversion is handled automatically, but user-definable conversion routines can also be written to handle any R or Ruby class.
17
+ email: ag357@cam.ac.uk
18
+ executables: []
19
+
20
+ extensions:
21
+ - ext/extconf.rb
22
+ extra_rdoc_files:
23
+ - README.txt
24
+ - History.txt
25
+ - License.txt
26
+ - examples/bioc.rb
27
+ - examples/dataframe.rb
28
+ - examples/arrayfields.rb
29
+ - examples/erobj.rb
30
+ files:
31
+ - History.txt
32
+ - License.txt
33
+ - Manifest.txt
34
+ - README.txt
35
+ - Rakefile.rb
36
+ - examples/arrayfields.rb
37
+ - examples/bioc.rb
38
+ - examples/dataframe.rb
39
+ - examples/erobj.rb
40
+ - ext/Converters.c
41
+ - ext/Converters.h
42
+ - ext/R_eval.c
43
+ - ext/R_eval.h
44
+ - ext/extconf.rb
45
+ - ext/robj.c
46
+ - ext/rsruby.c
47
+ - ext/rsruby.h
48
+ - lib/rsruby.rb
49
+ - lib/rsruby/dataframe.rb
50
+ - lib/rsruby/erobj.rb
51
+ - lib/rsruby/robj.rb
52
+ - test/table.txt
53
+ - test/tc_array.rb
54
+ - test/tc_boolean.rb
55
+ - test/tc_cleanup.rb
56
+ - test/tc_eval.rb
57
+ - test/tc_extensions.rb
58
+ - test/tc_init.rb
59
+ - test/tc_io.rb
60
+ - test/tc_library.rb
61
+ - test/tc_matrix.rb
62
+ - test/tc_modes.rb
63
+ - test/tc_robj.rb
64
+ - test/tc_sigint.rb
65
+ - test/tc_to_r.rb
66
+ - test/tc_to_ruby.rb
67
+ - test/tc_util.rb
68
+ - test/tc_vars.rb
69
+ - test/test_all.rb
70
+ has_rdoc: true
71
+ homepage: http://web.kuicr.kyoto-u.ac.jp/~alexg/rsruby/
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --exclude
75
+ - test/*
76
+ - --main
77
+ - README.txt
78
+ - --inline-source
79
+ require_paths:
80
+ - lib
81
+ - test
82
+ - ext
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ version:
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ version:
95
+ requirements: []
96
+
97
+ rubyforge_project: rsruby
98
+ rubygems_version: 1.2.0
99
+ signing_key:
100
+ specification_version: 2
101
+ summary: RSRuby is a bridge library for Ruby giving Ruby developers access to the full R statistical programming environment. RSRuby embeds a full R interpreter inside the running Ruby script, allowing R methods to be called and data passed between the Ruby script and the R interpreter. Most data conversion is handled automatically, but user-definable conversion routines can also be written to handle any R or Ruby class.
102
+ test_files:
103
+ - test/test_all.rb