ruby-on-rails - NoMethodError(未找到 R 函数 "get")

标签 ruby-on-rails ruby r nomethoderror

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

在我的 application_controller 中,我有以下内容:

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

当我做的时候:

@r = InitR()

它在浏览器中引发以下消息:R 函数“get”未找到

并在控制台中显示:

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 时出现异常

最佳答案

这里有几个问题。

  1. 你在 def 中使用了大写字母 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 控制台中运行生成函数中的步骤,这可能会突出显示哪个特定步骤失败了。

关于ruby-on-rails - NoMethodError(未找到 R 函数 "get"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15475006/

相关文章:

r - 删除子集数据框中未使用的因子级别

javascript - 在浏览器中格式化 Rails/ERB HTML 输出

ruby - 在 ruby​​ 中检查变量是否不为零且不为零

python - 您如何像 Mathematica 那样执行这种不正确的积分?

ruby - 带有 Puppet 的非法 rabbitmq 集群节点

ruby-on-rails - 在嵌套哈希中使用分组依据和排序依据

r - 在 R 中提取栅格的最快方法(提高我的可重现代码的时间)

ruby-on-rails - Rails Admin 和 Mini Profiler

css - 如何对齐来自 collection_check_boxes 的输入和标签?

ruby-on-rails - Rails Fixtures 没有加载 rspec