如何使用/dev/stdin和read.csv()从终端读取输入? [英] How to read input from the terminal using /dev/stdin and read.csv()?
问题描述
我正在使用:
R version 3.0.0 (2013-04-03) -- "Masked Marvel"
Platform: x86_64-pc-linux-gnu (64-bit)
我尝试使用read.csv
直接从终端输入一些CSV数据段+标头.
I try to use read.csv
to input a little CSV data snippet + header, directly from the terminal.
我遇到的问题可能与 R跳过/dev的行有关/stdin 和 read.csv,第一行的标题,第二行的跳过但又足够不同(答案没有解释我在这里看到的内容),因此可以提出一个单独的问题.
I'm encountering a problem that may be related to R skips lines from /dev/stdin and read.csv, header on first line, skip second line but is different enough (the answers there don't explain what I see here) to warrant a separate question.
R似乎跳过了标题行,并将第二(数据)行视为标题:
R seems to skip the header line and treat the second (data) line as header:
R> d <- read.csv(file='/dev/stdin', header=TRUE)
a,b
1,2
3,4
# hit CTRL-D twice here to end the input
# (this is also unexpected:
# when reading a few lines interactively in bash, one CTRL-D suffices.
# Why is doing it twice necessary in R?)
R> d
X1 X2
1 3 4
R> colnames(d)
[1] "X1" "X2"
我找到了一种解决方法:由于默认情况下read.csv
具有blank.lines.skip = TRUE
,因此我在输入前加上一些空行.开始输入之前的5条空行似乎是使它按预期工作所需的最低要求.顺便说一句:一行包含5个空格的行同样有效,提示需要大约5个字节(或更多)的空白填充:
I found a workaround: since by default read.csv
has blank.lines.skip = TRUE
, I prefix the input with some blank lines. 5 empty lines before starting the input, seem to be the minimum required to get this to work as expected. BTW: a single line with 5 spaces works just as well, hinting at some 5 byte (or more) required whitespace padding:
R> d <- read.csv(file='/dev/stdin', header=TRUE)
a,b
1,2
3,4
# Enter CTRL-D twice here to mark the end of terminal input
R> d
a b
1 1 2
2 3 4
R> colnames(d)
[1] "a" "b"
问题:
- 为什么第一个示例无法按预期运行?
- 为什么需要5个空白行或空格(即使4个空格也不够)才能使其正常工作?
- 有没有更好的方法可以直接从终端读取简短的csv代码段?
(我知道
scan
和readLines
,但是我的数据已经是csv格式,所以我想使其尽可能简单地读取/解析/分配)
- Why isn't the 1st example working as expected?
- Why are 5 blank lines or spaces needed (even 4 aren't enough) to make it work?
- Is there a better way to reading a short csv snippet directly from the terminal?
(I know about
scan
andreadLines
, but my data is in csv format already, so I want to make it as simple to read/parse/assign as possible)
推荐答案
我认为您发布的第一个链接中的答案可能实际上是适用的. R似乎在/dev/stdin上创建了一个4字节的缓冲区.另外,如评论中所述,您可以改用stdin,它似乎可以正常工作. (尽管我仍然不明白为什么您必须两次按Ctrl + D).
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
这篇关于如何使用/dev/stdin和read.csv()从终端读取输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!