How to Read Input from the Terminal Using /Dev/Stdin and Read.Csv()

How to read input from the terminal using /dev/stdin and read.csv()?

I think the answer in the first link you posted may actually be applicable. R appears to create a 4 byte buffer on /dev/stdin. Also, as mentioned in the comment, you can use stdin instead, and it appears to work fine. (Although I still don't get why you have to hit Ctrl+D twice).

d <- read.csv(file='stdin', header=TRUE)
a,b
1,2
3,4
# Hit Control+D twice.
> d
a b
1 1 2
2 3 4

R skips lines from /dev/stdin

head --bytes=4K file | tail -n 3

yields this:

1039
1040
104

This suggests that R creates an input buffer on /dev/stdin, of size 4KB, and fills it during initialisation. When your R code then reads /dev/stdin, it starts in file at this point:

   1
1042
1043
...

Indeed, if in file you replace the line 1041 by 1043, you get a "3" instead of "1" in the table(x):

3  1042  1043  1044  1045  1046  1047  1048  1049  1050  1051  1052  1053 
1 1 1 1 1 1 1 1 1 1 1 1 1
...

The first 1 in table(x) is actually the last digit of 1041. The first 4KB of file have been eaten.

Is there an fread analog for reading from stdin?

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')

How to use read function to read from terminal input (stdin)?

On POSIX you can use symbols like STDIN_FILENO to represents the input of your application. But beware that the standard input is not always the terminal, especially when you redirect input/output via the shell.

How can I read, analyze, and then un-read and reread the beginning of an input stream in Perl?

As you said, if the filehandle might be STDIN, you can't use seek to rewind it. But it's still pretty simple. I wouldn't bother with a module:

my @lines;

while (<$file>) {
push @lines, $_;
last if @lines == 500;
}

... # examine @lines to determine format

while (defined( $_ = @lines ? shift @lines : <$file> )) {
... # process line
}

Remember that you need an explicit defined in this case, because the special case that adds an implicit defined to some while loops doesn't apply to this more complex expression.

Function doesn't execute the read.csv lines

You need to return your results from inside the function.

data_load <- function(){
links <- read.csv("vpn.txt", header=T, as.is=T)
nodes <- read.csv("vpn.txt", header=T, as.is=T)
return(list(links=links, nodes=nodes))
}

data=data_load()
links=data$links
nodes=data$nodes

Rstudio Automatically use input file name in read.csv when using write.csv & two headers

You can capture the filename from file.choose and save the skipped lines to write out later.

## Capture file name
FileName = file.choose()

# Capture skipped header lines
IN=file(FileName, open="r")
Header=readLines(IN, 2)
Input <- read.csv(IN) # No need to skip lines now
close(IN)

## Whatever processing

## Output
OUT = file(FileName ,open="w")
writeLines(Header, OUT)
write.csv(Input, OUT, row.names=FALSE)
close(OUT)

read.table function and stdin

?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').



Related Topics



Leave a reply



Submit