r_type 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.travis.yml +14 -0
  4. data/Gemfile +3 -0
  5. data/Guardfile +9 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +131 -0
  8. data/Rakefile +9 -0
  9. data/bin/r_console +17 -0
  10. data/lib/r_type.rb +41 -0
  11. data/lib/r_type/convert.rb +28 -0
  12. data/lib/r_type/core_ext.rb +30 -0
  13. data/lib/r_type/core_ext/boolean_delegate_r.rb +8 -0
  14. data/lib/r_type/core_ext/delegate_checker.rb +11 -0
  15. data/lib/r_type/core_ext/numeric_delegate_r.rb +20 -0
  16. data/lib/r_type/helper.rb +4 -0
  17. data/lib/r_type/helper/matrix_multiply.rb +13 -0
  18. data/lib/r_type/helper/robj_delegatable.rb +78 -0
  19. data/lib/r_type/helper/robj_delegatable_class_methods.rb +35 -0
  20. data/lib/r_type/helper/ruby_compareable.rb +9 -0
  21. data/lib/r_type/r.rb +50 -0
  22. data/lib/r_type/type/array.rb +7 -0
  23. data/lib/r_type/type/base.rb +14 -0
  24. data/lib/r_type/type/data_frame.rb +7 -0
  25. data/lib/r_type/type/function.rb +8 -0
  26. data/lib/r_type/type/integer.rb +7 -0
  27. data/lib/r_type/type/list.rb +7 -0
  28. data/lib/r_type/type/matrix.rb +65 -0
  29. data/lib/r_type/type/numeric.rb +7 -0
  30. data/lib/r_type/type/string.rb +7 -0
  31. data/lib/r_type/type/vector.rb +10 -0
  32. data/lib/r_type/version.rb +3 -0
  33. data/r_type.gemspec +28 -0
  34. data/sample/sample1.rb +34 -0
  35. data/sample/sample2.rb +38 -0
  36. data/sample/sample3.rb +79 -0
  37. data/sample/sample4.rb +43 -0
  38. data/sample/sample5.rb +61 -0
  39. data/spec/convert_spec.rb +93 -0
  40. data/spec/core_ext/boolean_delegate_r_spec.rb +39 -0
  41. data/spec/core_ext/numeric_delegate_r_spec.rb +27 -0
  42. data/spec/helper/robj_delegatable_spec.rb +68 -0
  43. data/spec/r_spec.rb +54 -0
  44. data/spec/spec_helper.rb +34 -0
  45. data/spec/type/base_spec.rb +52 -0
  46. data/spec/type/matrix_spec.rb +157 -0
  47. metadata +196 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8b9fb16ae61ba00a4a815f6989ad52d54c53575b
4
+ data.tar.gz: 08b5f71abdb66b91646f7e7e45ba17bcd0dd6f24
5
+ SHA512:
6
+ metadata.gz: 5c53f982491c53dc282091dd5164c1130ddd7e712b9acf6ed27f0a97319086e7f3fe6dd4212c2b80859283d30353b2fe237e70e6a3ac1aae0a40a692041d35f1
7
+ data.tar.gz: fc1a2cf86d1dae52c686c2870c6510f96a4e02b6580ae591308e1c2ea06527218e116e5ddac1929f73895f66d722666c17c22febd5c1e5a189053a6efccc3f0e
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ env:
2
+ - R_HOME=/usr/lib/R
3
+ rvm:
4
+ - 2.0.0
5
+ gemfile:
6
+ - Gemfile
7
+ branches:
8
+ only:
9
+ - master
10
+ before_install:
11
+ - sudo apt-add-repository -y ppa:marutter/rdev
12
+ - sudo apt-get update -qq
13
+ - sudo apt-get install -qq r-base-dev
14
+ - bundle config build.rsruby --with-R-include=/usr/share/R/include
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ guard 'rspec' do
2
+ watch %r{^spec/(.+_spec\.rb)$} do |m|
3
+ "spec/#{m[1]}"
4
+ end
5
+
6
+ watch %r{^lib/r_type/(.+)\.rb$} do |m|
7
+ "spec/#{m[1]}_spec.rb"
8
+ end
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tatsuya Takamura
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,131 @@
1
+ # RType [![Build Status](https://travis-ci.org/ttakamura/robject.png?branch=master)](https://travis-ci.org/ttakamura/robject) [![Dependency Status](https://gemnasium.com/ttakamura/r_type.png)](https://gemnasium.com/ttakamura/r_type)
2
+
3
+ ![r.type image](https://dl.dropboxusercontent.com/u/3111/rtype_1.png)
4
+
5
+ RType extend RSRuby to be rubyist can use R in a more Ruby way.
6
+
7
+ RSRuby provide to access R functions and variables from Ruby and basic class conversion.
8
+ And RType provide extended class conversion that convert R class to appropriate Ruby class that has many convenient methods.
9
+
10
+ ## Convert R class to RType
11
+
12
+ * `matrix` => `RType::Matrix`
13
+ * `numeric` => `RType::Numeric`
14
+ * `vector` => `RType::Vector`
15
+ * `list` => `RType::List`
16
+ * `array` => `RType::Array`
17
+ * `data.frame` => `RType::DataFrame`
18
+ * `character` => `RType::String`
19
+ * other => `RType::Base`
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ gem 'r_type'
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install r_type
34
+
35
+ ## Usage
36
+
37
+ ### Interactive Console
38
+
39
+ Execute `r_console` to open interactive RType console.
40
+
41
+ ```sh
42
+ r_console
43
+
44
+ > m = matrix (1..9).to_a, ncol: 3
45
+ => [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
46
+
47
+ > print m * m
48
+ [,1] [,2] [,3]
49
+ [1,] 30 66 102
50
+ [2,] 36 81 126
51
+ [3,] 42 96 150
52
+ ```
53
+
54
+ ### Use RType in your Ruby Class
55
+
56
+ * Simple script
57
+
58
+ ```ruby
59
+ a = RType::Matrix.new [1,2,3, 4,5,6], ncol: 2
60
+ b = RType::Matrix.new [10, 100], ncol: 1
61
+
62
+ RType::R.print a * b
63
+
64
+ # [,1]
65
+ # [1,] 410
66
+ # [2,] 520
67
+ # [3,] 630
68
+ ```
69
+
70
+ When you haven't define `::R` constant in your code, RType define `::R` as alias of `RType::R`. So, you can write following code.
71
+
72
+ ```ruby
73
+ a = R::Matrix.new [1,2,3, 4,5,6], ncol: 2
74
+ b = R::Matrix.new [10, 100], ncol: 1
75
+
76
+ R.print a * b
77
+ ```
78
+
79
+
80
+ * In `RType::R` context
81
+
82
+ `R.run` call given block in `RType::R` context.
83
+
84
+ ```ruby
85
+ R.run do
86
+ a = matrix [1,2,3, 4,5,6], ncol: 2
87
+ b = matrix [10, 100], ncol: 1
88
+ print a * b
89
+ end
90
+
91
+ # [,1]
92
+ # [1,] 410
93
+ # [2,] 520
94
+ # [3,] 630
95
+ ```
96
+
97
+
98
+ * Assign a variable from Ruby and use it in R
99
+
100
+ Call `RType::R.eval_R` with a string that has R code.
101
+
102
+ ```ruby
103
+ # assign a value from Ruby
104
+ R.hoge = [1,2,3,4,5]
105
+
106
+ # eval R code
107
+ R.eval_R <<RTEXT
108
+ f = function(x) x * 100
109
+ result = sapply(hoge, f)
110
+ print(result)
111
+ RTEXT
112
+ ```
113
+
114
+ Or, you can use inline eval_R when `RType::R` context.
115
+
116
+ ```ruby
117
+ R.run do
118
+ hoge = [1,2,3,4,5]
119
+ result = sapply hoge, `function(x) x * 100`
120
+ print result
121
+ end
122
+ ```
123
+
124
+
125
+ ## Contributing
126
+
127
+ 1. Fork it
128
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
129
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
130
+ 4. Push to the branch (`git push origin my-new-feature`)
131
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => [:spec]
4
+
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = 'spec/**/*_spec.rb'
8
+ spec.rspec_opts = ['-cfs']
9
+ end
data/bin/r_console ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path('../../lib/r_type', __FILE__)
3
+
4
+ begin
5
+ require 'pry'
6
+ rescue LoadError => e
7
+ puts "Can't find the Pry gem!"
8
+ puts "Please execute 'gem install pry' to use r_console"
9
+ exit 1
10
+ end
11
+
12
+ RType::R.run do
13
+ Pry.start binding, print: ->(output, value) do
14
+ value.to_ruby if value.respond_to?(:to_ruby)
15
+ output.puts "=> #{value.inspect}"
16
+ end
17
+ end
data/lib/r_type.rb ADDED
@@ -0,0 +1,41 @@
1
+ require 'rsruby'
2
+ require 'delegate'
3
+ require 'matrix'
4
+ require File.expand_path('../r_type/version', __FILE__)
5
+ require File.expand_path('../r_type/helper', __FILE__)
6
+ require File.expand_path('../r_type/helper/ruby_compareable', __FILE__)
7
+ require File.expand_path('../r_type/helper/matrix_multiply', __FILE__)
8
+ require File.expand_path('../r_type/helper/robj_delegatable_class_methods', __FILE__)
9
+ require File.expand_path('../r_type/core_ext/delegate_checker', __FILE__)
10
+ require File.expand_path('../r_type/core_ext/numeric_delegate_r', __FILE__)
11
+ require File.expand_path('../r_type/core_ext/boolean_delegate_r', __FILE__)
12
+ require File.expand_path('../r_type/helper/robj_delegatable', __FILE__)
13
+ require File.expand_path('../r_type/r', __FILE__)
14
+ require File.expand_path('../r_type/type/base', __FILE__)
15
+ require File.expand_path('../r_type/convert', __FILE__)
16
+ require File.expand_path('../r_type/type/matrix', __FILE__)
17
+ require File.expand_path('../r_type/type/vector', __FILE__)
18
+ require File.expand_path('../r_type/type/numeric', __FILE__)
19
+ require File.expand_path('../r_type/type/integer', __FILE__)
20
+ require File.expand_path('../r_type/type/string', __FILE__)
21
+ require File.expand_path('../r_type/type/list', __FILE__)
22
+ require File.expand_path('../r_type/type/array', __FILE__)
23
+ require File.expand_path('../r_type/type/data_frame', __FILE__)
24
+ require File.expand_path('../r_type/type/function', __FILE__)
25
+ require File.expand_path('../r_type/core_ext', __FILE__)
26
+
27
+ module RType
28
+ end
29
+
30
+ unless defined?(::R)
31
+ ::R = RType::R
32
+ ::R::Array = RType::Array
33
+ ::R::List = RType::List
34
+ ::R::Integer = RType::Integer
35
+ ::R::Function = RType::Function
36
+ ::R::DataFrame = RType::DataFrame
37
+ ::R::Matrix = RType::Matrix
38
+ ::R::Numeric = RType::Numeric
39
+ ::R::String = RType::String
40
+ ::R::Vector = RType::Vector
41
+ end
@@ -0,0 +1,28 @@
1
+ module RType
2
+ class Convert
3
+ class NotSupportTypeError < Exception; end
4
+
5
+ def self.call_R_without_convert func, *args
6
+ original_mode = RSRuby.get_default_mode
7
+ RSRuby.set_default_mode RSRuby::BASIC_CONVERSION
8
+ result = R.rsruby[func].call(*args)
9
+ ensure
10
+ RSRuby.set_default_mode original_mode
11
+ result
12
+ end
13
+
14
+ def convert robj
15
+ type = check_R_type robj
16
+ [Function, DataFrame, List, Array, Matrix, String, Vector, Integer, Numeric, Base].each do |klass|
17
+ if klass.match? robj, type
18
+ break klass.new robj
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+ def check_R_type robj
25
+ Convert.call_R_without_convert :class, robj
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,30 @@
1
+ Fixnum.class_eval do
2
+ prepend RType::CoreExt::NumericDelegateR
3
+ prepend RType::CoreExt::BooleanDelegateR
4
+ prepend RType::CoreExt::DelegateChecker
5
+ end
6
+
7
+ Bignum.class_eval do
8
+ prepend RType::CoreExt::NumericDelegateR
9
+ prepend RType::CoreExt::BooleanDelegateR
10
+ prepend RType::CoreExt::DelegateChecker
11
+ end
12
+
13
+ Float.class_eval do
14
+ prepend RType::CoreExt::NumericDelegateR
15
+ prepend RType::CoreExt::BooleanDelegateR
16
+ prepend RType::CoreExt::DelegateChecker
17
+ end
18
+
19
+ Array.class_eval do
20
+ prepend RType::CoreExt::NumericDelegateR
21
+ prepend RType::CoreExt::BooleanDelegateR
22
+ prepend RType::CoreExt::DelegateChecker
23
+ include RType::Helper::MatrixMultiply
24
+ end
25
+
26
+ ::Matrix.class_eval do
27
+ def as_r
28
+ ::RType::R.rbind(*self.to_a)
29
+ end
30
+ end
@@ -0,0 +1,8 @@
1
+ module RType::CoreExt
2
+ module BooleanDelegateR
3
+ extend RType::Helper::RObjDelegatable::ClassMethods
4
+
5
+ delegate_to_R '=~' => '=='
6
+ delegate_to_R '>', '<', '>=', '<=', '|', '&'
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ module RType::CoreExt
2
+ module DelegateChecker
3
+ def is_robj_matrix? val
4
+ val.is_a?(RType::Vector) || val.is_a?(RType::Matrix)
5
+ end
6
+
7
+ def can_delegate_to_R? *args
8
+ is_robj_matrix? args.first
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ module RType::CoreExt
2
+ module NumericDelegateR
3
+ extend RType::Helper::RObjDelegatable::ClassMethods
4
+
5
+ delegate_to_R '+', '-', '/', '**', '^'
6
+
7
+ def * val
8
+ if is_robj_matrix?(val)
9
+ if val.respond_to?(:is_robj_matrix_multiply?) && val.is_robj_matrix_multiply? &&
10
+ self.respond_to?(:is_robj_matrix_multiply?) && self.is_robj_matrix_multiply?
11
+ R['%*%'].call(self, val)
12
+ else
13
+ R[:*].call(self, val)
14
+ end
15
+ else
16
+ super
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ module RType
2
+ module Helper
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ module RType::Helper::MatrixMultiply
2
+ def * val
3
+ if val.respond_to?(:is_robj_matrix_multiply?) && val.is_robj_matrix_multiply?
4
+ R['%*%'].call self, val
5
+ else
6
+ super
7
+ end
8
+ end
9
+
10
+ def is_robj_matrix_multiply?
11
+ true
12
+ end
13
+ end
@@ -0,0 +1,78 @@
1
+ module RType
2
+ module Helper
3
+ module RObjDelegatable
4
+ extend ClassMethods
5
+ include RType::CoreExt::NumericDelegateR
6
+ include RType::CoreExt::BooleanDelegateR
7
+
8
+ delegate_to_R '*'
9
+
10
+ def self.included(klass)
11
+ klass.extend ClassMethods
12
+ end
13
+
14
+ def __getobj__
15
+ to_ruby
16
+ end
17
+
18
+ def robj
19
+ @robj ||= convert_ruby_to_robj.tap do |newobj|
20
+ @ruby_obj = nil unless newobj.nil?
21
+ end
22
+ end
23
+
24
+ def ruby_obj
25
+ @ruby_obj ||= convert_robj_to_ruby.tap do |newobj|
26
+ @robj = nil unless newobj.nil?
27
+ end
28
+ end
29
+
30
+ def robj= obj
31
+ @ruby_obj = nil
32
+ @robj = obj
33
+ end
34
+
35
+ def ruby_obj= obj
36
+ @robj = nil
37
+ @ruby_obj = obj
38
+ end
39
+
40
+ def as_r
41
+ robj.as_r
42
+ end
43
+
44
+ def to_ruby mode = nil
45
+ ruby_obj
46
+ end
47
+
48
+ def inspect
49
+ "RType::#{(@robj || @ruby_obj).inspect}"
50
+ end
51
+
52
+ def == obj
53
+ if obj.is_a?(RObjDelegatable)
54
+ self.robj == obj.robj || !!R['identical'].call(self, obj)
55
+ else
56
+ super
57
+ end
58
+ end
59
+
60
+ private
61
+ def convert_robj_to_ruby
62
+ @robj.to_ruby(::RSRuby::BASIC_CONVERSION)
63
+ end
64
+
65
+ def convert_ruby_to_robj
66
+ if @ruby_obj.respond_to?(:as_r)
67
+ @ruby_obj.as_r.robj
68
+ else
69
+ R.assign('_tmp_var_for_rtype', @ruby_obj).robj
70
+ end
71
+ end
72
+
73
+ def can_delegate_to_R? *args
74
+ true
75
+ end
76
+ end
77
+ end
78
+ end