【问题标题】:Reading binary data of indeterminate length in R在R中读取不确定长度的二进制数据
【发布时间】:2010-11-26 14:24:38
【问题描述】:

我想直接从 R 中的 URL 读取长度不确定的二进制文件。使用 readBin 从 URL 读取,而不指定文件大小,不起作用。

 anImage <- readBin('http://user2010.org/pics/useR-large.png','raw')

是否有其他方法可以做到这一点?

【问题讨论】:

    标签: r binaryfiles


    【解决方案1】:

    这会将文件下载到工作目录,但不会直接下载到内存中。

    download.file('http://user2010.org/pics/useR-large.png', 'anImage.png')

    Rcurl 包也可以做你想做的事。 (由于 SO 限制,链接未发布)

    【讨论】:

      【解决方案2】:

      一个简单的解决方案,如果将“n”设置为相当大,读取文件,检查可能的溢出,并在必要时重试。

      N <- 1e7
      repeat
      {
         anImage <- readBin(filename, 'raw', n=N)
         if(length(anImage) == N) N <- 5 * N else break
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-08
        • 2017-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多