R4rb 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,133 @@
1
+ # encoding: utf-8
2
+ require "mkmf"
3
+ require 'fileutils' #if RUBY_VERSION < "1.9"
4
+
5
+ # TODO: specify a file R4rb.conf to provide multiple R systems location
6
+ # in lib/R4rb.rb read this file to offer the several choices
7
+
8
+ def find_installed_R
9
+
10
+ if RUBY_PLATFORM=~/mingw/ or RUBY_PLATFORM=~/msys/
11
+ $prefix=`R RHOME`.gsub("\\","/")
12
+ $prefix_include=$prefix+"/include"
13
+ $prefix_lib=nil
14
+ (RUBY_PLATFORM=~/64/ ? ["","x64"] : ["","i386"]).each do |arch|
15
+ $prefix_lib=File.join($prefix,"bin",arch) if File.exists? File.join($prefix,"bin",arch,"R.dll")
16
+ #$versions=[arch]
17
+ break if $prefix_lib
18
+ end
19
+ elsif RUBY_PLATFORM=~/darwin/
20
+ versions="/Library/Frameworks/R.framework/Versions"
21
+ if File.directory? versions
22
+ $prefix=Dir[versions+"/*/Resources"].select{|e| e.split("/")[-2]!="Current"}
23
+ $prefix_include=$prefix.map{|e| e+"/include"}
24
+ $prefix_lib=$prefix.map{|e| e+"/lib"}
25
+ $versions=$prefix.map{|e| e.split("/")[-2]}
26
+ else
27
+ $prefix=`R RHOME`.strip
28
+ $prefix_include=$prefix+"/include"
29
+ $prefix_lib=$prefix+"/lib"
30
+ end
31
+
32
+ else
33
+ stddirs=["/usr/local/lib/R","/usr/lib/R","/usr/share/R","/usr/include/R","/usr/lib64/R"]
34
+ stddirs.unshift `R RHOME`.strip.split("\n").select{|l| l=~/^\//}[0] unless `R RHOME`.empty?
35
+ usrdirs = []
36
+ ARGV.each do |arg|
37
+ if arg =~ /--with-R/
38
+ option, value = arg.split('=')
39
+ usrdirs = [ value ] + usrdirs
40
+ end
41
+ end
42
+ dirs = usrdirs + stddirs
43
+ dirs.uniq! # remove duplicates
44
+
45
+ $prefix,$prefix_include,$prefix_lib=nil,nil,nil
46
+
47
+ dirs.each do |dir|
48
+ p dir
49
+ if !$prefix and FileTest.exists?(dir)
50
+ $prefix = dir[0..-3]
51
+ end
52
+
53
+ if !$prefix_include and FileTest.exists?(dir+"/include/R.h")
54
+ $prefix_include=dir+"/include"
55
+ end
56
+
57
+ if !$prefix_include and FileTest.exists?(dir+"/R.h")
58
+ $prefix_include=dir
59
+ end
60
+
61
+ if !$prefix_lib and FileTest.exists?(dir+"/lib/libR.so")
62
+ $prefix_lib=dir+"/lib"
63
+ end
64
+ end
65
+ raise RuntimeError, "couldn't find R Home : R seems to be uninstalled!!" unless $prefix
66
+ raise RuntimeError, "couldn't find R.h!!" unless $prefix_include
67
+ raise RuntimeError, "couldn't find libR.so!!" unless $prefix_lib
68
+ #return multi
69
+ end
70
+
71
+ end
72
+
73
+
74
+ def r4rb_makefile(inc,lib,version=nil)
75
+ $CFLAGS = "-I"+inc+" -I."
76
+ $LDFLAGS = "-L"+lib if lib
77
+ $libs = "-lR"
78
+
79
+ header = nil
80
+
81
+ rb4r_name="R4rb"+((version and version!="orig") ? "."+version : "" )
82
+ $objs = [rb4r_name+".o"]
83
+
84
+ dir_config("R4rb")
85
+ create_makefile(rb4r_name)
86
+ File.rename("Makefile", "Makefile-#{version}")
87
+ end
88
+
89
+ ## R is installed?
90
+ find_installed_R
91
+
92
+ $versions=["orig"] unless $versions
93
+ $prefix,$prefix_include,$prefix_lib=[$prefix],[$prefix_include],[$prefix_lib] unless $prefix.is_a? Array
94
+
95
+ modules = ""
96
+
97
+ p $versions
98
+
99
+ File.unlink("Makefile") if (FileTest.exist? "Makefile")
100
+ $versions.each_with_index {|version,i|
101
+ File.unlink("Makefile-#{version}") if (FileTest.exist? "Makefile-#{version}")
102
+ r4rb_makefile($prefix_include[i],$prefix_lib[i],version)
103
+ rb4r_name="R4rb"+((version and version!="orig") ? "."+version : "" )
104
+ modules += " #{rb4r_name}.#{CONFIG['DLEXT']}"
105
+ FileUtils.cp "R4rb.c", "#{rb4r_name}.c" if "R4rb.c" != "#{rb4r_name}.c"
106
+ }
107
+
108
+ open("Makefile", "w") {|f|
109
+ v = $nmake ? '{$(srcdir)}' : ''
110
+ f << "SHELL = /bin/sh" + "\n"
111
+ f << "srcdir = #{$srcdir}" + "\n"
112
+ f << "VPATH = $(srcdir)" + "\n"
113
+
114
+ f << "all: #{modules}" + "\n\n"
115
+
116
+ $versions.each do |version|
117
+ rb4r_name="R4rb"+((version and version!="orig") ? "."+version : "" )
118
+ f << "#{rb4r_name}.#{CONFIG['DLEXT']}: #{v}#{rb4r_name}.c" + "\n"
119
+ f << "\t@echo Now Making R4rb-#{version} extend module" + "\n"
120
+ f << "\t@$(MAKE) -f Makefile-#{version}" + "\n\n"
121
+ end
122
+
123
+ ["clean","distclean","install","site-install"].each do |task|
124
+ f << "#{task}:"
125
+ f << " "+modules if ["install","site-install"].include? task
126
+ f << "\n"
127
+ $versions.each do |version|
128
+ f << "\t@$(MAKE) -f Makefile-#{version} #{task}" + "\n"
129
+ end
130
+ f << "\n"
131
+ end
132
+
133
+ }
@@ -0,0 +1,5 @@
1
+ require 'R4rb.so'
2
+ require 'R4rb/R2rb_init'
3
+ require 'R4rb/R2rb_eval'
4
+ require 'R4rb/robj'
5
+ require 'R4rb/converter'
@@ -0,0 +1,59 @@
1
+ # encoding: UTF-8
2
+ Encoding.default_external = Encoding::UTF_8 unless RUBY_VERSION =~ /1\.8/
3
+ # loading library: compatible for ruby-1.8.7, ruby-1.9.3, ruby-2.0.0
4
+ if RUBY_PLATFORM=~/mingw32/ #because I did not manage to execute the same on mingw32
5
+ # cmd=begin `Rscript`; rescue; "undefined";end
6
+ # unless cmd=="undefined"
7
+ # cmd="Rscript.exe"
8
+ # version=`#{cmd} -e "cat(R.version$major,R.version$minor,sep='.')"`
9
+ # else
10
+ # if File.exists? (jsonfile=File.join(ENV["HOME"],"dyndoc","studio","win32","dyndoc.json"))
11
+ # require 'json'
12
+ # version=JSON.parse(File.read(jsonfile))["Rversion"]
13
+ # end
14
+ # end
15
+ version=nil
16
+ else
17
+ version=`Rscript -e 'cat(R.version$major,R.version$minor,sep=".")'`
18
+ version=version.split(".")[0...2].join(".")
19
+ #puts version
20
+ end
21
+
22
+ found=nil
23
+ [".bundle",".so"].each do |ext|
24
+ ["","../ext/R4rb"].each do |path| #sometimes in lib sometimes in ext/R4rb/
25
+ lib=File.join(File.dirname(__FILE__),path,'R4rb'+(version ? '.'+version+ext : ext ) )
26
+ if File.exists? lib
27
+ puts "#{lib} found"
28
+ require lib
29
+ puts "#{lib} loaded"
30
+ found=true
31
+ break
32
+ end
33
+ end
34
+ break if found
35
+ end
36
+
37
+ # unless found #windows case
38
+ # ["i386","x64"].each do |version|
39
+ # ["","../ext/R4rb"].each do |path|
40
+ # lib=File.join(File.dirname(__FILE__),path,'R4rb.'+version+'.so')
41
+ # if File.exists? lib
42
+ # puts "#{lib} found"
43
+ # require lib
44
+ # found=true
45
+ # break
46
+ # end
47
+ # end
48
+ # end
49
+ # end
50
+
51
+ require 'R4rb.so' unless found
52
+
53
+ # loading ruby files
54
+ require 'R4rb/R2rb_init'
55
+ require 'R4rb/R2rb_eval'
56
+ require 'R4rb/robj'
57
+ require 'R4rb/Rserve'
58
+ require 'R4rb/converter'
59
+ require 'R4rb/R4rb'
@@ -0,0 +1,116 @@
1
+ # encoding: utf-8
2
+ ## Module R2rb
3
+
4
+ module R2rb
5
+ def R2rb.eval(s,aff=nil)
6
+ s=["{\n"+s+"}\n"]
7
+ evalLines s,aff
8
+ end
9
+
10
+ def R2rb.parse(s,aff=nil)
11
+ s=["{\n"+s+"}\n"]
12
+ parseLines s,aff
13
+ end
14
+
15
+ def R2rb.try_eval(code)
16
+ try_code=".result_try_code<-try({\n"+code+"\n},silent=TRUE)\n.result_try_code"
17
+ R2rb << try_code
18
+ puts ".result_try_code".to_R if "inherits(.result_try_code,'try-error')".to_R
19
+ end
20
+
21
+
22
+ def R2rb.<<(s)
23
+ R2rb.eval(s)
24
+ end
25
+
26
+ class RVector
27
+
28
+ def <<(name)
29
+ if name.is_a? Symbol
30
+ @name=name.to_s
31
+ @type="var"
32
+ else
33
+ @name=name
34
+ @type="expr"
35
+ end
36
+ return self
37
+ end
38
+
39
+ def arg=(arg)
40
+ @arg=arg
41
+ end
42
+
43
+ #this method is the same as the previous one but return self! Let us notice that even by adding return self in the previous one
44
+ # I could not manage to execute (rvect.arg="[2]").value_with_arg but fortunately rvect.set_arg("[2]").value_with_arg is working!
45
+ def set_arg(arg)
46
+ @arg=arg
47
+ return self
48
+ end
49
+
50
+ def >(arr)
51
+ res=self.get
52
+ #puts "res";p @name;p res
53
+ if res
54
+ #puts "arr.class:";p arr.class
55
+ #puts "res.class";p res.class
56
+ res=[res] unless res.is_a? Array
57
+ arr.replace(res)
58
+ else
59
+ arr.clear
60
+ end
61
+ return self
62
+ end
63
+
64
+ =begin #Done directly inside R4rb.c
65
+ def value_with_arg(arg)
66
+ old_name,old_type=@name.dup,@type.dup
67
+ @name,@type=@name+arg,"expr"
68
+ value
69
+ @name,@type=old_name,old_type
70
+ end
71
+ =end
72
+
73
+ end
74
+
75
+ @@out=[]
76
+ ##@@out.rb2R=R2rb
77
+
78
+ def R2rb.<(rcode)
79
+ @@out.replace [] ##@@out=[] #important! it could otherwise remove
80
+ @@out.rb2R=self
81
+ @@out < rcode.to_s ##@@out.inR2rb rcode.to_s
82
+ return (@@out.length<=1 ? @@out[0] : @@out)
83
+ end
84
+
85
+ class << self
86
+ alias output <
87
+ end
88
+
89
+ class Server
90
+ @@in,@@out=nil,[]
91
+
92
+ def Server.in
93
+ return @@in
94
+ end
95
+
96
+ def Server.in=(block)
97
+ @@in=block
98
+ end
99
+
100
+ def Server.<<(block)
101
+ @@in << block
102
+ end
103
+
104
+ def Server.out
105
+ return @@out.join("\n")
106
+ end
107
+
108
+ def Server.echo(block=nil)
109
+ @@in=block if block
110
+ R2rb << ".output<<-capture.output({"+@@in+"})"
111
+ return (@@out < '.output').join("\n")
112
+ end
113
+ end
114
+
115
+ end
116
+
@@ -0,0 +1,109 @@
1
+ # encoding: utf-8
2
+
3
+ ## Attention "...\n..." have to be replaced by "...\\n..."
4
+ ## example : 'cat("toto\n")' fails but not 'cat("toto\\n")'
5
+ ## more surprisingly, this fails even in comment '# cat("toto\n")'
6
+
7
+ module R2rb
8
+
9
+ def R2rb.init(args=["--save","--slave","--quiet"])
10
+ @@initR=R2rb.initR(args) unless R2rb.alive?
11
+ end
12
+
13
+ def R2rb.alive?
14
+ defined? @@initR
15
+ end
16
+
17
+ class RBuffer
18
+ def initialize
19
+ @s=String.new
20
+ end
21
+
22
+ def init
23
+ R2rb.init
24
+ end
25
+
26
+ def clear
27
+ @s=""
28
+ end
29
+
30
+ def +(s=nil)
31
+ if s
32
+ s=s.to_s unless s.is_a? String
33
+ @s += "\n"+s
34
+ else
35
+ clear
36
+ end
37
+ return self
38
+ end
39
+
40
+ def exec(aff=nil)
41
+ R2rb.eval @s,aff
42
+ end
43
+ end
44
+
45
+ class RConsole
46
+ def initialize
47
+ require "readline"
48
+ end
49
+
50
+ def init args
51
+ R2rb.init args
52
+ end
53
+
54
+ def exec
55
+ #todo : continuation
56
+ toggle=true
57
+ fin=false
58
+ words=[]
59
+ rvect=R2rb::RVector.new("")
60
+ Readline.completion_proc=Proc.new {|e|
61
+ cpt=0
62
+ begin
63
+ begin
64
+ cpt += 1
65
+ toggle = !toggle
66
+ rvect << (toggle ? "apropos(" : "ls(pat=")+"'^"+e+"')" > words
67
+ res=words.map{|w| w[Regexp.new("^"+e+".*")]}-[nil]
68
+ end while res.empty? and cpt<3
69
+ res
70
+ rescue
71
+ warn("\r"+e+" not suitable for completion!!!")
72
+ []
73
+ end
74
+ }
75
+ Readline.completion_append_character=""
76
+ begin
77
+ line = Readline.readline("R> ", true)
78
+ if !(fin = line=="quit")
79
+ R2rb.eval line,true
80
+ toggle=true
81
+ end
82
+ end until fin
83
+ end
84
+ end
85
+ end
86
+
87
+ def find_installed_R
88
+
89
+ if RUBY_PLATFORM=~/mingw/ or RUBY_PLATFORM=~/msys/
90
+ ENV["R_HOME"]=`R RHOME`
91
+ elsif RUBY_PLATFORM=~/darwin/
92
+ ENV["R_HOME"]=`R RHOME`.strip
93
+ else
94
+ dirs=["/usr/lib/R","/usr/local/lib/R","/usr/lib64/R"]
95
+
96
+ dirs.each do |dir|
97
+ if FileTest.exists?(dir)
98
+ return dir
99
+ end
100
+ end
101
+
102
+ raise RuntimeError, "couldn't find R Home : R seems to be uninstalled!!"
103
+ end
104
+
105
+ end
106
+
107
+ ENV["R_HOME"]=`R RHOME`.strip.split("\n").select{|l| l=~/^\//}[0] unless `R RHOME`.empty?
108
+
109
+ ENV["R_HOME"]=find_installed_R unless ENV["R_HOME"]
@@ -0,0 +1,164 @@
1
+ # encoding: utf-8
2
+ class Object
3
+
4
+ def R4rb_is(mode)
5
+ unless [R2rb,Rserve,"R2rb","Rserve",:R2rb,:Rserve].include? mode
6
+ puts "Improper value for R4rb_is function!"
7
+ return
8
+ end
9
+ unless mode.is_a? Module
10
+ mode=mode.to_s.to_sym
11
+ ##puts "R4rb_is: mode=#{mode}"
12
+ mode= case mode
13
+ when :R2rb
14
+ R2rb
15
+ when :Rserve
16
+ Rserve
17
+ end
18
+ end
19
+ if mode==Rserve and !Rserve.running?
20
+ puts "Rserve is not running and R4rb_is method cannot be set to :Rserve!"
21
+ mode=R2rb
22
+ end
23
+ ##p mode
24
+ ## trick to avoid warning when modifying constant!
25
+ con=Object #( self.is_a?(Module) ? self : self.class )
26
+ con.send(:remove_const, :R4rb) if con.const_defined?(:R4rb)
27
+ con.const_set(:R4rb, mode)
28
+ end
29
+
30
+ def R2rb_running?
31
+ Object.const_get(:R4rb)==R2rb
32
+ end
33
+
34
+ def Rserve_running?
35
+ Object.const_get(:R4rb)==Rserve
36
+ end
37
+
38
+ def R4rb_status?
39
+ puts "R is now in "+ ( R2rb_running? ? "local" : "server" )+" use!"
40
+ end
41
+
42
+ end
43
+
44
+
45
+
46
+ ## first init
47
+ unless Object.const_defined?(:R4rb)
48
+ ##puts "First, R4rb initialized to R2rb!"
49
+ R4rb_is R2rb
50
+ end
51
+
52
+ =begin
53
+ module R4rb
54
+
55
+ def R4rb.mode=(mode) #mode=:R4rb or :Rserve
56
+ return unless [:R2rb,:Rserve].include? mode
57
+ puts mode
58
+ puts Rserve
59
+ $R4rb=(mode==:R2rb ? R2rb : Rserve)
60
+ puts "R4rb mode changed: #{$R2rb.inspect}"
61
+ end
62
+
63
+ def R4rb.init(args=["--save","--slave","--quiet"])
64
+ R2rb.init(args)
65
+ end
66
+
67
+ def R4rb.<<(code)
68
+ $R4rb << code
69
+ end
70
+
71
+ def R4rb.<(code)
72
+ $R4rb < code
73
+ end
74
+
75
+ def R4rb.output(code)
76
+ $R4rb.output(code)
77
+ end
78
+
79
+
80
+ class << self
81
+ alias eval <<
82
+ end
83
+
84
+ RVector=$R4rb::RVector
85
+
86
+ end
87
+ =end
88
+
89
+ ## reload
90
+ class String
91
+
92
+ def R2rb
93
+ R2rb < self
94
+ end
95
+
96
+ def Rserve(cli=nil)
97
+ Rserve < self
98
+ end
99
+
100
+ def R4rb
101
+ R4rb < self
102
+ end
103
+
104
+ alias to_R R4rb
105
+ alias evalR R4rb
106
+ alias Reval R4rb
107
+ alias R R4rb
108
+
109
+ end
110
+
111
+ class Array
112
+
113
+ def R2rb(var)
114
+ R2rb::RVector.assign(var.to_s,self)
115
+ end
116
+
117
+ def RServe(var,cli=nil)
118
+ Rserve::RVector.assign(var.to_s,self,cli)
119
+ end
120
+
121
+ def R4rb(var)
122
+ R4rb::RVector.assign(var.to_s,self)
123
+ end
124
+
125
+ alias to_R R4rb
126
+ alias evalR R4rb
127
+ alias Reval R4rb
128
+ alias R R4rb
129
+
130
+
131
+ #@@rb2R=nil
132
+
133
+ ## connect Array class to some RVector!!!
134
+ def Array.initR(init=true)
135
+ R2rb.init if init
136
+ #p @@rb2R
137
+ end
138
+
139
+ ## Here again to make this package self-content
140
+ def rb2R=(mode=nil)
141
+ ##puts "rb2R mode #{object_id}";p mode
142
+ mode=R4rb unless mode
143
+ return if @rb2R_mode and @rb2R_mode==mode
144
+ @rb2R_mode=mode unless @rb2R_mode
145
+ @rb2R=(@rb2R_mode==Rserve ? Rserve::RVector.new("") : R2rb::RVector.new("") )
146
+ ##puts "rb2R=";p @rb2R
147
+ end
148
+
149
+ def >(outR) #outR represents here an R object
150
+ self.rb2R=nil unless @rb2R
151
+ @rb2R << outR
152
+ @rb2R < self
153
+ return self
154
+ end
155
+
156
+ def <(outR) #outR represents here an R expression to execute and put inside the Array
157
+ #p @rb2R
158
+ self.rb2R=nil unless @rb2R
159
+ @rb2R << outR
160
+ @rb2R > self
161
+ return self
162
+ end
163
+
164
+ end