在Bash命令行中对文件运行R函数

艾米丽·达蒙(Emily Damone):

我正在尝试对给定目录中的所有文件迭代用R(strandcode.txt)用R编写的函数。

twincode.txt如下所示,它是计算卡方检验的简单函数。

strand <- function(file){
data <- as.data.frame(read.table(file))
colnames(data) <- c('chr', 'pos', 'fwd', 'bkwd')
data$chi <- ((.5 - (data$fwd / (data$fwd + data$bkwd)))^2)/.5
keep <- data[data$chi < .823, ]
return(keep)
}

strand{$i}

当我在Linux服务器上运行此命令时,我正在使用Rscript并通过以下命令遍历目录中的所有文件。

for i in $( ls ); do Rscript strandcode.txt >> strandout.txt; done

但这给我一个错误错误:“ strand {”中的意外'{'

我也尝试了以下命令行(将最后一行从strandcode.txt中删除)

for i in $( ls ); do Rscript strandcode.txt; Rscript strand{$i} >>     strandout.txt; done
for i in $( ls ); do Rscript strandcode.txt strand{$i} >> strandout.txt; done

两者都运行没有错误,也没有向我的输出文件输出任何内容。

任何建议将不胜感激。谢谢!

欧内斯特A:

您必须使用仅与数据文件匹配的模式,而不是将$( ls )模式扩展到目录中的每个文件,包括strandcode.txt假设您已将所有数据文件移动到名为的子目录中data/,则可以执行以下操作

for i in data/*; do Rscript -e "source('strandcode.txt'); print(strand('$i'))" >> strandout.txt; done

从中删除最后一行后strandcode.txt,注释中指出这是错误的。只要文件名不包含单引号或其他有问题的字符,这应该起作用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章