我想fread
在 (R) 脚本中使用,该脚本将通过 linux 管道机制获取输入数据。以下有fread
模拟吗?
read.csv(file = 'stdin', ...)
我还将满足于阅读stdin
其他方式,然后使用fread
它来解析它,因为我主要希望它用于fread
' 的高级分隔符和标题逻辑。
我想fread
在 (R) 脚本中使用,该脚本将通过 linux 管道机制获取输入数据。以下有fread
模拟吗?
read.csv(file = 'stdin', ...)
我还将满足于阅读stdin
其他方式,然后使用fread
它来解析它,因为我主要希望它用于fread
' 的高级分隔符和标题逻辑。
原来它很简单:
fread('file:///dev/stdin')
这是可行的,因为fread
当前 7 个字符是“file://”或“http://”时,实际上会创建一个临时文件,并用于download.file
将数据复制到那里然后再复制fread
。
更新:从1.8.11 版开始,可以在 中使用 shell 命令fread
,从而实现另一种解决方案:
fread('cat /dev/stdin')
所有read.*
功能都在其内部使用“扫描”。scan
级别相当低,但确实具有将数据行解析为不同类的能力。
> mat <- matrix(scan(), 4,4) # will paste in block of data
1: 0.5 0.1428571 0.25
4: 0.5 0.1428571 0.25
7: 0.5 0.1428571 0.25
10: 0.5 0.1428571 0.25
13: 0.5 0.1428571 0.25
16: 0.5
17: # Terminate with two <cr>'s
Read 16 items
> mat
[,1] [,2] [,3] [,4]
[1,] 0.5000000 0.1428571 0.2500000 0.5000000
[2,] 0.1428571 0.2500000 0.5000000 0.1428571
[3,] 0.2500000 0.5000000 0.1428571 0.2500000
[4,] 0.5000000 0.1428571 0.2500000 0.5000000
> lst <- scan(what=list(double(0), "a"))
1: 4 t
2: 6 h
3: 8 l
4: 8 8
5:
Read 4 records
> lst
[[1]]
[1] 4 6 8 8
[[2]]
[1] "t" "h" "l" "8"
您还应该查看该?connections
页面。