rinruby 0.99.1 → 0.99.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.txt +2 -1
  2. data/lib/rinruby.rb +227 -7
  3. metadata +38 -45
data/README.txt CHANGED
@@ -4,11 +4,12 @@ http://rinruby.ddahl.org
4
4
  Copyright 2005-2008 David B. Dahl
5
5
 
6
6
  Developed by David B. Dahl
7
- Documented by Scott Crawford and David B. Dahl
7
+ Documented by David B. Dahl and Scott Crawford
8
8
  Homepage: http://rinruby.ddahl.org
9
9
 
10
10
 
11
11
 
12
+
12
13
  This program is free software: you can redistribute it and/or modify
13
14
  it under the terms of the GNU General Public License as published by
14
15
  the Free Software Foundation, either version 3 of the License, or
@@ -1,11 +1,45 @@
1
- # RinRuby
2
- # Accessing the R interpreter from pure Ruby
3
- # Copyright 2005-2008 David B. Dahl
1
+ #=RinRuby: Accessing the R[http://www.r-project.org] interpreter from pure Ruby
4
2
  #
5
- # Coded by David B. Dahl
6
- # Documented by Scott Crawford and David B. Dahl
7
- # http://rinruby.ddahl.org
3
+ #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.
8
4
  #
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.
6
+ #
7
+ #<b>Code</b>:
8
+ #
9
+ # require "rinruby"
10
+ # n = 10
11
+ # beta_0 = 1
12
+ # beta_1 = 0.25
13
+ # alpha = 0.05
14
+ # seed = 23423
15
+ # R.x = (1..n).entries
16
+ # R.eval <<EOF
17
+ # set.seed(#{seed})
18
+ # y <- #{beta_0} + #{beta_1}*x + rnorm(#{n})
19
+ # fit <- lm( y ~ x )
20
+ # est <- round(coef(fit),3)
21
+ # pvalue <- summary(fit)$coefficients[2,4]
22
+ # EOF
23
+ # puts "E(y|x) ~= #{R.est[0]} + #{R.est[1]} * x"
24
+ # if R.pvalue < alpha
25
+ # puts "Reject the null hypothesis and conclude that x and y are related."
26
+ # else
27
+ # puts "There is insufficient evidence to conclude that x and y are related."
28
+ # end
29
+ #
30
+ #<b>Output</b>:
31
+ #
32
+ # E(y|x) ~= 1.264 + 0.273 * x
33
+ # Reject the null hypothesis and conclude that x and y are related.
34
+ #
35
+ #Coded by:: David B. Dahl
36
+ #Documented by:: David B. Dahl & Scott Crawford
37
+ #Copyright:: 2005-2008
38
+ #Web page:: http://rinruby.ddahl.org
39
+ #E-mail:: mailto:rinruby@ddahl.org
40
+ #License:: GNU General Public License (GPL), version 3 or later
41
+ #
42
+ #--
9
43
  # This program is free software: you can redistribute it and/or modify
10
44
  # it under the terms of the GNU General Public License as published by
11
45
  # the Free Software Foundation, either version 3 of the License, or
@@ -18,15 +52,45 @@
18
52
  #
19
53
  # You should have received a copy of the GNU General Public License
20
54
  # along with this program. If not, see <http://www.gnu.org/licenses/>
55
+ #++
56
+ #
21
57
  #
58
+ #The required files java and readline are used when present at functionality.
22
59
 
23
60
  class RinRuby
24
61
 
62
+ VERSION = '0.99.3'
63
+
25
64
  require 'socket'
26
65
 
27
66
  attr_reader :interactive
28
67
  attr_reader :readline
29
68
 
69
+ #RinRuby is invoked within a Ruby script (or the interactive 'irb' prompt denoted >>) using:
70
+ #
71
+ # >> require "rinruby"
72
+ #
73
+ #This reads the definition of the RinRuby class into the current Ruby interpreter and creates an instance of the RinRuby class named R. There is a second method for starting an instance of R which allows the user to use any name for the instance, in this case myr:
74
+ #
75
+ # >> require "rinruby"
76
+ # >> myr = RinRuby.new
77
+ # >> myr.eval "rnorm(1)"
78
+ #Any number of independent instances of R can be created in this way.
79
+ #
80
+ #<b>Parameters that can be passed to the new method</b>
81
+ #
82
+ #* echo: By setting the echo to false, output from R is suppressed, although warnings are still printed. This option can be changed later by using the echo method. The default is true.
83
+ #* interactive: When interactive is false, R is run in non-interaction mode, resulting in plots without an explicit device being written to Rplots.pdf. Otherwise, when interactive is true, such plots are shown on the screen. The default is true.
84
+ #* executable: The path of the R executable (which is R in Linux and Mac OSX, or Rterm.exe in Windows) can be set with the executable argument. The default is nil, which on Windows uses the registry keys to find the path, or on linux and Mac OS X uses the path defined by $PATH.
85
+ #* port_number: This is the smallest port number on the local host that could be used to pass data between Ruby and R. The actual port number used depends on port_width.
86
+ #* port_width: RinRuby will randomly select a uniform number between port_number and port_number + port_width - 1, (inclusive), to pass data between Ruby and R. If the randomly selected port is not available, RinRuby will continue selecting random ports until it finds one that is available. By setting port_width to 1, RinRuby will wait until port_number is available. The default port_width is 1000.
87
+ #
88
+ #It may be desirable to change the parameters to the instance of R, but still call it by the name of R. In that case the old instance of R which was created with the require rinruby statement should be closed first using the quit method which is explained below. Unless the previous instance is killed, it will continue to use system resources until exiting Ruby. The following shows an example by changing the parameter echo:
89
+ #
90
+ # >> require "rinruby"
91
+ # >> R.quit
92
+ # >> R = RinRuby.new(false)
93
+
30
94
  def initialize(echo=true, interactive=true, executable=nil, port_number=38442, port_width=1000)
31
95
  while true
32
96
  begin
@@ -45,7 +109,7 @@ class RinRuby
45
109
  when /bccwin/: 'windows'
46
110
  when /cygwin/: 'windows-cygwin'
47
111
  when /java/
48
- require 'java'
112
+ require 'java' #:nodoc:
49
113
  if java.lang.System.getProperty("os.name") =~ /[Ww]indows/
50
114
  'windows-java'
51
115
  else
@@ -85,6 +149,8 @@ class RinRuby
85
149
  @socket = @server_socket.accept
86
150
  end
87
151
 
152
+ #The method quit will gracefully close the bridge between Ruby and R, freeing up system resources. This method is automatically run when a Ruby script ends.
153
+
88
154
  def quit
89
155
  @writer.puts "q(save='no')"
90
156
  @socket.close rescue true
@@ -95,6 +161,36 @@ class RinRuby
95
161
  true
96
162
  end
97
163
 
164
+
165
+ #The eval instance method passes the R commands contained in the supplied string and displays any resulting plots or prints the output. For example:
166
+ #
167
+ # >> sample_size = 10
168
+ # >> R.eval "x <- rnorm(#{sample_size})"
169
+ # >> R.eval "summary(x)"
170
+ # >> R.eval "sd(x)"
171
+ #
172
+ #produces the following:
173
+ #
174
+ # Min. 1st Qu. Median Mean 3rd Qu. Max.
175
+ # -1.88900 -0.84930 -0.45220 -0.49290 -0.06069 0.78160
176
+ # [1] 0.7327981
177
+ #This example used a string substitution to make the argument to first eval method equivalent to x <- rnorm(10). This example used three invocations of the eval method, but a single invoke is possible using a here document:
178
+ # >> R.eval <<EOF
179
+ # x <- rnorm(#{sample_size})
180
+ # summary(x)
181
+ # sd(x)
182
+ # EOF
183
+ #
184
+ #<b>Parameters that can be passed to the eval method</b>
185
+ #
186
+ #* string: The string parameter is the code which is to be passed to R, for example, string = "hist(gamma(1000,5,3))". The string can also span a large chunk of code by use of a here document, as shown:
187
+ # R.eval <<EOF
188
+ # x<-rgamma(1000,5,3)
189
+ # hist(x)
190
+ # EOF
191
+ #
192
+ #* echo_override: This argument allows one to set the echo behavior for this call only. The default for echo_override is nil, which does not override the current echo behavior.
193
+
98
194
  def eval(string, echo_override=nil)
99
195
  echo_enabled = ( echo_override != nil ) ? echo_override : @echo_enabled
100
196
  begin
@@ -134,6 +230,14 @@ class RinRuby
134
230
  true
135
231
  end
136
232
 
233
+ #When using sending code to Ruby using an interactive prompt, this method will change the prompt to an R prompt. From the R prompt commands can be sent to R exactly as if the R program was actually running. When the user is ready to return to Ruby, then the command exit() will return the prompt to Ruby. This is the ideal situation for the explorative programmer who needs to run several lines of code in R, and see the results after each command. This is also an easy way to execute loops without the use of a here document. It should be noted that the prompt command does not work in a script, just Ruby's interaction irb.
234
+ #
235
+ #<b>Parameters that can be passed to the prompt method:</b>
236
+ #
237
+ #* regular_prompt: This defines the string used to denote the R prompt.
238
+ #
239
+ #* continue_prompt: This is the string used to denote R's prompt for an incomplete statement (such as a multiple for loop).
240
+
137
241
  def prompt(regular_prompt="> ", continue_prompt="+ ")
138
242
  raise "The 'prompt' method only available in 'interactive' mode" if ! @interactive
139
243
  return false if ! eval("0",false)
@@ -170,6 +274,25 @@ class RinRuby
170
274
  true
171
275
  end
172
276
 
277
+ #If a method is called which is not defined, then it is assumed that the user is attempting to either pull or assign a variable to R. This allows the user to do a simplified notation. When an undefined method is followed by an equals sign, then it is assumed to be an assignment. Thus
278
+ #
279
+ # >> R.x = 2
280
+ #
281
+ #is the same as
282
+ #
283
+ # >> R.assign("x",2)
284
+ #
285
+ #If an equals sign does not follow the method, then it is assumed to be a pull. Thus
286
+ #
287
+ # >> n = R.x
288
+ #
289
+ #is the same as
290
+ #
291
+ # >> n = R.pull("x")
292
+ #
293
+ #This is also the reason is it not recommended that any variables be named after a method already defined, since this simplified notation would not work for that variable.
294
+ #The parameters passed to method_missing are those used for the pull or assign depending on the context.
295
+
173
296
  def method_missing(symbol, *args)
174
297
  name = symbol.id2name
175
298
  if name =~ /(.*)=$/
@@ -181,6 +304,42 @@ class RinRuby
181
304
  end
182
305
  end
183
306
 
307
+ #Data is copied from Ruby to R using the assign method or a short-hand equivalent. For example:
308
+ #
309
+ # >> names = ["Lisa","Teasha","Aaron","Thomas"]
310
+ # >> R.assign "people", names
311
+ # >> R.eval "sort(people)"
312
+ #
313
+ #produces the following :
314
+ #
315
+ # [1] "Aaron" "Lisa" "Teasha" "Thomas"
316
+ #
317
+ #The short-hand equivalent to the assign method is simply:
318
+ #
319
+ # >> R.people = names
320
+ #
321
+ #Some care is needed when using the short-hand of the assign method since the label (i.e., people in this case) must be a valid method name in Ruby. For example, R.copy.of.names = names will not work, but R.copy_of_names = names is permissible.
322
+ #
323
+ #The assign method supports Ruby variables of type Fixnum (i.e., integer), Float (i.e., double), String, and arrays of one of those three fundamental types. Data in other formats must be coerced when copying to R.
324
+ #
325
+ #<b>Parameters that can be passed to the assign method:</b>
326
+ #
327
+ #* name: The name of the variable desired in R.
328
+ #
329
+ #* value: The value the R variable should have. The assign method supports Ruby variables of type Fixnum (i.e., integer), Float (i.e., double), String, and arrays of one of those three fundamental types. Data in other formats must be coerced when copying to R.
330
+ #
331
+ #* as_integer: Numerical variables in R are stored as doubles by default, but R provides the ability to have vectors of integers. A vector of integers may be required when interfacing between programs such as R, Fortran, C, or C++. Setting as_integer to true will specify that the value should be type cast into integers. The default is false.
332
+ #
333
+ #The assign method is an alternative to the simplified method, with some additional flexibility. When using the simplified method, the parameters of name and value are automatically used, in other words:
334
+ #
335
+ # >> R.test = 144
336
+ #
337
+ #is the same as
338
+ #
339
+ # >> R.assign("test",144)
340
+ #
341
+ #Of course it would be confusing to use the shorthand notation to assign a variable named eval, echo, or any other already defined function. RinRuby would assume you were calling the function, rather than trying to assign a variable.
342
+
184
343
  def assign(name, value, as_integer=false)
185
344
  if value.kind_of?(String)
186
345
  type = RinRuby_Type_String
@@ -240,6 +399,59 @@ class RinRuby
240
399
  nil
241
400
  end
242
401
 
402
+ #Data is copied from R to Ruby using the pull method or a short-hand equivalent. The R object x defined with an eval method can be copied to Ruby object copy_of_x as follows:
403
+ #
404
+ # >> R.eval "x <- rnorm(10)"
405
+ # >> copy_of_x = R.pull "x"
406
+ # >> puts copy_of_x
407
+ #
408
+ #which produces the following :
409
+ #
410
+ # -0.376404489256671
411
+ # -1.0759798269397
412
+ # -0.494240140140996
413
+ # 0.131171385795721
414
+ # -0.878328334369391
415
+ # -0.762290423047929
416
+ # -0.410227216105828
417
+ # 0.0445512804225151
418
+ # -1.88887454545995
419
+ # 0.781602719849499
420
+ #
421
+ #RinRuby also supports a convenient short-hand notation when the argument to pull is simply a previously-defined R object (whose name conforms to Ruby's requirements for method names). For example:
422
+ #
423
+ # >> copy_of_x = R.x
424
+ #
425
+ #The explicit assign method, however, can take an arbitrary R statement. For example:
426
+ #
427
+ # >> summary_of_x = R.pull "as.numeric(summary(x))"
428
+ # >> puts summary_of_x
429
+ #
430
+ #produces the following :
431
+ #
432
+ # -1.889
433
+ # -0.8493
434
+ # -0.4522
435
+ # -0.4929
436
+ # -0.06069
437
+ # 0.7816
438
+ #
439
+ #Notice the use above of the as.numeric function in R. This is necessary since the pull method only supports R vectors of numerics (i.e., integers or doubles) and characters (i.e., strings). Data in other formats must be coerced when copying to Ruby.
440
+ #
441
+ #<b>Parameters that can be passed to the pull method:</b>
442
+ #
443
+ #* string: The name of the variable that should be pulled from R. The pull method only supports R vectors of numerics (i.e., integers or doubles) and characters (i.e., strings). Data in other formats must be coerced when copying to Ruby.
444
+ #
445
+ #* singletons: R represents a single number as a vector of length one, but in Ruby it is often more convenient to use a number rather than an array of length one. Setting singleton=false will cause the pull method to shed the array, while singletons=true will return the number of string within an array. The default is false.
446
+ #
447
+ #The pull method is an alternative to the simplified form where the parameters are automatically used, in other words:
448
+ #
449
+ # >> puts R.test
450
+ #
451
+ #is the same as
452
+ #
453
+ # >> puts R.pull("test")
454
+
243
455
  def pull(string, singletons=false)
244
456
  @writer.puts <<-EOF
245
457
  #{RinRuby_Variable} <- try(#{string})
@@ -303,6 +515,12 @@ class RinRuby
303
515
  result
304
516
  end
305
517
 
518
+ #The eval method displays the output from R by default, but this can be turned off by using the echo method. This method is useful when the output behavior needs to be changed
519
+ #
520
+ #<b>Parameters that can be passed to the eval method</b>
521
+ #
522
+ #* enable: Setting enable to false will turn all output off until the echo command is used again with enable equal to true. Warnings are still sent to the output. The default is nil, which will return the current setting.
523
+
306
524
  def echo(enable=nil)
307
525
  @echo_enabled = enable if enable != nil
308
526
  @echo_enabled
@@ -384,6 +602,8 @@ class RinRuby
384
602
  end
385
603
 
386
604
  if ! defined?(R)
605
+ #R is an instance of RinRuby. If for some reason the user does not want R to be initialized (to save system resources), then create a default value for R (e.g., <b>R=2</b> ) in which case RinRuby will not overwrite the value of R.
606
+
387
607
  R = RinRuby.new
388
608
  end
389
609
 
metadata CHANGED
@@ -1,54 +1,47 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
+ required_ruby_version: !ruby/object:Gem::Requirement
3
+ requirements:
4
+ - - '>='
5
+ - !ruby/object:Gem::Version
6
+ version: !str 0
7
+ version:
8
+ email: rinruby@ddahl.org
9
+ cert_chain: []
10
+ summary: Accessing the R interpreter from pure Ruby
11
+ post_install_message:
12
+ extra_rdoc_files: []
13
+ homepage: http://rinruby.ddahl.org
14
+ signing_key:
2
15
  name: rinruby
3
- version: !ruby/object:Gem::Version
4
- version: 0.99.1
5
- platform: ruby
6
- authors:
7
- - David B. Dahl
16
+ rdoc_options: []
8
17
  autorequire:
9
- bindir: bin
10
- cert_chain: []
11
-
12
- date: 2008-06-16 00:00:00 -05:00
13
- default_executable:
14
- dependencies: []
15
-
16
- description:
17
- email: rinruby@ddahl.org
18
+ rubyforge_project: rinruby
18
19
  executables: []
19
-
20
- extensions: []
21
-
22
- extra_rdoc_files: []
23
-
20
+ description:
21
+ specification_version: 2
22
+ default_executable:
24
23
  files:
25
24
  - lib/rinruby.rb
26
25
  - README.txt
27
- has_rdoc: true
28
- homepage: http://rinruby.ddahl.org
29
- post_install_message:
30
- rdoc_options: []
31
-
32
- require_paths:
33
- - lib
34
- required_ruby_version: !ruby/object:Gem::Requirement
35
- requirements:
36
- - - ">="
37
- - !ruby/object:Gem::Version
38
- version: "0"
39
- version:
40
- required_rubygems_version: !ruby/object:Gem::Requirement
26
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
27
  requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- version: "0"
28
+ - - '>='
29
+ - !ruby/object:Gem::Version
30
+ version: !str 0
45
31
  version:
46
- requirements: []
47
-
48
- rubyforge_project: rinruby
49
- rubygems_version: 1.1.1
50
- signing_key:
51
- specification_version: 2
52
- summary: Accessing the R interpreter from pure Ruby
32
+ extensions: []
33
+ rubygems_version: 1.1.0
34
+ requirements:
35
+ - R (http://www.r-project.org), an environment for statistical computing and graphics
36
+ authors:
37
+ - David B. Dahl
38
+ date: 2008-07-02 05:00:00 +00:00
39
+ platform: ruby
53
40
  test_files: []
54
-
41
+ version: !ruby/object:Gem::Version
42
+ version: 0.99.3
43
+ require_paths:
44
+ - lib
45
+ dependencies: []
46
+ bindir: bin
47
+ has_rdoc: true