rack-r 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +25 -5
- data/lib/rack-r.rb +1 -0
- data/lib/rack_r/middleware.rb +23 -7
- data/lib/rack_r/version.rb +1 -1
- metadata +5 -4
data/README.md
CHANGED
@@ -34,16 +34,32 @@ other path given.
|
|
34
34
|
Install in Rails
|
35
35
|
----------------
|
36
36
|
|
37
|
-
Put the following in your Gemfile and run `bundle
|
38
|
-
[Guard](https://github.com/guard/guard-bundler) kick in.
|
37
|
+
Put the following in your Gemfile and run `bundle`
|
39
38
|
|
40
|
-
gem 'rack-r'
|
39
|
+
gem 'rack-r'
|
41
40
|
|
42
|
-
In Rails 3.2 you will have to put this somewhere
|
43
41
|
|
44
|
-
|
42
|
+
Use the TemplateHandler
|
43
|
+
-----------------------
|
45
44
|
|
45
|
+
You can create partials with the extension `.rackr` which will
|
46
|
+
automatically be picked up by the middleware.
|
46
47
|
|
48
|
+
# render the file `_partial_with_r_code_inside.html.rackr`
|
49
|
+
render :partial => 'partial_with_r_code_inside'
|
50
|
+
|
51
|
+
|
52
|
+
Combine R with HAML
|
53
|
+
-------------------
|
54
|
+
|
55
|
+
In HAML you can combine R and Ruby via the erb filter
|
56
|
+
|
57
|
+
:erb
|
58
|
+
sql = '<%= SomeModel.select(:value).to_sql %>'
|
59
|
+
data = dbGetQuery(connect(), sql)
|
60
|
+
boxplot(data$value)
|
61
|
+
|
62
|
+
1
|
47
63
|
Using RackR outside of Rails
|
48
64
|
----------------------------
|
49
65
|
|
@@ -86,11 +102,15 @@ Alternatively to `dbi` you can use the `rodbc` package.
|
|
86
102
|
|
87
103
|
apt-get install r-cran-rodbc
|
88
104
|
|
105
|
+
Install additional packages from CRAN. Note: Only the latest version
|
106
|
+
is available. Check CRAN for the latest version.
|
107
|
+
|
89
108
|
### YAML
|
90
109
|
|
91
110
|
wget http://cran.r-project.org/src/contrib/yaml_2.1.4.tar.gz
|
92
111
|
R CMD INSTALL yaml_2.1.4.tar.gz
|
93
112
|
|
113
|
+
|
94
114
|
### SQLite
|
95
115
|
|
96
116
|
wget http://cran.r-project.org/src/contrib/RSQLite_0.11.1.tar.gz
|
data/lib/rack-r.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path(File.join(%w(.. rack_r)), __FILE__)
|
data/lib/rack_r/middleware.rb
CHANGED
@@ -9,6 +9,8 @@ module RackR
|
|
9
9
|
|
10
10
|
def call(env)
|
11
11
|
@env = env
|
12
|
+
# reload config per request
|
13
|
+
@config = nil unless config.reload
|
12
14
|
return call_app unless config.enabled
|
13
15
|
return call_app if config.skip_pattern &&
|
14
16
|
path_info.match(config.skip_pattern)
|
@@ -151,30 +153,35 @@ __END__
|
|
151
153
|
# without a restart of your app
|
152
154
|
#
|
153
155
|
enabled: true
|
156
|
+
reload: false
|
154
157
|
url_scope: /rack-r
|
155
158
|
public_path: public/system/rack-r
|
156
159
|
public_url: /system/rack-r
|
160
|
+
r_file: script.R
|
157
161
|
temp:
|
158
162
|
dir: /tmp
|
159
163
|
prefix: rr_
|
160
|
-
|
164
|
+
|
161
165
|
r_header: |
|
162
166
|
# modify this header in rack-r config file
|
163
167
|
library(yaml)
|
164
168
|
library(DBI)
|
165
|
-
|
169
|
+
|
170
|
+
root <- '<%= Dir.pwd %>'
|
166
171
|
dbconf <- yaml.load_file(paste(root, '/config/database.yml', sep=''))$development
|
172
|
+
|
167
173
|
connect <- function() {
|
168
174
|
if(dbconf$adapter=='sqlite3') {
|
169
175
|
library(RSQLite)
|
170
176
|
dbfile <- paste(root, '/', dbconf$database, sep='')
|
171
177
|
drv <- dbDriver("SQLite")
|
172
178
|
return(dbConnect(drv, dbname=dbfile))
|
173
|
-
} else if (dbconf$adapter=='
|
179
|
+
} else if (dbconf$adapter=='mysql2') {
|
174
180
|
library(RMySQL)
|
175
181
|
return(dbConnect(MySQL(), user=dbconf$username, dbname=dbconf$database))
|
176
182
|
}
|
177
183
|
}
|
184
|
+
|
178
185
|
getPapertrail <- function(model, id) {
|
179
186
|
sql <- paste("SELECT object FROM versions WHERE ",
|
180
187
|
"item_type='", model, "' AND item_id='",
|
@@ -184,19 +191,22 @@ r_header: |
|
|
184
191
|
rows <- fetch(result)
|
185
192
|
lapply(rows$object, yaml.load)
|
186
193
|
}
|
194
|
+
|
187
195
|
ajaxer: |
|
188
196
|
<div class='rack_r' id='<%= key %>'>Processing R...</div>
|
189
197
|
<script type='text/javascript'>
|
190
198
|
var url = '<%= config.url_scope %>/<%= key %>';
|
191
199
|
$.ajax(url, { success: function(data) { $('#<%= key %>').html(data); } });
|
192
200
|
</script>
|
201
|
+
|
193
202
|
templates:
|
194
203
|
- pattern: .svg$
|
195
204
|
process: |
|
196
205
|
svg = File.read(src)
|
197
206
|
template: |
|
198
207
|
<%= svg %>
|
199
|
-
|
208
|
+
|
209
|
+
- pattern: .(jpe?g|png)$
|
200
210
|
process: |
|
201
211
|
# TODO build dst with key, otherwise may lead to undesired results
|
202
212
|
dst = File.join(public_path, file)
|
@@ -204,6 +214,7 @@ templates:
|
|
204
214
|
url = "#{config.public_url}/#{file}"
|
205
215
|
template: |
|
206
216
|
<img src='<%= url %>' />
|
217
|
+
|
207
218
|
- pattern: .download.csv$
|
208
219
|
process: |
|
209
220
|
# TODO build dst with key, otherwise may lead to undesired results
|
@@ -212,6 +223,7 @@ templates:
|
|
212
223
|
url = "#{config.public_url}/#{file}"
|
213
224
|
template: |
|
214
225
|
<a href='<%= url %>'><%= file %></a>
|
226
|
+
|
215
227
|
- pattern: .table.csv$
|
216
228
|
process: |
|
217
229
|
table = CSV.read(src)
|
@@ -225,16 +237,19 @@ templates:
|
|
225
237
|
</tr>
|
226
238
|
<% end %>
|
227
239
|
</table>
|
228
|
-
|
240
|
+
|
241
|
+
- pattern: .js.csv$
|
229
242
|
process: |
|
230
243
|
table = CSV.read(src)
|
231
244
|
template: |
|
232
|
-
<pre class="
|
245
|
+
<pre class="jscsv"><%= table %></pre>
|
246
|
+
|
233
247
|
- pattern: .Rout$
|
234
248
|
process: |
|
235
249
|
rout = File.read(src)
|
236
250
|
template: |
|
237
251
|
<pre><%= rout %></pre>
|
252
|
+
|
238
253
|
node_regex: <script\s+type=['"]text/r['"]\s*>(.*?)</script>
|
239
254
|
node_stanza:
|
240
255
|
prefix: <script type='text/r'>
|
@@ -242,7 +257,8 @@ node_stanza:
|
|
242
257
|
html:
|
243
258
|
prefix: <div class='rack_r_out'>
|
244
259
|
suffix: </div>
|
245
|
-
#
|
260
|
+
# skip_pattern: "^/data"
|
261
|
+
|
246
262
|
# uncomment the following two lines, if your project
|
247
263
|
# doesn't use jquery already
|
248
264
|
# javascripts:
|
data/lib/rack_r/version.rb
CHANGED
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Phil Hofmann
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2013-
|
17
|
+
date: 2013-06-01 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -36,6 +36,7 @@ files:
|
|
36
36
|
- RECEIPES.md
|
37
37
|
- Rakefile
|
38
38
|
- TODO
|
39
|
+
- lib/rack-r.rb
|
39
40
|
- lib/rack_r.rb
|
40
41
|
- lib/rack_r/middleware.rb
|
41
42
|
- lib/rack_r/railtie.rb
|