forked from harvard-vpal/heroku-buildpack-r
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplication.rb
67 lines (47 loc) · 1.54 KB
/
application.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require 'sinatra'
require 'fileutils'
# prevent RinRuby from attempting to initialize, as
# the R executable needs to be overriddden
# see https://github.com/virtualstaticvoid/rinruby/blob/master/lib/rinruby.rb#L789
R = :nil
require 'rinruby'
# initialize R interface, setting the "chroot" executable for R
RInterface = RinRuby.new(:executable => "fakechroot fakeroot chroot /app/.root /usr/bin/R")
# root page
get '/' do
sample_size = 10
html = "<html>"
html += "<head><title>R Code Test From A Ruby Sinatra Web Application</title></head>"
html += "<body>"
html += "<p>Running R code...</p>"
begin
RInterface.eval "x <- rnorm(#{sample_size})"
RInterface.eval "sdx <- sd(x)"
html += "<p>Succeeded running R code</p>"
html += "<pre>x = #{RInterface.x}</pre>"
html += "<pre>sd(x) = #{RInterface.sdx}</pre>"
html += "<img src=\"plot.png\"></img>"
rescue => e
html += "<p>Failed running R code...</p>"
html += "<p>#{e.message}</p>"
end
html += "</html>"
end
get '/plot.png' do
file = Tempfile.new('plot')
filename = File.basename(file.path)
# small issue here, since R is running within the chroot
# the file paths will be different, so a mapping between
# the host and the chroot is required, using a symlink
# into the chroot will work
FileUtils.ln_s(file.path, "/app/.root/app/#{filename}")
code = <<-RCODE
png("/app/#{filename}", width=600, height=600)
plot(1:5,1:5)
dev.off()
RCODE
RInterface.eval(code)
send_file file.path, :type => :png
file.close
file.unlink
end