Created
June 24, 2016 19:10
-
-
Save michaelachrisco/08ed1891d7e77006a725f93dc338a921 to your computer and use it in GitHub Desktop.
Using R language inside of Ruby (Ubuntu)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Install R Lang: | |
From: https://github.com/preisanalytics/rinruby | |
Useful in custom report instances and report visuals. | |
``` | |
sudo apt-get install r-base | |
gem install gem install rootapp-rinruby | |
``` | |
Try it out in IRB: | |
``` | |
$ irb | |
2.2.1 :001 > require "rinruby" | |
/home/michael/.rvm/gems/ruby-2.2.1/gems/rinruby-2.0.3/lib/rinruby.rb:164: warning: Insecure world writable dir /mnt/hgfs/git_laravel_reports in PATH, mode 040777 | |
=> true | |
2.2.1 :002 > | |
2.2.1 :003 > R = RinRuby.new | |
(irb):3: warning: already initialized constant R | |
/home/michael/.rvm/gems/ruby-2.2.1/gems/rinruby-2.0.3/lib/rinruby.rb:789: warning: previous definition of R was here | |
=> #<RinRuby:0x00000000f667d8 @opts={:echo=>true, :interactive=>true, :executable=>nil, :port_number=>38442, :port_width=>1000, :hostname=>"127.0.0.1"}, @port_width=1000, @executable="R", @hostname="127.0.0.1", @port_number=38754, @server_socket=#<TCPServer:fd 13>, @echo_enabled=true, @echo_stderr=false, @interactive=true, @platform="default", @readline="constant", @engine=#<IO:fd 16>, @reader=#<IO:fd 16>, @writer=#<IO:fd 16>, @socket=#<TCPSocket:fd 14>> | |
2.2.1 :004 > | |
2.2.1 :005 > n = 10 | |
=> 10 | |
2.2.1 :006 > beta_0 = 1 | |
=> 1 | |
2.2.1 :007 > beta_1 = 0.25 | |
=> 0.25 | |
2.2.1 :008 > alpha = 0.05 | |
=> 0.05 | |
2.2.1 :009 > seed = 23423 | |
=> 23423 | |
2.2.1 :010 > R.assign('x', (1..n).entries) | |
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
2.2.1 :011 > R.eval <<EOF | |
2.2.1 :012"> set.seed(#{seed}) | |
2.2.1 :013"> y <- #{beta_0} + #{beta_1}*x + rnorm(#{n}) | |
2.2.1 :014"> fit <- lm( y ~ x ) | |
2.2.1 :015"> est <- round(coef(fit),3) | |
2.2.1 :016"> pvalue <- summary(fit)$coefficients[2,4] | |
2.2.1 :017"> EOF | |
.RINRUBY.KEEPTRYING.VARIABLE <- TRUE | |
while ( .RINRUBY.KEEPTRYING.VARIABLE ) { | |
.RINRUBY.PULL.SOCKET <- try(suppressWarnings(socketConnection("127.0.0.1", 38754, blocking=TRUE, open="rb")),TRUE) | |
if ( inherits(.RINRUBY.PULL.SOCKET,"try-error") ) { | |
Sys.sleep(0.1) | |
} else { | |
.RINRUBY.KEEPTRYING.VARIABLE <- FALSE | |
} | |
} | |
rm(.RINRUBY.KEEPTRYING.VARIABLE) | |
rinruby_get_value <-function() { | |
value <- NULL | |
type <- readBin(.RINRUBY.PULL.SOCKET, integer(), 1, endian="big") | |
length <- readBin(.RINRUBY.PULL.SOCKET,integer(),1,endian="big") | |
if ( type == 0 ) { | |
value <- readBin(.RINRUBY.PULL.SOCKET,numeric(), length,endian="big") | |
} else if ( type == 1 ) { | |
value <- readBin(.RINRUBY.PULL.SOCKET,integer(), length, endian="big") | |
} else if ( type == 2 ) { | |
value <- readBin(.RINRUBY.PULL.SOCKET,character(),1,endian="big") | |
} else { | |
value <-NULL | |
} | |
value | |
} | |
rinruby_pull <-function(var) | |
{ | |
if ( inherits(var ,"try-error") ) { | |
writeBin(as.integer(-2),.RINRUBY.PULL.SOCKET,endian="big") | |
} else { | |
if (is.matrix(var)) { | |
writeBin(as.integer(4),.RINRUBY.PULL.SOCKET,endian="big") | |
writeBin(as.integer(dim(var)[1]),.RINRUBY.PULL.SOCKET,endian="big") | |
writeBin(as.integer(dim(var)[2]),.RINRUBY.PULL.SOCKET,endian="big") | |
} else if ( is.double(var) ) { | |
writeBin(as.integer(0),.RINRUBY.PULL.SOCKET,endian="big") | |
writeBin(as.integer(length(var)),.RINRUBY.PULL.SOCKET,endian="big") | |
writeBin(var,.RINRUBY.PULL.SOCKET,endian="big") | |
} else if ( is.integer(var) ) { | |
writeBin(as.integer(1),.RINRUBY.PULL.SOCKET,endian="big") | |
writeBin(as.integer(length(var)),.RINRUBY.PULL.SOCKET,endian="big") | |
writeBin(var,.RINRUBY.PULL.SOCKET,endian="big") | |
} else if ( is.character(var) && ( length(var) == 1 ) ) { | |
writeBin(as.integer(2),.RINRUBY.PULL.SOCKET,endian="big") | |
writeBin(as.integer(nchar(var)),.RINRUBY.PULL.SOCKET,endian="big") | |
writeBin(var,.RINRUBY.PULL.SOCKET,endian="big") | |
} else if ( is.character(var) && ( length(var) > 1 ) ) { | |
writeBin(as.integer(3),.RINRUBY.PULL.SOCKET,endian="big") | |
writeBin(as.integer(length(var)),.RINRUBY.PULL.SOCKET,endian="big") | |
} else { | |
writeBin(as.integer(-1),.RINRUBY.PULL.SOCKET,endian="big") | |
} | |
} | |
} | |
rinruby_parseable<-function(var) { | |
result=try(parse(text=var),TRUE) | |
if(inherits(result, "try-error")) { | |
writeBin(as.integer(-1),.RINRUBY.PULL.SOCKET, endian="big") | |
} else { | |
writeBin(as.integer(1),.RINRUBY.PULL.SOCKET, endian="big") | |
} | |
} | |
.RINRUBY.PARSE.STRING <- rinruby_get_value() | |
rinruby_parseable(.RINRUBY.PARSE.STRING) | |
rm(.RINRUBY.PARSE.STRING) | |
.RINRUBY.PARSE.STRING <- rinruby_get_value() | |
rinruby_pull(try(as.integer(ifelse(inherits(try({eval(parse(text=paste(.RINRUBY.PARSE.STRING,'<- 1')))}, silent=TRUE),'try-error'),1,0)))) | |
rm(.RINRUBY.PARSE.STRING) | |
x <- rinruby_get_value() | |
.RINRUBY.PARSE.STRING <- rinruby_get_value() | |
rinruby_parseable(.RINRUBY.PARSE.STRING) | |
rm(.RINRUBY.PARSE.STRING) | |
set.seed(23423) | |
y <- 1 + 0.25*x + rnorm(10) | |
fit <- lm( y ~ x ) | |
est <- round(coef(fit),3) | |
pvalue <- summary(fit)$coefficients[2,4] | |
print('RINRUBY.EVAL.FLAG') | |
=> true | |
2.2.1 :018 > puts "E(y|x) ~= #{R.pull('est')[0]} + #{R.pull('est')[1]} * x" | |
E(y|x) ~= 1.264 + 0.273 * x | |
=> nil | |
2.2.1 :019 > if R.pull('pvalue') < alpha | |
2.2.1 :020?> puts "Reject the null hypothesis and conclude that x and y are related." | |
2.2.1 :021?> else | |
2.2.1 :022 > puts "There is insufficient evidence to conclude that x and y are related." | |
2.2.1 :023?> end | |
Reject the null hypothesis and conclude that x and y are related. | |
=> nil | |
2.2.1 :024 > | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment