4

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

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

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.

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

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

I have also tried the following command lines (taking the final line out of 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!

  • 3
    `strand{$i}` is not proper R code. Your `$i` is not visible inside the script, so you need to get what bash knows as `$i` into something your script knows about. This may be a duplicate of http://stackoverflow.com/questions/2151212/how-can-i-read-command-line-parameters-from-an-r-script and the linked page http://stackoverflow.com/questions/2151212/how-can-i-read-command-line-parameters-from-an-r-script/2151627#2151627, where Dirk modifies his recommendation. – r2evans Jun 13 '16 at 20:47

1 Answers1

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

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.

Ernest A
  • 7,526
  • 8
  • 34
  • 40