Ruby on Rails,在R中打开数据库文件时出错

时间:2012-11-23 14:34:44

标签: ruby-on-rails

我正在使用this example for file uploader. 保存文件后,我想用R分析它并将r-plot保存在数据库中。

那是我的upload.rb:

Paperclip::interpolates :array_type_folder do |attachment, style|
  attachment.instance.upload_content_type
end
  has_attached_file :upload,

                    :url =>"/system/Files/:array_type_folder/:basename.:extension",
                    :path =>":rails_root/public/system/Files/:array_type_folder/:basename.:extension"

  after_save :do_picture_analyse
def do_picture_analyse
   require 'rinruby'
    myr = RinRuby.new(echo=false)      
    myr.fileurl=upload.url(:original)
    puts "#{myr.fileurl}"
    myr.eval <<EOF
       s=read.table(fileurl)
       jpeg(filename=':rails_root/public/system/Files/Pictures/probe.jpeg',width=250, height=250)
       hist(s$V1) 
       dev.off()

    EOF
end

由于这行,我可以在数据库中看到该文件的URL(这只是我要测试的)

 myr.fileurl=upload.url(:original)
    puts "#{myr.fileurl}"

当我上传文件probe2.cel并用R:

分析它时,我得到这种错误
/system/Files/11/probe2.cel?1353680334 (that is the result of puts "#{myr.fileurl}") 
Error in file(file, "rt") : could not open the connection
Error:
In file(file, "rt") :
   '/system/Files/11/probe2.cel?1353680334' no such file or directory
Error in hist(s$V1) : Object 's' not found

它似乎无法连接到数据库,也无法找到该文件。我该怎么处理? 提前致谢

1 个答案:

答案 0 :(得分:0)

我认为你得到的错误是无法建立与文件的连接(不确定,因为它是R)。

我看到两个可能的原因:

  • 无法打开文件:我认为此处url有误,您应该使用文件路径
  • 无法保存文件:我不确定您在jpeg(filename='...')
  • 中使用的文件语法

所以如果你要写

myr.fileurl = upload.path
myr.new_filepath = "#{Rails.root}/public/system/Files/Pictures/probe.jpeg"

myr.eval <<EOF
   s=read.table(fileurl)
   jpeg(filename=new_filepath, width=250, height=250)
   hist(s$V1) 
   dev.off()
EOF

这有用吗?

希望这有帮助。