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

时间:2022-02-24 20:11:17

This question already has an answer here:

这个问题已经有了答案:

I am trying to iterate a function I have written in R (strandcode.txt) over all of the files in a given directory.

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

strandcode.txt is shown below, it's a simple function to compute a Chi Squared test.

strandcode。txt如下所示,它是一个计算Chi平方测试的简单函数。

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}

When I am running this on my Linux server I am using Rscript and iterating over all of the files in the directory by the command below.

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

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

However this is giving me the error Error: unexpected '{' in "strand{" Execution halted

然而,这给了我一个错误:在“strand{”执行中意外的“{”停止

I have also tried the following command lines (taking the final line out of strandcode.txt)

我还尝试了以下命令行(从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

Both run without an error and without outputting anything to my outfile.

两者运行时没有错误,也没有输出任何东西到我的输出文件。

Any suggestions would be greatly appreciated. Thanks!

如有任何建议,我们将不胜感激。谢谢!

1 个解决方案

#1


3  

You have to use a pattern that matches only the data files, instead of $( ls ) which expands to every file in the directory, including strandcode.txt. Assuming you have moved all the data files to a subdirectory called data/, you could do

您必须使用只匹配数据文件的模式,而不是将$(ls)扩展到目录中的每个文件,包括strandcode.txt。假设您已经将所有数据文件移动到名为data/的子目录中,您可以这样做

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

after removing the last line from strandcode.txt, which is incorrect as stated in the comments. This should work as long as the file names don't contain single quotes or other problematic characters.

从strandcode中删除最后一行之后。txt,如注释所述不正确。只要文件名不包含单引号或其他有问题的字符,这就可以工作。

#1


3  

You have to use a pattern that matches only the data files, instead of $( ls ) which expands to every file in the directory, including strandcode.txt. Assuming you have moved all the data files to a subdirectory called data/, you could do

您必须使用只匹配数据文件的模式,而不是将$(ls)扩展到目录中的每个文件,包括strandcode.txt。假设您已经将所有数据文件移动到名为data/的子目录中,您可以这样做

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

after removing the last line from strandcode.txt, which is incorrect as stated in the comments. This should work as long as the file names don't contain single quotes or other problematic characters.

从strandcode中删除最后一行之后。txt,如注释所述不正确。只要文件名不包含单引号或其他有问题的字符,这就可以工作。