Skip to content

Commit 7f6820c

Browse files
committedAug 9, 2014
Merged from tuesy
2 parents 494c0b9 + d700189 commit 7f6820c

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed
 

‎lib/rinruby.rb

+26-27
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
#RinRuby is a Ruby library that integrates the R interpreter in Ruby, making R's statistical routines and graphics available within Ruby. The library consists of a single Ruby script that is simple to install and does not require any special compilation or installation of R. Since the library is 100% pure Ruby, it works on a variety of operating systems, Ruby implementations, and versions of R. RinRuby's methods are simple, making for readable code. The {website [rinruby.ddahl.org]}[http://rinruby.ddahl.org] describes RinRuby usage, provides comprehensive documentation, gives several examples, and discusses RinRuby's implementation.
44
#
5-
#Below is a simple example of RinRuby usage for simple linear regression. The simulation parameters are defined in Ruby, computations are performed in R, and Ruby reports the results. In a more elaborate application, the simulation parameter might come from input from a graphical user interface, the statistical analysis might be more involved, and the results might be an HTML page or PDF report.
5+
#Below is a simple example of RinRuby usage for simple linear regression. The simulation parameters are defined in Ruby, computations are performed in R, and Ruby reports the results. In a more elaborate application, the simulation parameter might come from input from a graphical user interface, the statistical analysis might be more involved, and the results might be an HTML page or PDF report.
66
#
77
#<b>Code</b>:
88
#
@@ -34,6 +34,7 @@
3434
#
3535
#Coded by:: David B. Dahl
3636
#Documented by:: David B. Dahl & Scott Crawford
37+
#Maintained by:: Claudio Bustos
3738
#Copyright:: 2005-2009
3839
#Web page:: http://rinruby.ddahl.org
3940
#E-mail:: mailto:rinruby@ddahl.org
@@ -58,21 +59,19 @@
5859
#The files "java" and "readline" are used when available to add functionality.
5960
require 'matrix'
6061
class RinRuby
61-
62+
6263
require 'socket'
6364

64-
6565
VERSION = '2.0.3'
6666

67-
6867
attr_reader :interactive
6968
attr_reader :readline
7069
# Exception for closed engine
7170
EngineClosed=Class.new(Exception)
7271
# Parse error
7372
ParseError=Class.new(Exception)
74-
75-
73+
74+
7675
#RinRuby is invoked within a Ruby script (or the interactive "irb" prompt denoted >>) using:
7776
#
7877
# >> require "rinruby"
@@ -98,7 +97,7 @@ class RinRuby
9897
# >> require "rinruby"
9998
# >> R.quit
10099
# >> R = RinRuby.new(false)
101-
attr_reader :echo_enabled
100+
attr_accessor :echo_enabled
102101
attr_reader :executable
103102
attr_reader :port_number
104103
attr_reader :port_width
@@ -115,7 +114,7 @@ def initialize(*args)
115114
opts[:port_width]=args.shift unless args.size==0
116115
end
117116
default_opts= {:echo=>true, :interactive=>true, :executable=>nil, :port_number=>38442, :port_width=>1000, :hostname=>'127.0.0.1'}
118-
117+
119118
@opts=default_opts.merge(opts)
120119
@port_width=@opts[:port_width]
121120
@executable=@opts[:executable]
@@ -187,16 +186,16 @@ def initialize(*args)
187186
#The quit method will properly close the bridge between Ruby and R, freeing up system resources. This method does not need to be run when a Ruby script ends.
188187

189188
def quit
190-
begin
189+
begin
191190
@writer.puts "q(save='no')"
192191
# TODO: Verify if read is needed
193192
@socket.read()
194-
#@socket.close
195-
@engine.close
196-
197-
198-
@server_socket.close
199-
#@reader.close
193+
#@socket.close
194+
@engine.close
195+
196+
197+
@server_socket.close
198+
#@reader.close
200199
#@writer.close
201200
true
202201
ensure
@@ -527,8 +526,8 @@ def echo(enable=nil,stderr=nil)
527526
RinRuby_Max_R_Integer = 2**31-1
528527
RinRuby_Min_R_Integer = -2**31+1
529528
#:startdoc:
530-
531-
529+
530+
532531
def r_rinruby_parseable
533532
@writer.puts <<-EOF
534533
rinruby_parseable<-function(var) {
@@ -561,7 +560,7 @@ def r_rinruby_get_value
561560
}
562561
EOF
563562
end
564-
563+
565564
def r_rinruby_pull
566565
@writer.puts <<-EOF
567566
rinruby_pull <-function(var)
@@ -573,7 +572,7 @@ def r_rinruby_pull
573572
writeBin(as.integer(#{RinRuby_Type_Matrix}),#{RinRuby_Socket},endian="big")
574573
writeBin(as.integer(dim(var)[1]),#{RinRuby_Socket},endian="big")
575574
writeBin(as.integer(dim(var)[2]),#{RinRuby_Socket},endian="big")
576-
575+
577576
} else if ( is.double(var) ) {
578577
writeBin(as.integer(#{RinRuby_Type_Double}),#{RinRuby_Socket},endian="big")
579578
writeBin(as.integer(length(var)),#{RinRuby_Socket},endian="big")
@@ -595,8 +594,8 @@ def r_rinruby_pull
595594
}
596595
}
597596
EOF
598-
599-
597+
598+
600599
end
601600
def to_signed_int(y)
602601
if y.kind_of?(Integer)
@@ -614,7 +613,7 @@ def assign_engine(name, value)
614613
eval "#{name}=matrix(c(#{values.join(',')}), #{value.row_size}, #{value.column_size}, TRUE)"
615614
return original_value
616615
end
617-
616+
618617
if value.kind_of?(String)
619618
type = RinRuby_Type_String
620619
length = 1
@@ -687,7 +686,7 @@ def pull_engine(string)
687686
end
688687
@socket.read(4,buffer)
689688
length = to_signed_int(buffer.unpack('N')[0].to_i)
690-
689+
691690
if ( type == RinRuby_Type_Double )
692691
@socket.read(8*length,buffer)
693692
result = buffer.unpack('G'*length)
@@ -730,12 +729,12 @@ def complete?(string)
730729
@writer.puts "rm(#{RinRuby_Parse_String})"
731730
result = to_signed_int(buffer.unpack('N')[0].to_i)
732731
return result==-1 ? false : true
733-
734-
=begin
735-
732+
733+
=begin
734+
736735
result = pull_engine("unlist(lapply(c('.*','^Error in parse.*','^Error in parse.*unexpected end of input.*'),
737736
grep,try({parse(text=#{RinRuby_Parse_String}); 1}, silent=TRUE)))")
738-
737+
739738
return true if result.length == 1
740739
return false if result.length == 3
741740
raise ParseError, "Parse error"

0 commit comments

Comments
 (0)
Please sign in to comment.