2

I am using bash scripting to write and run R scripts: since I was not proficient in R I used to write loops and conditions in the bash script that then were translated in the R scripts or "here documents"... as you can imagine the R_scripts produced in such a way becomes extremely long and difficult to be read... so I learn how to write loops and conditions with R but I found several difficulties with the command system(), so I realized that shell scripting was somehow necessary if I didn't want to get crazy with quoting and escaping... ;-) One of the first problems I faced was this: I wanted to declare a variable like this Rarr="file_1", "file_2", "file_3" ecc because I wanted to insert it in the R_script

cat>my_R_script.R<<EOF
my_arr<-c(${Rarr})
do something with my_arr
EOF

quotes are needed since if file_names were not quoted R would prompt you that it cannot find the objects named file_names

I tryed to follow the first solution in comma separated elements of array defining IFS="" ,"" but it seems that when Rarr="${arr[*]}";echo "${Rarr}" the elements of arr are separated just by the first character of ${IFS}... in my case they will be separated by " is there a way to avoid this?

So basically my question is: how to force shell to consider all the characters in ${IFS} ?

anyway I found two workarounds to my problem.. the first

arr=($(ls -1 | tail))
new_IFS="\" ,\""
Rarr=${arr[0]}
for ((i=1;i<${#arr[@]};i++))
do 
    Rarr="${Rarr}${new_IFS}${arr[$i]}"
    #echo "${Rarr}"
done;
Rarr=\""${Rarr}"\"
#echo ${Rarr}

and another with parameter substitution... but I would like to know if there exist a direct solution to my problem

thank you in advance for your help

Community
  • 1
  • 1
Mareczek
  • 304
  • 1
  • 2
  • 13
  • 1
    Can you simplify your description and provide an example. I feel it's hard to get your problem. – Mu Qiao Aug 26 '11 at 11:24
  • Why are you doing either of these (writing scripts and executing scripts) via bash? Why do you think it's necessary to dynamically change the contents of an R script? This really is not the way to use _any_ scriptable tool, be it R, MatLab, numpy, etc. – Carl Witthoft Aug 26 '11 at 12:13
  • Hi Carl, I thought to follow this way because I have to do the same analysis on many different systems. So I use bash to generate the R script and then, once the bash script has produced the script I run it with `R CMD BATCH name_of_R_script` – Mareczek Aug 26 '11 at 13:57
  • Hi Mu basically I was asking why the solution posted [here](http://stackoverflow.com/questions/1527049/bash-join-elements-of-an-array) does not work in my case: that is there a way to force the IFS be more than one character? – Mareczek Aug 26 '11 at 14:01
  • In that case, why not write a bash script which calls vi or emacs to do your editing? – Carl Witthoft Aug 26 '11 at 14:30
  • Yes I think it is a good idea... I admit that my question look like an idle curiosity... anyway I think that this is the best place to get any doubts in shell scripting be clarified! ;-) Anyway was my description really wrong? Do you understand what am I asking?... Really sorry for my bad english – Mareczek Aug 26 '11 at 14:48

2 Answers2

0

If we do without the spaces (which we don't need):

#!/bin/bash
arr=(${arr[*]/#/\"})            # prepend quotes
arr=(${arr[*]/%/\"})            # append quotes
IFS=, eval 'Rarr="${arr[*]}"'   # join by commas

(note: arr is modified).

Armali
  • 18,255
  • 14
  • 57
  • 171
0

How about simply defining Rarr as Rarr="\"file_1\" \"file_2\" \"file_3\"" using the standard IFS ?

touffy
  • 147
  • 2
  • this is not possible because I do not want to type the members of the arrays, these are to come as result of a command – Mareczek Oct 25 '11 at 09:45