Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

r

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

#!/bin/bash
cd documents
module load r

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

However, it appears that after running this using qsub script.sh, it comes back with errors saying how several commands were not recognized. Am I doing this wrong? Thanks!

like image 756
user1398057 Avatar asked Jan 10 '23 16:01

user1398057


1 Answers

I think you're looking for a "Here document" in the shell. That 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, the Rscript is called (from /usr/bin/env) and on the standard input, all of the subsequent lines are passed until the marker (here, EOF) is seen.

like image 139
Matthew Lundberg Avatar answered Jan 12 '23 07:01

Matthew Lundberg