The posted version of the script replaces single-quotes with double-quotes, which is problematic when the quotes are nested.

Below I adapted the relevant portion of the script to allow for single-quotes. To interpret the alternative code (which uses a '$' in the system call), note that it seems that four backslashes are required to produce one blackslash that gets passed to Applescript.

[Note: I had trouble with escaped backslashes (`\\`). So I modified the code below. I don't really entirely understand what's going on with the backslashes between Vim and R--why do I need 16 backslashes for two to make it to R?--but this seems to work. Date: 2011-02-11]