我正在尝试从网上读取 csv。当我运行时
library(data.table)
rawfile<-fread("http://www.pjm.com/pub/account/genoutages/20140116_genout.csv",stringsAsFactors=FALSE,skip=2)
rawfile
我明白
Empty data.table (0 rows) of 2 cols: File Posted on 16-Jan-2014 04:00,V2
但是如果我先将文件下载到我的计算机上,然后执行
rawfile<-fread("c:\\temp\\20140116_genout.csv",stringsAsFactors=FALSE,skip=2)
rawfile
然后我得到了我期望的输出。
我知道这很容易解决,因为我只是想看看这是否是一个错误,或者是否存在一些我无法理解的细微差别。
最佳答案
更新 2014 年 9 月:现已在 v1.9.3 中修复
URLs now work on Windows. R's
download.file()
converts\r\n
to\r\r\n
on Windows. Now avoided by downloading in binary mode. Thanks to Steve Miller and Dean MacGregor for reporting, #492.
感谢您的报告。当我刚刚在 Linux 上尝试时,它对我来说效果很好。请参阅下面的输出。
?fread
确认支持 URL:“为了方便起见,文件名输入通过 path.expand 传递,并且可能是以 http://或 file://开头的 URL。”
鉴于评论,这似乎是 Windows 独有的问题。在这里提出了一个错误:
#5288 fread reads URLs on Linux but not Windows?
这是在 Linux 上运行的输出:
fread("http://www.pjm.com/pub/account/genoutages/20140116_genout.csv",stringsAsFactors=FALSE,skip=2) trying URL 'http://www.pjm.com/pub/account/genoutages/20140116_genout.csv' Content type 'application/octet-stream' length 2767 bytes opened URL ================================================== downloaded 2767 bytes
Date Forecasted Generation Outage Western Region Other Regions
1: 1/17/2014 1543 374 1169
2: 1/18/2014 1520 374 1146
3: 1/19/2014 1520 374 1146
4: 1/20/2014 1520 374 1146
5: 1/21/2014 1520 374 1146
6: 1/22/2014 1520 374 1146
7: 1/23/2014 1506 374 1132
.. snip
关于r - 为什么 fread 在 Linux 上可以正常读取 URL,但在 Windows(1.9.3 之前)上却不行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21173293/