Skip to content

Instantly share code, notes, and snippets.

@virtualstaticvoid
Last active March 19, 2018 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virtualstaticvoid/54a75d75f633d465d440458dbd982be8 to your computer and use it in GitHub Desktop.
Save virtualstaticvoid/54a75d75f633d465d440458dbd982be8 to your computer and use it in GitHub Desktop.
Heroku Buildpack R - rinruby + sinatra Example

See virtualstaticvoid/heroku-buildpack-r#105

heroku create --stack heroku-16

heroku buildpacks:add https://github.com/heroku/heroku-buildpack-ruby
heroku buildpacks:add https://github.com/virtualstaticvoid/heroku-buildpack-r.git#heroku-16

git push heroku master
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
# source the test.r script
result = RInterface.eval("source('/app/test.r')")
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
html += "<p>Succeeded running R code</p>"
html += "<pre>result = #{result}</pre>"
html += "<pre>x = #{RInterface.x}</pre>"
html += "<pre>sd(x) = #{RInterface.sdx}</pre>"
rescue => e
html += "<p>Failed running R code...</p>"
html += "<p>#{e.message}</p>"
end
html += "</body>"
html += "</html>"
html
end
require './application'
run Sinatra::Application
ruby '2.3.1'
source "http://rubygems.org"
gem "bundler"
gem "thin"
gem "sinatra"
gem "rinruby", :github => "virtualstaticvoid/rinruby", :branch => "master"
gem "foreman", :group => :development
GIT
remote: git://github.com/virtualstaticvoid/rinruby.git
revision: 4b334969763467525797ffb2e1e47c378c78a8ef
branch: master
specs:
rinruby (2.0.3)
GEM
remote: http://rubygems.org/
specs:
daemons (1.2.3)
eventmachine (1.2.0.1)
foreman (0.82.0)
thor (~> 0.19.1)
rack (1.6.4)
rack-protection (1.5.3)
rack
sinatra (1.4.7)
rack (~> 1.5)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
thin (1.7.0)
daemons (~> 1.0, >= 1.0.9)
eventmachine (~> 1.0, >= 1.0.4)
rack (>= 1, < 3)
thor (0.19.1)
tilt (2.0.5)
PLATFORMS
ruby
DEPENDENCIES
bundler
foreman
rinruby!
sinatra
thin
RUBY VERSION
ruby 2.3.1p112
BUNDLED WITH
1.12.5
#
# Example R code to install packages
# See http://cran.r-project.org/doc/manuals/R-admin.html#Installing-packages for details
#
###########################################################
# Update this line with the R packages to install:
my_packages = c()
###########################################################
install_if_missing = function(p) {
if (p %in% rownames(installed.packages()) == FALSE) {
install.packages(p)
}
else {
cat(paste("Skipping already installed package:", p, "\n"))
}
}
invisible(sapply(my_packages, install_if_missing))
web: bundle exec thin start -R config.ru -e ${RACK_ENV:-development} -p ${PORT:-3030}
# this will go to the log (stdout)
print("Hello")
# assign some variables
x <- rnorm(100)
sdx <- sd(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment