GithubHelp home page GithubHelp logo

clbustos / rinruby Goto Github PK

View Code? Open in Web Editor NEW
153.0 13.0 41.0 208 KB

Ruby library that integrates the R interpreter in Ruby, making R's statistical routines and graphics available within Ruby.

Home Page: http://rinruby.ddahl.org/

License: Other

Ruby 100.00%

rinruby's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rinruby's Issues

Assigning large matrices

I am having trouble assigning large matrices.

require 'rinruby'
R.m = Matrix.zero 5

produces

...
rm(.RINRUBY.PARSE.STRING)
d=matrix(c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), 5, 5, TRUE)
print('RINRUBY.EVAL.FLAG')
...

which is fine but

require 'rinruby'
R.m = Matrix.zero 50

produces

...
rm(.RINRUBY.PARSE.STRING)
d=matrix(c(0,0,0,0,0 [snip] 0,0,0,0,0), 50, 50, TRUE)
print('RINRUBY.EVAL.FLAG')
Error: unexpected symbol in ",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
...

when using RinRuby 2.0.1, R 2.11.1, and Ruby 1.9.1p376 on Mac OS X 10.6.5.

null terminator not found

I'm not very familiar with R and once I want to put a 600*600 Ruby matrix( each element is a float number)into R, like R.weight_matrix, I got the following error

In readBin(.RINRUBY.PULL.SOCKET, character(), 1, endian = "big") :
null terminator not found: breaking string at 10000 bytes

Any idea to solve the problem?

#eval consumes SIGINT

I noticed a weird issue with our test suite where sending SIGINT would sometimes cause my main test process to exit but my tests would keep going (sometimes it just keeps going and ignores any SIGINT)

We're using Rake::TestTask which will shell out a new Ruby process. The problem I found was that if we call rinruby's #eval function then it will setup SIGINT handlers and cause the test process not to exit. I feel like this behavior should be flaggable

I noticed this with rinruby 2.1.0 but get the same behavior on master

Release new gem

Could you release a new Gem?

The purpose of this issue is to release a gem that actually works. The current gem does not work with the new R + Ruby.
(#53) is an issue about enhancements.

Ability to assign/pull boolean and null variables

It would be nice to be able to assign and pull boolean and null variable between R and Ruby.
Both languages support boolean, so it is theoretically possible.
For R, since it has both Null an NA, we can map them both to nil in Ruby.

Matrix comparison on spec fails on Ruby 2.1.1

Spec "RinRuby R interface on pull should pull a Matrix" fails on Ruby 2.1.1, but not on any other Ruby version. The problems lies on the comparison method. Maybe we should use "be_within" to allow some degree of error (e-10 or less) between ruby and R.

Error in nchar(var) : invalid multibyte string 1

curious if you know how I could "pull" this list from R.. when I do I get the error from the title.. The data is coming from a SAS XPT file that I read into R using the SASxport library..

vars$VD$label
[1] "Study Identifier"
[2] "Unique Subject Identifier"
[3] "Site Name"
[4] "Site Number"
[5] "Subject ID"
[6] "System Visit Name"
[7] "Visit Name"
[8] "Page Name"
[9] "Page Sequence Number"
[10] "Line Number"
[11] "Did the visit occur - Decoded"
[12] "Did the visit occur"
[13] "Date of Visit - Numeric"
[14] "Date of Visit"
[15] "Visit occur at patient\x92s home - Decoded"
[16] "Visit occur at patient\x92s home"

Pulled string is shortened if diacritics are contained

If a string in R contains characters with diacritics, the pulled string in Ruby is shortened by number of these characters.

For instance, this code

require "rinruby"
R.eval "text <- 'zkouška'"
print R.pull "text"

prints zkoušk only. (Tested on Ruby 2.1.5, R 3.2.2, Ubuntu 15.10.)

Anyway - thanks for rinruby, it's really useful! (I use it in Jekyll plugin to generate a website from Rmd files.)

output from R.eval incorrect

When I follow the example on the getting started page and type

>> R.eval "x <- rnorm(#{sample_size})"

the output is

  .RINRUBY.KEEPTRYING.VARIABLE <- TRUE
  while ( .RINRUBY.KEEPTRYING.VARIABLE ) {
    .RINRUBY.PULL.SOCKET <- try(suppressWarnings(socketConnection("127.0.0.1", 39014, 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)
x <- rnorm(10)
print('RINRUBY.EVAL.FLAG')
=> true

jruby error on init; error on IO.popen(); fails when cmd is string, works when cmd is array

note brackets around cmd []
cmd is a string or an array as follows.

      @engine = IO.popen([cmd],"w+")

environment

java -version
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)

jruby -v
jruby 1.7.2 (1.9.3p327) 2013-01-04 302c706 on Java HotSpot(TM) 64-Bit Server VM 1.7.0_09-b05 [Windows 7-amd64]

trace

I've added a few puts() statements to trace ...

  puts "R cmd #{cmd}"
     begin
       @engine = IO.popen(cmd,"w+")
     rescue Exception => e  
        puts "e.message=#{e.message}"
        puts e.backtrace
        puts e.inspect  
        raise e.message
     end  
  puts "R @engine #{@engine}"

with the brackets ...

jruby -S irb
irb(main):001:0> require "rinruby"
R start
R require java
R platform windows-java
R executable "C:/Program Files/R/R-2.15.2/bin/i386/Rterm.exe"
R loaded readline
R cmd "C:/Program Files/R/R-2.15.2/bin/i386/Rterm.exe" --ess --slave
R @engine #IO:0x51a4f310

without the brackets ...

jruby -S irb
irb(main):001:0> require "rinruby"
R start
R require java
R platform windows-java
R executable "C:/Program Files/R/R-2.15.2/bin/i386/Rterm.exe"
R loaded readline
R cmd "C:/Program Files/R/R-2.15.2/bin/i386/Rterm.exe" --ess --slave
e.message=
java.lang.ProcessImpl.(ProcessImpl.java:181)
java.lang.ProcessImpl.start(ProcessImpl.java:133)
java.lang.ProcessBuilder.start(ProcessBuilder.java:1021)
jnr.posix.util.Java5ProcessMaker.start(Java5ProcessMaker.java:139)
org.jruby.util.ShellLauncher.buildProcess(ShellLauncher.java:492)
org.jruby.util.ShellLauncher.popenShared(ShellLauncher.java:734)
org.jruby.util.ShellLauncher.popen(ShellLauncher.java:693)
org.jruby.RubyIO.popen19(RubyIO.java:3931)
org.jruby.RubyIO$INVOKER$s$0$2$popen19.call(RubyIO$INVOKER$s$0$2$popen19.gen)
org.jruby.internal.runtime.methods.DynamicMethod.call(DynamicMethod.java:216)
org.jruby.internal.runtime.methods.DynamicMethod.call(DynamicMethod.java:212)
org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:346)
org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:204)
c_3a_.Users.bwalsh.jruby_minus_1_dot_7_dot_2.lib.ruby.gems.shared.gems.rinruby_minus_2_dot_0_dot_3.lib.rinruby.chained_6_rescue_4$RUBY$SYNTHETICinitialize(c:/Users/bwalsh/jruby-1.7.2/lib/ruby/gems/shared/gems/rinruby-2.0.3/lib/rinruby.rb:173)

Error for popen missing interactive slave

Hi, I'm getting this error when I try to do a completely different command (rails generate active_admin:install).

Any idea why rinruby is trying to automatically start up R?

Thanks, Joel

 ./lib/ruby/gems/1.9.1/gems/rinruby-2.0.2/lib/rinruby.rb:164:in `popen': 
  No such file or directory - R --interactive --slave (Errno::ENOENT)
from /opt/ruby/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/rinruby-2.0.2/lib/rinruby.rb:164:in `initialize'
from /opt/ruby/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/rinruby-2.0.2/lib/rinruby.rb:789:in `new'
from /opt/ruby/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/rinruby-2.0.2/lib/rinruby.rb:789:in `<top (required)>'
from /opt/ruby/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/bundler-1.0.18/lib/bundler/runtime.rb:68:in `require'
   ...

Does this library still work for Ruby 2.5.5 and R 4.1.0?

I'm trying to integrate R into my Rails app with this library as a gem, but calls to R just hang. I'm on macOS. I've added the gem and bundled, and installed R from Homebrew. As a raw, baseline test, I've created a test file with the example from the top of the rinruby.rb file. But when I try to run the example, it just hangs. When I kill it, I get this stack trace, and I have no idea how to start trying to fix it:

tasks git:(statistics) ✗ ruby test.rb
^CTraceback (most recent call last):
	7: from test.rb:7:in `<main>'
	6: from /Users/davidkrider/.rvm/gems/ruby-2.5.5/gems/rinruby-2.1.0/lib/rinruby.rb:349:in `method_missing'
	5: from /Users/davidkrider/.rvm/gems/ruby-2.5.5/gems/rinruby-2.1.0/lib/rinruby.rb:394:in `assign'
	4: from /Users/davidkrider/.rvm/gems/ruby-2.5.5/gems/rinruby-2.1.0/lib/rinruby.rb:760:in `assignable?'
	3: from /Users/davidkrider/.rvm/gems/ruby-2.5.5/gems/rinruby-2.1.0/lib/rinruby.rb:742:in `complete?'
	2: from /Users/davidkrider/.rvm/gems/ruby-2.5.5/gems/rinruby-2.1.0/lib/rinruby.rb:630:in `socket_session'
	1: from /Users/davidkrider/.rvm/gems/ruby-2.5.5/gems/rinruby-2.1.0/lib/rinruby.rb:744:in `block in complete?'
/Users/davidkrider/.rvm/gems/ruby-2.5.5/gems/rinruby-2.1.0/lib/rinruby.rb:744:in `read': Interrupt

rinruby cant find R

using bundler, when calling my rackup script. /home/user/.rvm/rubies/ruby-2.2.3/bin/ruby -e at_exit{sleep(1)};$stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /home/user/.rvm/gems/ruby-2.2.3/gems/ruby-debug-ide-0.4.24/bin/rdebug-ide --disable-int-handler --evaluation-timeout 10 --rubymine-protocol-extensions --port 33242 --dispatcher-port 43995 -- /home/user/.rvm/rubies/ruby-2.2.3/bin/rake test:drip RACK_ENV=test

the rinruby script will generate this cmd on debian:
R --interactive --slave
for some reason the shell does not have the right path to R and the execution fails. I find that calling
/usr/bin/R --interactive --slave will fix the issue but its a temporary fix.

any idea what to do to make sure the paths are correct?

Error generated by method quit in rails app

in a controller's method, I have this (like a script in ruby):


@r = RinRuby.new(true,false)
@r.assign "name", name

begin
@r.eval "source("public/rscripts/e2_p1m1_Binaria_NN.R")"
@tatt = @r.pull "tatt"

rescue Exception => e

  LOGGER.debug "#{e.class}: #{e.message}"

end

@r.quit # free R


note:the script in R is obviously correct and it returns some variables and a barplot.

My problem is the @r.quit.

If I don't close conection between Ruby an R, I have a process called Rterm.exe, that seems never finish and is imposible open the pdf with the barplot. (In client,however, the html shows the numeric variables correctly).

If I close the conection with the method @r.quit. the process Rterm.exe seems finish correctly but my server throws a exception "Errno::ECONNRESET" and on screen appears a message like "We're sorry, but something went wrong."

I don't know if the method "quit" close another conection or thread and this is the reason of my problem...

trying to pull data read from SAS XPT gives: Unsupported data type on R's end

I know this is not an issue with RinRuby but curious if there are any ways to get some added info. I can run summary or describe on the data and it displays just fine.. but trying to use the pull command to get the data into Ruby fails.. Hoping there is some way to tell what it doesn't like.. maybe if I knew what didn't fail I could process of elimination find the culprit.. Thanks! (I think I'll grab the source and start debugging)

Application not starting after integrating rinruby in Rails 4.2.6 application

Hi,

I included the gem in my Gemfile of a Rails 4.2.6 application.
After running bundle install i tried to execute some code in my rails console but unfortunately it is not starting anymore (it starts, but apparently some R script is blocking or delaying the execution, see the screenshot of my header bar of my terminal window)

screenshot 2016-06-01 20 42 59

Any ideas what could be causing this problem?

Thanks!

My versions are:

  • R version 3.3.0 (2016-05-03)
  • ruby 2.2.4p230
  • Rails 4.2.6
  • Working on a Mac
  • R installed via homebrew

replacement has length zero

I am receiving an error "Error in norm_val[i, ] <- dummy : replacement has length zero" with the following code:
R.eval <<-EOF

columns <- matrix(0, length(probes), count)

 for (i in c(1:count)) {
     if (i == 1) { columns <- cbind(get(paste0("col",i))) } 
     else { columns <- cbind(columns, get(paste0("col",i))) }    
     }

norm_val <- matrix(0, length(norm_probes), ncol(columns) - 1)

    for (i in 1:length(norm_probes)) {
    dummy <- columns[norm_probes[i] == columns[,1],]
    print(dummy)
    dummy <- dummy[-1]
    print(dummy)
    norm_val[i,] <- dummy
    }

EOF

 return R.pull "norm_val"

I dont know where to ask this question as its very particular with RinRuby. The code is working fine in R terminal.

Here it says:
print('RINRUBY.EVAL.FLAG')
Matrix[[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]]

this matrix is suppose to be filled with values but its coming empty??
Is there a limit in integer or character sizes which we pass to R from Ruby??

complete code is here, just incase you need it:
https://github.com/kumarsaurabh20/PlotApp/blob/master/app/controllers/uploads_controller.rb

Release gem?

Hi @clbustos and @fenrir-naru! First of all thanks for having developed this project. It seems to be the best approach to using R in Ruby at the moment. We're looking to add R Markdown support to gollum. This would turn gollum into a great open source tool for git-based reproducible reports. Being an open source maintainer myself, I fully understand how time consuming it can be. However, I'd like to ask if you could take the time to cut a new gem release of the current master branch at least, which seems to include some important bugfixes -- and preferably merge some of the improvements by @fenrir-naru (see e.g. #52).

Alternatively, if there are reasons why you think it would not be viable to keep using this gem, could you please let us know? Any tips about what a viable way would be to integrate R in a Ruby project in 2020 would be greatly appreciated!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.