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

6
  • 1
    Can you simplify your description and provide an example. I feel it's hard to get your problem. Commented Aug 26, 2011 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. Commented Aug 26, 2011 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 Commented Aug 26, 2011 at 13:57
  • Hi Mu basically I was asking why the solution posted here does not work in my case: that is there a way to force the IFS be more than one character? Commented Aug 26, 2011 at 14:01
  • In that case, why not write a bash script which calls vi or emacs to do your editing? Commented Aug 26, 2011 at 14:30

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

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
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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.