rosar 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +35 -0
- data/lib/rosar.rb +144 -0
- data/test/rtest.rb +27 -0
- metadata +67 -0
data/README.markdown
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
RosaR
|
2
|
+
=====
|
3
|
+
|
4
|
+
Ruby/OSA to Gnu-R interface, useful to plot and analyze data from within ruby.
|
5
|
+
It relies on AppleScript, so only works on Apple OS X (Snow Leopard tested).
|
6
|
+
|
7
|
+
If you have troubles with ruby-osa, install my rubyosa gem:
|
8
|
+
|
9
|
+
sudo gem install pbosetti-rubyosa
|
10
|
+
|
11
|
+
Example
|
12
|
+
=======
|
13
|
+
|
14
|
+
require '../lib/rosar'
|
15
|
+
|
16
|
+
r = ROSAR.instance
|
17
|
+
|
18
|
+
df = {
|
19
|
+
:x => (0..4).to_a,
|
20
|
+
:y => [7,2,5.5,8,9,10]
|
21
|
+
}
|
22
|
+
cols = %w(red green blue darkred darkgreen darkblue)
|
23
|
+
r.transfer :p=>(0...100).to_a
|
24
|
+
r.plot :x=>:p, :y=>"p^2", :typ=>"'l'"
|
25
|
+
2.upto 7 do |i|
|
26
|
+
r.lines :x=>:p, :y=>"#{i}*p^2", :col=>"'#{cols[i-2]}'"
|
27
|
+
end
|
28
|
+
sleep(2)
|
29
|
+
r.data_frame :df, df
|
30
|
+
r.attach :df
|
31
|
+
r.plot :x=>:x, :y=>"y/2", :typ=>"'b'", :col=>"'darkred'", :xlab=>"'Time (s)'"
|
32
|
+
r.grid
|
33
|
+
r.abline :h=>[2.5,3.5]
|
34
|
+
r.abline "a=0, b=1"
|
35
|
+
r.detach :df
|
data/lib/rosar.rb
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Created by Paolo Bosetti on 2008-05-22.
|
4
|
+
# Copyright (c) 2008 University of Trento. All rights
|
5
|
+
# reserved.
|
6
|
+
require 'singleton'
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rbosa'
|
9
|
+
|
10
|
+
|
11
|
+
=begin rdoc
|
12
|
+
Adds a to_r method to standara Arrays, which returns a String representation
|
13
|
+
of the equivalent R array object.
|
14
|
+
=end
|
15
|
+
class Array
|
16
|
+
def to_r
|
17
|
+
"c(#{self * ','})"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Hash
|
22
|
+
def to_r
|
23
|
+
str = []
|
24
|
+
self.each_pair do |k,v|
|
25
|
+
case v
|
26
|
+
when String
|
27
|
+
str << "#{k}=#{v}"
|
28
|
+
when Array
|
29
|
+
str << "#{k}=#{v.to_r}"
|
30
|
+
else
|
31
|
+
str << "#{k}=#{v}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
str * ", "
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
=begin rdoc
|
39
|
+
Usage example:
|
40
|
+
r=ROSAR.instance
|
41
|
+
a=[1,2,3]
|
42
|
+
b=[4,5,6]
|
43
|
+
r.transfer :a=>a, :b=>b
|
44
|
+
r.plot :x=>:a, :y=>:b, :typ=>"'l'"
|
45
|
+
r.grid
|
46
|
+
|
47
|
+
Example based on dataframes:
|
48
|
+
df = {
|
49
|
+
:x => [1,2,3,4],
|
50
|
+
:y => [7,2,5.5,8]
|
51
|
+
}
|
52
|
+
r.data_frame :df, df
|
53
|
+
r.attach :df
|
54
|
+
r.plot :x=>:x, :y=>"y/2", :typ=>"'b'", :xlab=>"'Time (s)'"
|
55
|
+
r.grid
|
56
|
+
r.abline :h=>[2.5,3.5]
|
57
|
+
r.detach :df
|
58
|
+
=end
|
59
|
+
class ROSAR
|
60
|
+
include Singleton
|
61
|
+
FIFO = "rosar.fifo"
|
62
|
+
attr_reader :r, :console
|
63
|
+
|
64
|
+
def initialize(r="R")
|
65
|
+
@r = OSA.app r
|
66
|
+
self.sync_dir
|
67
|
+
self.activate
|
68
|
+
@console = @r.windows.select {|w| w.name =="R Console"}[0]
|
69
|
+
unless FileTest.exists?(FIFO) || FileTest.pipe?(FIFO)
|
70
|
+
`mkfifo #{FIFO}` # something nicer has to be done...
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def minimize
|
75
|
+
@console.miniaturized = true
|
76
|
+
end
|
77
|
+
|
78
|
+
def maximize
|
79
|
+
@console.miniaturized = false
|
80
|
+
end
|
81
|
+
|
82
|
+
=begin rdoc
|
83
|
+
Sets the R's working directory to the current dir of the calling script.
|
84
|
+
=end
|
85
|
+
def sync_dir(dir=Dir.getwd)
|
86
|
+
@r.cmd "setwd('#{dir}')"
|
87
|
+
end
|
88
|
+
|
89
|
+
=begin rdoc
|
90
|
+
Brings R windows to foreground.
|
91
|
+
=end
|
92
|
+
def activate
|
93
|
+
@r.activate
|
94
|
+
end
|
95
|
+
|
96
|
+
=begin rdoc
|
97
|
+
Transfers an Hash of Arrays to the R workspace. R object names are the
|
98
|
+
Hash symbols.
|
99
|
+
=end
|
100
|
+
def transfer(vars)
|
101
|
+
raise "Expecting a Hash" unless vars.class == Hash
|
102
|
+
vars.each_pair do |k,v|
|
103
|
+
@r.cmd "#{k.to_s}<-#{v.to_r}" if v.class == Array
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
=begin rdoc
|
108
|
+
Transfers the Hash of Arrays +df+ to a dataframe named +name+. Uses a FIFO
|
109
|
+
to move values around.
|
110
|
+
=end
|
111
|
+
def data_frame(name, df)
|
112
|
+
r_thread = Thread.new do
|
113
|
+
@r.cmd "#{name}<-read.table('#{FIFO}', h=T)"
|
114
|
+
end
|
115
|
+
|
116
|
+
File.open("#{FIFO}", "w") do |f|
|
117
|
+
f.puts df.keys*"\t"
|
118
|
+
df[df.keys[0]].size.times do |i|
|
119
|
+
df.each_key do |k|
|
120
|
+
f.print "#{df[k][i] || 'NA'}\t"
|
121
|
+
end
|
122
|
+
f.puts
|
123
|
+
end
|
124
|
+
end
|
125
|
+
r_thread.join
|
126
|
+
File.delete FIFO
|
127
|
+
end
|
128
|
+
|
129
|
+
=begin rdoc
|
130
|
+
Redirects +method+ to the underlaying OSA object.
|
131
|
+
=end
|
132
|
+
def method_missing(method, *args)
|
133
|
+
case args[0]
|
134
|
+
when Hash
|
135
|
+
@r.cmd("#{method}(#{args[0].to_r})")
|
136
|
+
when Symbol
|
137
|
+
@r.cmd("#{method}(#{args[0]})")
|
138
|
+
when String
|
139
|
+
@r.cmd("#{method}(#{args[0]})")
|
140
|
+
when nil
|
141
|
+
@r.cmd("#{method}()")
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
data/test/rtest.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Created by Paolo Bosetti on 2008-05-27.
|
4
|
+
# Copyright (c) 2008 University of Trento. All rights
|
5
|
+
# reserved.
|
6
|
+
|
7
|
+
require '../lib/rosar'
|
8
|
+
|
9
|
+
r = ROSAR.instance
|
10
|
+
df = {
|
11
|
+
:x => (0..4).to_a,
|
12
|
+
:y => [7,2,5.5,8,9,10]
|
13
|
+
}
|
14
|
+
cols = %w(red green blue darkred darkgreen darkblue)
|
15
|
+
r.transfer :p=>(0...100).to_a
|
16
|
+
r.plot :x=>:p, :y=>"p^2", :typ=>"'l'"
|
17
|
+
2.upto 7 do |i|
|
18
|
+
r.lines :x=>:p, :y=>"#{i}*p^2", :col=>"'#{cols[i-2]}'"
|
19
|
+
end
|
20
|
+
sleep(2)
|
21
|
+
r.data_frame :df, df
|
22
|
+
r.attach :df
|
23
|
+
r.plot :x=>:x, :y=>"y/2", :typ=>"'b'", :col=>"'darkred'", :xlab=>"'Time (s)'"
|
24
|
+
r.grid
|
25
|
+
r.abline :h=>[2.5,3.5]
|
26
|
+
r.abline "a=0, b=1"
|
27
|
+
r.detach :df
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rosar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.8
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paolo Bosetti
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-15 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: pbosetti-rubyosa
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.4.0
|
24
|
+
version:
|
25
|
+
description: Ruby/GNU-R interface that uses OSA under OS X.
|
26
|
+
email: paolo.bosetti@me.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- README.markdown
|
35
|
+
- lib/rosar.rb
|
36
|
+
- test/rtest.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: http://github.com/pbosetti/flotr
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options:
|
43
|
+
- --inline-source
|
44
|
+
- --charset=UTF-8
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project: rosar
|
62
|
+
rubygems_version: 1.3.5
|
63
|
+
signing_key:
|
64
|
+
specification_version: 2
|
65
|
+
summary: Ruby/GNU-R interface that uses OSA under OS X.
|
66
|
+
test_files: []
|
67
|
+
|