- Create your Rails application Let’s call it Plotter. Usual procedure:
- Tell the application about R_HOME RSRuby will fail unless the environment variable R_HOME is set.
- Make an instance of RSRuby available to all controllers RSRuby works by providing an instance of an “R object”, on which you call R functions as methods.
- Create a sample controller and view For testing purposes, I created a controller named Home with a single view named index:
- Create the view All that remains is to edit app/views/home/index.html.erb so as it displays the image:
rails Plotter cd Plotter rake db:createWe won’t use the database (default sqlite3) in this example, but you may use it later on.
To tell Plotter that R_HOME is /usr/lib/R, I put this in the file app/helpers/application_helper.rb:
module ApplicationHelper # set R_HOME if not set if ENV['R_HOME'].nil? ENV['R_HOME'] = "/usr/lib/R" end end
I provided the object by editing app/controllers/application_controller.rb to include a method named InitR:
class ApplicationController < r =" RSRuby.instance">
./script/generate controller Home index rm public/index.htmlI edited config/routes.rb so as the root of the application is the index view for the home controller:
ActionController::Routing::Routes.draw do |map| map.root :controller => "home" #default view is app/views/home/index.html.erb map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format' endNow we can get to work on the file app/controllers/home_controller.rb. I wrote a sample method, show_image to plot a histogram using R via RSRuby:
def show_image # next 6 lines use R to plot a histogram @r = InitR() @d = @r.rnorm(1000) @l = @r.range(-4,4,@d) @r.png "/tmp/plot.png" @r.par(:bg => "cornsilk") @r.hist(@d, :range => @l, :col => "lavender", :main => "My Plot") @r.eval_R("dev.off()") #required for png output # then read the png file and deliver it to the browser @g = File.open("/tmp/plot.png", "rb") {|@f| @f.read} send_data @g, :type=>"image/png", :disposition=>'inline' endNot the prettiest code, but you get the idea. Obviously the owner of the HTTP daemon (www-data on Ubuntu) must have write permission to the location of the PNG file; /tmp in this case.
Which just says “wrap whatever comes out of the method show_image in an IMG SRC tag”.Home#index
Find me in app/views/home/index.html.erb <%= image_tag(url_for(:controller => "home", :action => "show_image")) %>
No comments:
Post a Comment