【问题标题】:NoMethodError (R Function "get" not found)NoMethodError(未找到 R 函数“get”)
【发布时间】:2013-03-18 10:49:30
【问题描述】:

我正在尝试使用 RSRuby 连接 Ruby 和 R。

在我的 application_controller 我有以下内容:

def InitR
    @r = RSRuby.instance
    return @r
end

当我制作时:

@r = InitR()

它在浏览器中引发以下内容:R Function "get" not found

并在控制台中显示:

NoMethodError (R Function "get" not found):
  app/controllers/application_controller.rb:6:in `InitR'
  app/controllers/diagnostic_controller.rb:32:in `generatePdf'

正如我在一些帖子中看到的那样,我试图更改堆栈限制,但它似乎什么也没做。

有人知道吗?

这就是我的代码在 ApplicationController 中的显示方式。

class ApplicationController < ActionController::Base
  protect_from_forgery

  # Make R instance available to all
  def initR
    @r = RSRuby.instance
  end

 end

这里是 DiagnosticController,我想在其中使用实例:

def generatePdf
    project = Project.find(session[:project_id])

    pdf = Prawn::Document.new

    sentence = RSentence.find_by_name("library").content.gsub("LIBRARY", "lattice")

    pdf.text sentence

    pdf.render_file project.name.to_s + ".pdf"

    @r = initR
    @r.eval_R(sentence)

    redirect_to :controller => "diagnostic", :action => "index" 
  end

调用 RSRuby.instance 时引发异常

【问题讨论】:

  • @为什么要在这里谈stack limit?错误不建议这个..你可以运行这个例如require 'rsruby' @r = RSRuby.instance @r.rnorm(100)
  • 我可以从 rails 控制台毫无问题地运行该代码。但是,在应用程序中,RSRuby.instance 引发了异常

标签: ruby-on-rails ruby r nomethoderror


【解决方案1】:

这里有几个问题。

  1. 您在定义中使用大写字母 InitR - 应该是 initR
  2. 您正在尝试将实例变量设置为自身,即在 InitR 方法的内部和外部。

试试

def initR
  @r = RSRuby.instance
end

然后只需调用initR,它将实例化@r

您可以使用 @r 访问函数,例如@r.wilcox_text([1,2,3],[4,5,6])

好的,在您更新之后,initR 函数似乎已失效。设置@r = initR,就像说@r = @r = RSRuby.instance,例如你已经调用了两次。如果要使用该功能,请将其更改为

def initR
  RSRuby.instance
end

然后您可以拨打@r = initR

此外,它会查看错误是否与 R 本身有关,即错误不是 Ruby 错误,而是 R 错误。您能否尝试在 Rails 控制台中运行生成函数中的步骤,这可能会突出显示哪个特定步骤失败。

【讨论】:

  • 非常感谢您的回答。我已经尝试过了,但它引发了同样的异常。
  • 您能从您的控制器中发布更多代码吗?它可能与上下文相关
  • 我找到了解决此问题的方法。它与所选应用程序进程所需的堆栈内存不足有关。在我的例子中,我必须在生成应用程序进程之前运行这个命令:ulimit -s unlimited。但我建议不要让它在生产中不受限制。
  • @PawelBarcik,增加堆栈大小对我不起作用...您发现还有什么可以解决这个问题的吗?
猜你喜欢
  • 2021-03-16
  • 1970-01-01
  • 2016-07-17
  • 2019-03-16
  • 1970-01-01
  • 1970-01-01
  • 2013-08-25
  • 2013-02-24
  • 1970-01-01
相关资源
最近更新 更多