rserve-simpler 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -18,21 +18,20 @@ the heavy lifting and should be consulted for background.
18
18
 
19
19
  === converse, convert, command
20
20
 
21
- These commands specify ways of talking to your Rserve connection. All three
22
- allow passing names through to R in a hash. They differ chiefly in the type
23
- of output you'll receive back from R.
21
+ These commands specify ways of talking to your Rserve connection. They differ
22
+ chiefly in the type of output you'll receive back from R.
24
23
 
25
- # converse: to_ruby the result
24
+ # converse: casts the result with 'to_ruby'
26
25
  r.converse "mean(c(1,2,3))" # -> 2.0
27
26
 
28
- # convert: Rserve::REXP result
27
+ # convert: returns the raw Rserve::REXP result (like eval)
29
28
  rexp = r.convert "mean(c(1,2,3))" # -> #<Rserve::REXP::Double:0x00000002296498 @payload=[2.0], @attr=nil>
30
29
  rexp.as_doubles # -> [2.0]
31
30
  rexp.to_ruby # -> 2.0
32
31
 
33
- # command: boolean reply if things went well
32
+ # command: tell R what to do and only expect boolean reply
34
33
  r.command "z <- mean(c(1,2,3))" # -> true
35
- # can retrieve variables with converse
34
+ # all variables are persistent in the session and can be retrieved later
36
35
  r.converse "z" # -> 2.0
37
36
 
38
37
  converse/convert/command let you name variables in a hash as a shortcut to
@@ -48,14 +47,16 @@ converse/convert/command let you name variables in a hash as a shortcut to
48
47
  '>>' is an alias for converse, so you can write code like this:
49
48
 
50
49
  r >> "mean(c(1,2,3))" # -> 2.0
51
- # note: use '.' to get proper behavior with multiple args or blocks
50
+ # note: use '.>>' (and parentheses at times) to get proper behavior with multiple args or blocks
52
51
  r.>> "cor(a,b)", a: [1,2,3], b: [1,2,3]
53
52
 
54
53
  === simple DataFrame
55
54
 
56
- ==== with a hash
55
+ Data frames are a prominent object in R. Simpler provides a simple ruby DataFrame object.
57
56
 
58
- Column names are derived from hash keys (#keys). Ruby 1.9 hashes keep track
57
+ ==== Hash based
58
+
59
+ Column names are derived from the data hash's keys. Ruby 1.9 hashes keep track
59
60
  of insertion order. Use an OrderedHash (gem install orderedhash) for ruby
60
61
  1.8, or set colnames.
61
62
 
@@ -75,6 +76,8 @@ of insertion order. Use an OrderedHash (gem install orderedhash) for ruby
75
76
 
76
77
  ==== with an array of Structs
77
78
 
79
+ The equivalent data frame as above can be generated with an array of Struct objects.
80
+
78
81
  DataRow = Struct.new(:var1, :fac1, :res1)
79
82
  structs = [
80
83
  DataRow.new(1,3,4),
@@ -105,15 +108,35 @@ Rserve is great for graphical output. Here are some pointers when getting start
105
108
 
106
109
  ==== plot to screen
107
110
 
108
- r.command "plot(c(1,2,3), c(3,4,5))"
111
+ Plots to x11() are persistent until the script or session ends.
112
+
113
+ Inside an irb session:
114
+
115
+ irb>> r >> "plot(c(1,2,3), c(3,4,5))"
109
116
  # if you resize a window (at least on Ubuntu) you may need to re-issue the call
110
- r.command "plot(c(1,2,3), c(3,4,5))"
117
+ irb>> r >> "plot(c(1,2,3), c(3,4,5))"
111
118
  # return a list of currently used devices (device 2 is the only one being used)
112
119
  # to_ruby returns an array of 1 value as a number
113
- r.converse "dev.list()" # -> 2
120
+ irb>> r >> "dev.list()"
121
+ => 2
114
122
  # shut off graphics device 2
115
- r.command "dev.off(2)"
116
- r.command "graphics.off()" # shut off all graphics
123
+ # [??] sometimes this generates windows 3 & 4, not sure what is happening
124
+ irb>> r >> "dev.off(2)"
125
+ # shut off all graphics
126
+ irb>> r >> "graphics.off()"
127
+
128
+ In a script, your plot will appear until the script exits. This usually means
129
+ you'll miss the display of your plot. Two ways to deal with this:
130
+
131
+ ===== sleep
132
+
133
+ r >> "plot(c(1,2,3), c(4,5,6))"
134
+ sleep(7) # pause for 7 seconds
135
+
136
+ ===== r.pause
137
+
138
+ r >> "plot(c(1,2,3), c(4,5,6))"
139
+ r.pause # pauses things until a key is pressed in terminal
117
140
 
118
141
  ==== plot to file
119
142
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -2,8 +2,15 @@
2
2
  module Rserve
3
3
  # An R-centric container for storing data frame-ish data
4
4
  class DataFrame
5
+
6
+ # the colnames can be set explicitly, or they will be generated by
7
+ # data.keys
8
+ attr_writer :colnames
9
+ # rownames as an array of Integers or Strings. nil by default.
5
10
  attr_accessor :rownames
6
- attr_accessor :hash
11
+ # a hash where the keys are the column names and the values are arrays of
12
+ # data
13
+ attr_accessor :data
7
14
 
8
15
  # takes an array of structs and returns a data frame object
9
16
  def self.from_structs(array)
@@ -14,14 +21,17 @@ module Rserve
14
21
  lengthwise_arrays[n][m] = val
15
22
  end
16
23
  end
17
- hash = {}
24
+ data = {}
18
25
  names.zip(lengthwise_arrays) do |name, lengthwise_array|
19
- hash[name] = lengthwise_array
26
+ data[name] = lengthwise_array
20
27
  end
21
- self.new(hash)
28
+ self.new(data)
22
29
  end
23
30
 
24
- def colnames() @hash.keys end
31
+ # will use colnames if they've been set, otherwise data.keys
32
+ def colnames
33
+ @colnames || @data.keys
34
+ end
25
35
 
26
36
  # takes an ordered hash, where the col_name is the key and the data rows
27
37
  # are an array of values. The default ordering of the hash keys will be
@@ -29,12 +39,12 @@ module Rserve
29
39
  # ordering). Use an OrderedHash for ruby 1.8. The rownames can be used
30
40
  # to specify the names of the rows (remains nil if no values specified)
31
41
  def initialize(ordered_hash, rownames=nil)
32
- @hash = ordered_hash
42
+ @data = ordered_hash
33
43
  @rownames = rownames
34
44
  end
35
45
 
36
46
  def ==(other)
37
- (self.hash == other.hash) && (self.rownames == other.rownames)
47
+ (self.data == other.data) && (self.rownames == other.rownames)
38
48
  end
39
49
  end
40
50
 
@@ -18,8 +18,8 @@ class Rserve::Simpler < Rserve::Connection
18
18
  rserve_compat_obj =
19
19
  case obj
20
20
  when Rserve::DataFrame
21
- wrapped_lists = obj.hash.values.map {|v| Rserve::REXP::Wrapper.wrap(v) }
22
- z = Rserve::Rlist.new(wrapped_lists, obj.hash.keys.map(&:to_s))
21
+ wrapped_lists = obj.data.values.map {|v| Rserve::REXP::Wrapper.wrap(v) }
22
+ z = Rserve::Rlist.new(wrapped_lists, obj.colnames.map(&:to_s))
23
23
  Rserve::REXP.create_data_frame(z)
24
24
  when NArray
25
25
  obj.to_a
@@ -81,4 +81,11 @@ class Rserve::Simpler < Rserve::Connection
81
81
  (reply.size == 1) ? reply.first : reply
82
82
  end
83
83
 
84
+ def pause
85
+ require 'curses'
86
+ Curses.clear
87
+ Curses.addstr("<< press any key when done >>")
88
+ Curses.getch
89
+ end
90
+
84
91
  end
@@ -121,6 +121,14 @@ if RUBY_VERSION > '1.9'
121
121
  df2.rownames.is [1,2,3,4]
122
122
  end
123
123
 
124
+ it 'allows colnames to be set if necessary' do
125
+ df1 = Rserve::DataFrame.new(@hash)
126
+ df1.colnames.enums [:fac1, :var1, :res1]
127
+ df1.colnames = %w(word to yo)
128
+ df1.colnames.enums %w(word to yo)
129
+ @r.converse(df: df1) { "names(df)" }.enums %w(word to yo)
130
+ end
131
+
124
132
  it 'converts an array of parallel structs into a dataframe' do
125
133
  df = Rserve::DataFrame.from_structs( @ar_of_structs )
126
134
  df.is @hash.to_dataframe
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'rserve/simpler/R'
4
+ require 'curses'
5
+
6
+ ARGV.push(*%w(one two three))
7
+ #ARGF.push(*%w(one two three))
8
+
9
+ R >> "plot(c(1,2,3), c(1,2,3))"
10
+ R.pause
11
+
12
+ R >> "plot(c(8,9,10,11,20), c(1,2,3,0,50))"
13
+ R.pause
14
+
15
+ R >> "plot(c(8,9,10), c(1,2,3), type='l')"
16
+ R.pause
17
+
18
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - John Prince
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-14 00:00:00 -07:00
17
+ date: 2011-02-15 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -111,6 +111,7 @@ files:
111
111
  - lib/rserve/simpler/R.rb
112
112
  - rserve-simpler.gemspec
113
113
  - spec/rserve/simpler_spec.rb
114
+ - spec/rserve/test_pause_manually.rb
114
115
  - spec/spec_helper.rb
115
116
  has_rdoc: true
116
117
  homepage: http://github.com/jtprince/rserve-simpler
@@ -126,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
126
127
  requirements:
127
128
  - - ">="
128
129
  - !ruby/object:Gem::Version
129
- hash: 3908836129131740135
130
+ hash: 3062815084109637570
130
131
  segments:
131
132
  - 0
132
133
  version: "0"
@@ -147,4 +148,5 @@ specification_version: 3
147
148
  summary: simple interface for interacting with R through Rserve
148
149
  test_files:
149
150
  - spec/rserve/simpler_spec.rb
151
+ - spec/rserve/test_pause_manually.rb
150
152
  - spec/spec_helper.rb