0

This question is similar to my previous question How to insert a python program into a bash script?. I am basically trying to achieve the same thing but with R. I tried the following

#!/bin/bash

R 1 2 3 4 << EOF 

args<-commandArgs(TRUE)

for (i in args) {

        print(i) 

}

EOF

The output (as displayed when ran in R) should be:

[1] "1"
[1] "2"
[1] "3"
[1] "4"

The method of the answer to my previous question worked fine for python but not for R (which is what I expected). Is there a way to run R code within a a bash script as there is with python?

1 Answer 1

2

Short version: Don't do that.

Long version: Write an R script for which you can use the Rscript executable or, if you have it, the r one from our littler package.

While shell scripts seem tempting at first, you will at some point run into issues with variable interpolation and escaping. Proper R scripts are better -- and the R sources contain a large number of examples.

Example:

/tmp$ ./rex.R 1 2 3 4
[1] "1"
[1] "2"
[1] "3"
[1] "4"
/tmp$ 

With this source:

/tmp$ cat rex.R 
#!/usr/bin/Rscript

args<-commandArgs(TRUE)
for (i in args) {
     print(i) 
}
tmp$ 
Sign up to request clarification or add additional context in comments.

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.