theomega
theomega

Reputation: 32051

Reading csv directly from another program

How can I tell R to import the csv-data in the read.table command from the stdout of another program instead of from a file? I want R to launch my csv-export-utility on its own.

Upvotes: 2

Views: 544

Answers (2)

Dirk is no longer here
Dirk is no longer here

Reputation: 368579

See the help page from help(connections) and the documentation of the pipe() function.

Here is a complete (but nonsensical) example where we write a csv file

R> write.csv(iris, file="/tmp/iris.csv", row.names=FALSE)

and then just use cat to show its content on stdin and read that via read.csv() from the pipe():

R> newiris <- read.csv(pipe("cat /tmp/iris.csv"))

What we get is what we had, expected:

R> head(newiris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
R> all.equal(iris, newiris)
[1] TRUE
R> 

Upvotes: 7

Elvis
Elvis

Reputation: 565

Use a named pipe, but this question should be on stack overflow.

Upvotes: 1

Related Questions