Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read.table function and stdin

Tags:

r

stdin

I have a tab-delimited text file that I am trying to load into R with the read.table function. The first few lines of the script look like this

#!/usr/bin/env Rscript
args <- commandArgs(trailingOnly=TRUE)
data <- read.table(args[1], header=TRUE, sep="\t", quote="")
# process the data

This works. I had originally tried to get R to read the data from standard input, but was unsuccessful. My first approach...

#!/usr/bin/env Rscript
data <- read.table(stdin(), header=TRUE, sep="\t", quote="")
# process the data

...didn't seem to work at all. My second approach...

#!/usr/bin/env Rscript
data <- read.table("/dev/stdin", header=TRUE, sep="\t", quote="")
# process the data

...read the data file but (for some reason I don't understand) the first 20 or so lines get mangled, which is a big problem (especially since those lines contain the header information). Is there any way to get read.table to read from standard input? Am I missing something completely obvious?

like image 292
Daniel Standage Avatar asked Oct 10 '13 18:10

Daniel Standage


People also ask

What is read table function in R?

table() function in R Language is used to read data from a text file. It returns the data in the form of a table. Syntax: read.table(filename, header = FALSE, sep = “”)

What is the difference between read table and read CSV in R?

csv() as well as the read. csv2() function are almost identical to the read. table() function, with the sole difference that they have the header and fill arguments set as TRUE by default. Tip: if you want to learn more about the arguments that you can use in the read.

How do I pass Ein to stdin?

It should be sent by the user. So is it that only the user can invoke EOF in stdin by pressing Ctrl + Z ? Yes, you can set the EOF indicator for stdin with a special key combination you can input in the console, for linux console that is Ctrl + D and for windows it's Ctrl + Z .


1 Answers

?stdin says:

stdin() refers to the ‘console’ and not to the C-level ‘stdin’ of the process. The distinction matters in GUI consoles (which may not have an active ‘stdin’, and if they do it may not be connected to console input), and also in embedded applications. If you want access to the C-level file stream ‘stdin’, use file("stdin").

And:

When R is reading a script from a file, the file is the ‘console’: this is traditional usage to allow in-line data …

That’s the probable reason for the observed behaviour. In principle you can read.table from standard input – but in most (almost all?) cases you’ll want to do this via file('stdin').

like image 189
Konrad Rudolph Avatar answered Sep 25 '22 18:09

Konrad Rudolph