gpt4 book ai didi

r - 如何从 R 脚本中的管道读取标准输入?

转载 作者:行者123 更新时间:2023-12-01 08:08:05 27 4
gpt4 key购买 nike

我尝试绘制一个蜜蜂图 (http://www.cbs.dtu.dk/~eklund/beeswarm/),我让它工作了,现在我想编写一个小的 R 脚本来实现自动化。我想给这个 R 脚本的输入来自 STDIN,我无法从 STDIN 读取数据。

这是我的 R 脚本:

args <- commandArgs(TRUE)
f1 <- args[1]

plotbeeswarm <- function(output){
library(beeswarm)
f <- read.table(stdin(), header=TRUE)
png(output, width=800, height=800)
beeswarm(Data ~ Category, data=f, pch=16, pwcol=1+as.numeric(sample),
xlab="")
}

plotbeeswarm(f1)

我认为问题在于输入文件是如何被读取并处理成 f 的。谁能帮我修复我的代码?非常感谢!

最佳答案

这是我在 littler 网页上使用的示例,您也应该能够将其改编为 Rscript:

脚本代码如下:

#!/usr/bin/r -i

fsizes <- as.integer(readLines(file("stdin")))
print(summary(fsizes))
stem(fsizes)

然后我将 ls -l 的结果输入,通过 awk 过滤以获得文件大小的一列:

edd@max:~/svn/littler/examples$ ls -l /bin/ | awk '{print $5}' | ./fsizes.r 
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
3 6240 30820 61810 60170 2000000 1

The decimal point is 5 digit(s) to the right of the |

0 | 00000000000000000000000000000000000111111111111111111111111122222222+57
1 | 111112222345679
2 | 7
3 | 1
4 | 1
5 |
6 |
7 |
8 |
9 | 6
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 | 0

edd@max:~/svn/littler/examples$

关于r - 如何从 R 脚本中的管道读取标准输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18498305/

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