How to Make my R script executable? How to Make my R script executable? r r

How to Make my R script executable?


Ah, Its carriage return (\r) issue, It's added to the first line, if you are using vi editor, :set list will show it. line endings will be shown as $ and carriage return chars as ^M.

#!/usr/bin/env Rscript  Makes your script portable than #!/usr/bin/Rscript

Btw, you can insert \r in vi by going into insert(i)/Append(a) mode and type ctrl+v and then ctrl+m


If you want to point directly to the executable, then you need the full path after the shebang (no space):

#!/usr/bin/Rscript

As Ravi pointed out, if this fix doesn't work then the solution may just involve deleting the line break and putting it in again.

I'm not a fan of the env workaround to make things more portable, because it makes the line more confusing and most people don't realise that it's actually calling another program (i.e. env) to run the code in a modified shell. More information on that here.


The best answer I've found is here: env: python\r: No such file or directory

Open the file in vim or vi, and administer the following command::set ff=unixSave and exit::wqDone!