gpt4 book ai didi

read.table 函数和 stdin

转载 作者:行者123 更新时间:2023-12-02 13:21:52 27 4
gpt4 key购买 nike

我有一个制表符分隔的文本文件,我正在尝试使用 read.table 函数将其加载到 R 中。脚本的前几行如下所示

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

这有效。我最初尝试让 R 从标准输入读取数据,但没有成功。我的第一个方法...

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

...似乎根本不起作用。我的第二种方法...

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

...读取数据文件,但(由于某种原因我不明白)前 20 行左右被破坏,这是一个大问题(特别是因为这些行包含标题信息)。有没有办法让 read.table 从标准输入读取?我是否遗漏了一些完全明显的东西?

最佳答案

?stdin 说:

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").

还有:

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

这就是观察到的行为的可能原因。 原则上可以从标准输入read.table - 但在大多数(几乎所有?)情况下,您需要通过文件('stdin')

关于read.table 函数和 stdin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19303218/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com