I'd like to use fread
in a (R)script that would get input data via the linux pipe mechanism. Is there an fread
analog for the following?
read.csv(file = 'stdin', ...)
I'll also settle for reading stdin
some other way and then using fread
to parse it, as I mainly want this for fread
's superior separator and header logic.
Turns out it's as simple as:
fread('file:///dev/stdin')
This works, because fread
actually creates a temporary file when the first 7 characters are "file://" or "http://" and uses download.file
to copy the data there and then fread
that.
Update: As of version 1.8.11 one can use shell commands in fread
, making another solution possible:
fread('cat /dev/stdin')
All of the read.*
functions use 'scan' under their hoods. scan
is fairly low level but does have the capacity for parsing lines of data into different classes.
> 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"
You should also look at the ?connections
page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With