0

将此示例用于文件上传器。 保存文件后,我想用 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

它似乎无法连接到数据库并且找不到文件。我该如何处理?提前致谢

4

1 回答 1

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

那样有用吗?

希望这可以帮助。

于 2012-11-23T18:24:59.660 回答