In a shell script, how can you combine the `Rscript` and` Unix` commands?

I am currently trying to run an R file as an Rscript file. I have the following code inside a file script.sh

:

#!/bin/bash
cd documents
module load r

#!/usr/bin/env Rscript
mem2 <- 4+5
packageVersion("data.table")
save.image("OUT.RData")

      

However, it looks like after running with qsub script.sh

it it returns with errors, saying that several commands were not recognized. Am I doing it wrong? Thank!

+3


source to share


1 answer


I think you are looking for the "Here" document in the shell. This allows text to be passed to another interpreter. For example:

#!/bin/bash
cd documents
module load r

/usr/bin/env Rscript -<<EOF
mem2 <- 4+5
packageVersion("data.table")
save.image("OUT.RData")
EOF

      



Here Rscript

is called (from /usr/bin/env

) and on standard input all subsequent lines are passed until a marker appears (here, EOF

).

+7


source







All Articles