[Solved] Running a R script with in a shell script

Hi,

I do have an R script named KO.R. Basically reads thousands of files, whose name has a pattern that differs at a portion of the file name, List.txt.

Row_file1_mile.txt
Row_file2_mile.txt
Row_file3_mile.txt
...
...
Row_file1000_mile.txt

Below is a portion of my Rscript that reads the file.

vi KO.R

Sample =
  read.delim("/Users/dropBox/Row_$file_mile.txt")
...
...
...

If I provide the list of the variable portion of the file name like

file1
file2
file3
...
..
file1000

how could I run this script one by one by reading each of the files and execute the script.

This is what I tried by making a .sh file

while read file
do

R CMD BATCH KO.R 

done < /Users/dropBox/List.txt

where List.txt contains the list of the variable portion of the file.

Could you please let me know the best way to excecute this R script with in the .sh executable shell script?

Try this instead:

while read file
do

    sed 's/\$file/'"$file"'/g' KO.R > KO_NEW.R
    R CMD BATCH KO_NEW.R
    rm KO_NEW.R

done < /Users/dropBox/List.txt

It didn't work

Can you try the following sed command from the command line and check the new .R file to ensure the replacement you wanted has happened:

file=file3
sed 's/\$file/'"$file"'/g' KO.R > KO_NEW.R

Thanks. I got it working now.

There was a typo on my side.