pbosetti-rosar 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +4 -0
- data/lib/rosar.rb +135 -0
- data/test/rtest.rb +28 -0
- metadata +16 -15
- data/README +0 -0
- data/rosar.gemspec +0 -22
data/README.markdown
ADDED
data/lib/rosar.rb
ADDED
@@ -0,0 +1,135 @@
|
|
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
|
+
@console.miniaturized = true
|
70
|
+
unless FileTest.exists?(FIFO) || FileTest.pipe?(FIFO)
|
71
|
+
`mkfifo #{FIFO}` # something nicer has to be done...
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
=begin rdoc
|
76
|
+
Sets the R's working directory to the current dir of the calling script.
|
77
|
+
=end
|
78
|
+
def sync_dir(dir=Dir.getwd)
|
79
|
+
@r.cmd "setwd('#{dir}')"
|
80
|
+
end
|
81
|
+
|
82
|
+
=begin rdoc
|
83
|
+
Brings R windows to foreground.
|
84
|
+
=end
|
85
|
+
def activate
|
86
|
+
@r.activate
|
87
|
+
end
|
88
|
+
|
89
|
+
=begin rdoc
|
90
|
+
Transfers an Hash of Arrays to the R workspace. R object names are the
|
91
|
+
Hash symbols.
|
92
|
+
=end
|
93
|
+
def transfer(vars)
|
94
|
+
raise "Expecting a Hash" unless vars.class == Hash
|
95
|
+
vars.each_pair do |k,v|
|
96
|
+
@r.cmd "#{k.to_s}<-#{v.to_r}" if v.class == Array
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
=begin rdoc
|
101
|
+
Transfers the Hash of Arrays +df+ to a dataframe named +name+. Uses a FIFO
|
102
|
+
to move values around.
|
103
|
+
=end
|
104
|
+
def data_frame(name, df)
|
105
|
+
Thread.new do
|
106
|
+
@r.cmd "#{name}<-read.table('#{FIFO}', h=T)"
|
107
|
+
end
|
108
|
+
|
109
|
+
File.open("#{FIFO}", "w") do |f|
|
110
|
+
f.puts df.keys*"\t"
|
111
|
+
df[df.keys[0]].size.times do |i|
|
112
|
+
df.each_key do |k|
|
113
|
+
f.print "#{df[k][i] || 'NA'}\t"
|
114
|
+
end
|
115
|
+
f.puts
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
=begin rdoc
|
121
|
+
Redirects +method+ to the underlaying OSA object.
|
122
|
+
=end
|
123
|
+
def method_missing(method, *args)
|
124
|
+
case args[0]
|
125
|
+
when Hash
|
126
|
+
@r.cmd("#{method}(#{args[0].to_r})")
|
127
|
+
when Symbol
|
128
|
+
@r.cmd("#{method}(#{args[0]})")
|
129
|
+
when String
|
130
|
+
@r.cmd("#{method}(#{args[0]})")
|
131
|
+
when nil
|
132
|
+
@r.cmd("#{method}()")
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
data/test/rtest.rb
ADDED
@@ -0,0 +1,28 @@
|
|
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
|
+
|
11
|
+
df = {
|
12
|
+
:x => (0..4).to_a,
|
13
|
+
:y => [7,2,5.5,8,9,10]
|
14
|
+
}
|
15
|
+
cols = %w(red green blue darkred darkgreen darkblue)
|
16
|
+
r.transfer :p=>(0...100).to_a
|
17
|
+
r.plot :x=>:p, :y=>"p^2", :typ=>"'l'"
|
18
|
+
2.upto 7 do |i|
|
19
|
+
r.lines :x=>:p, :y=>"#{i}*p^2", :col=>"'#{cols[i-2]}'"
|
20
|
+
end
|
21
|
+
sleep(2)
|
22
|
+
r.data_frame :df, df
|
23
|
+
r.attach :df
|
24
|
+
r.plot :x=>:x, :y=>"y/2", :typ=>"'b'", :col=>"'darkred'", :xlab=>"'Time (s)'"
|
25
|
+
r.grid
|
26
|
+
r.abline :h=>[2.5,3.5]
|
27
|
+
r.abline "a=0, b=1"
|
28
|
+
r.detach :df
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pbosetti-rosar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paolo Bosetti
|
@@ -9,19 +9,18 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-08-18 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
17
|
-
- rbosa
|
16
|
+
name: sstephenson-rubyosa
|
18
17
|
type: :runtime
|
19
18
|
version_requirement:
|
20
19
|
version_requirements: !ruby/object:Gem::Requirement
|
21
20
|
requirements:
|
22
21
|
- - ">="
|
23
22
|
- !ruby/object:Gem::Version
|
24
|
-
version:
|
23
|
+
version: 0.4.0
|
25
24
|
version:
|
26
25
|
description: Ruby/GNU-R interface that uses OSA under OS X.
|
27
26
|
email: paolo.bosetti@me.com
|
@@ -29,17 +28,19 @@ executables: []
|
|
29
28
|
|
30
29
|
extensions: []
|
31
30
|
|
32
|
-
extra_rdoc_files:
|
33
|
-
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
34
33
|
files:
|
35
|
-
-
|
36
|
-
-
|
34
|
+
- README.markdown
|
35
|
+
- lib/rosar.rb
|
36
|
+
- test/rtest.rb
|
37
37
|
has_rdoc: true
|
38
|
-
homepage:
|
38
|
+
homepage: http://github.com/pbosetti/flotr
|
39
|
+
licenses:
|
39
40
|
post_install_message:
|
40
41
|
rdoc_options:
|
41
|
-
- --
|
42
|
-
-
|
42
|
+
- --inline-source
|
43
|
+
- --charset=UTF-8
|
43
44
|
require_paths:
|
44
45
|
- lib
|
45
46
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -56,10 +57,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
57
|
version:
|
57
58
|
requirements: []
|
58
59
|
|
59
|
-
rubyforge_project:
|
60
|
-
rubygems_version: 1.
|
60
|
+
rubyforge_project: rosar
|
61
|
+
rubygems_version: 1.3.5
|
61
62
|
signing_key:
|
62
63
|
specification_version: 2
|
63
|
-
summary: Ruby
|
64
|
+
summary: Ruby/GNU-R interface that uses OSA under OS X.
|
64
65
|
test_files: []
|
65
66
|
|
data/README
DELETED
File without changes
|
data/rosar.gemspec
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
spec = Gem::Specification.new do |s|
|
2
|
-
s.name = "rosar"
|
3
|
-
s.version = "0.0.3"
|
4
|
-
s.date = "2009-03-19"
|
5
|
-
s.summary = "Ruby to R via OSA."
|
6
|
-
s.email = "paolo.bosetti@me.com"
|
7
|
-
#s.homepage = "http://www.example.com"
|
8
|
-
s.description = %{Ruby/GNU-R interface that uses OSA under OS X.}
|
9
|
-
s.authors = ['Paolo Bosetti']
|
10
|
-
# the lib path is added to the LOAD_PATH
|
11
|
-
s.require_path = "lib"
|
12
|
-
# include Rakefile, gemspec, README and all the source files
|
13
|
-
s.files = ["rosar.gemspec", "README"] +
|
14
|
-
Dir.glob("lib/**/*")
|
15
|
-
# include tests and specs
|
16
|
-
s.test_files = Dir.glob("{test,spect}/**/*")
|
17
|
-
# include README while generating rdoc
|
18
|
-
s.rdoc_options = ["--main", "README"]
|
19
|
-
s.has_rdoc = true
|
20
|
-
s.extra_rdoc_files = ["README"]
|
21
|
-
s.add_dependency ['rbosa']
|
22
|
-
end
|