Embed R code into BASH unix script - r

We know that we can embed AWK or SED directly into a BASH script by
awk { ....}
. I wanted to do something similar to R such as
mybashscript.sh
R { .... }
without having to create a .R file and calling the .R file from bash using Rscript myRscript.R
Is there a way to do this. I have searched everywhere but was not able to find an answer. Please help.

You can easily embed R code directly into a bash script using the Rscript command, and specifically, with regards to your question, the -e expr construction. Check man Rscript.
An example:
#!/bin/bash
echo "This is a bash script"
echo "Running R code..."
Rscript -e 'cat("hello world")'

Bash is able to execute a statement from an arbitrary scripting language. It is in the form VARIABLE_NAME=$(INTERPRETER -c "EMBEDDED CODE"). In the case of R INTERPRETER -c would be replaced with R --arch i386 -q --slave -e. The --arch option can be any supported architecture such as i386 or x64, -q keep the interpreter from printing start-up info and --slave keeps the output as clean as possible.

Related

How to write console output to a text file with Rscript, like you can with R CMD BATCH

In the past I have used R CMD BATCH to execute R code from the command line on a Linux server. The syntax I used was
R CMD BATCH --no-save --no-restore rcode.r output.txt
The above code writes console output to output.txt which can be monitored as the script is running. Is this also possible with Rscript? I would prefer to use Rscript since I have heard that R CMD BATCH is deprecated.
To clarify my original question, R CMD BATCH writes all console output, including messages, warnings, and print() statements, to output.txt. In contrast Rscript rcode.r > output.txt writes only the print()ed output to the text file and everything else to the terminal. How can I replicate the behavior of R CMD BATCH with Rscript?
I discovered after some digging that, at least on the Linux system I'm using, Rscript is just a convenience function. If you call
Rscript --verbose foobar.r
you will see the underlying call is:
running
'/usr/lib/R/bin/R --no-echo --no-restore --file=foobar.r'
This means that --no-echo is baked into Rscript.
Therefore the solution is to run
/usr/lib/R/bin/R --no-restore --file=foobar.r > output.txt
where the --no-echo is removed, and the output is redirected to a text file as suggested by #MrFlick. The commands will be echoed in addition to the output.
You can create a new alias for Rscript if you want --no-echo to be removed by default. For example, in my .bashrc file I have the following:
function Rscript2() { R --no-restore --file="$1"; }
export -f Rscript2
Now, in my Slurm batch job scripts, I can run Rscript2 file.R to get the desired behavior: all R console output is included in the slurm-*.out files.
Just redirect the output to a file like you would with any other command line output
Rscript rcode.r > output.txt

`Terminal` vs `system()` in R

I tried running the following in R
system("Message=HelloWoRld;echo $(sed 's/R/r/' <(echo ${Message}))")
but it fails, while
Message=HelloWoRld
echo $(sed 's/R/r/' <(echo ${Message}))
works fine when copy pasted in the terminal. The issue seems related to <(..). When I do either which bash or system("which bash"), I get /bin/bash.
Why does the same command via system() or directly on the terminal window does not yield to the same output?
FYI, I am on Mac OS X 10.11.3. Bash is GNU bash, version 3.2.57(1) and R is R version 3.2.3.
system is not a terminal emulator, and it’s not running Bash. Your terminal runs Bash. To get the same effect with system, run the command inside Bash. E.g.
system('bash -c \'echo $(date)\'')
What’s more, your current Bash command is quite convoluted and uses unnecessary command invocations; you can achieve the same via the much simpler
sed s/R/r/ <<< $Message
#chepner makes the excellent point that another solution can be used directly in system without need to pass execution to Bash:
system("Message=HelloWoRld; echo $Message | sed 's/R/r/'")

Rscript in silent mode

I am using Rscript to run an R script but I get a lot of output on my screen. Can I run Rscript in silent mode (meaning without any screen output)?
Several options come to mind:
within R: use sink() to divert output to a file, see help(sink)
on the shell: Rscript myscript.R 2>&1 >/dev/null
edit the code :)
on Linux, use our littler frontend as it runs in --slave mode by default :)
Options 3 is the most involved but possibly best. You could use a logging scheme where you print / display in "debug" or "verbose" but not otherwise. I often do that, based on a command-line toggle given to the script.
You can redirect the output with
Rscript myscript.R >& >/dev/null (linux)
or
Rscript myscript.R >$null (windows)
or use R directly:
R --quiet --vanilla < myscript.R
or
R CMD BATCH myscript.R
(That last version writes the output to a file myscript.Rout)
One more option: if you want to separate the output and the error message into different files, which makes it easier to identify the problems, you can use the command on the shell:
Rscript myscript.R >a.Rout 2>a.Rerr
This will write the program output to a.Rout and the error messages to a.Rerr. Note that the files of a.Rout and a.Rerr should be removed beforehand, to avoid an error.

Running R script through qsub

I am trying to run an R script called test.r through qsub. My R script is as follows:
#!/usr/bin/Rscript
x <- 1
write.csv(x,"test.csv")
If in Ubuntu terminal I type R CMD BATCH test.r, then the script behaves as planned; test.csv gets exported in the same directory.
However if I create a bash script called testbash.sh and run it through the command qsub testbash.sh; it will run without errors but the output won't be there.
#!/usr/bin/bash
R CMD BATCH test.r
How to fix this?
Try modifying your R script to:
#!/usr/bin/Rscript
x <- 1
print(getwd())
write.csv(x,"test.csv")
When you run a script via qsub, the script is normally running in another server, and by default as in your home directory. You need to change to the original directory in your script, there is a variable PBS_O_WORKDIR for that:
#!/usr/bin/bash
#PBS -N qsub_R_test
echo We are in the $PWD directory
cd $PBS_O_WORKDIR
echo We are now in $PBS_O_WORKDIR, running an R script.
R --vanilla < test.r > test.log 2> test.log
I normally cannot use R CMD BATCH, but redirection to R -vanilla works. You can also specify options for the PBS in the script, starting with #PBS, like the job name in this case (qsub_R_test).
You can get a more detailed list of qsub parameters here:
http://www.csc.fi/english/pages/louhi_guide/batch_jobs/commands/qsub
And an example of a PBS script here:
http://bose.utmb.edu/Compu_Center/Cluster_users/PBS%20HOWTO/PBS_HOW_TO.html
You may be doing it wrong. If you have a shebang line like
#!/usr/bin/Rscript
then "simply" do chmod 0755 test.r on the file, and run it:
./test.r
That should work, and you can then have that invocation in your qsub-called script.

Run R script from Powershell

In batch script, I can run an R script with the following syntax:
Rterm.exe --quiet --slave --vanilla < "C:\some_script.R"
However, Powershell seems to have reserved "<" for future expansion. I am wondering if there is a direct way to run R script within another Powershell script.
You should probably look Rscript instead of redirection -- this would become
Rscript.exe C:\someScript.R
where you can add the usual options.
Easiest way is probably to wrap it in a call to cmd.exe:
cmd.exe /C "Rterm.exe --quiet --slave --vanilla < `"C:\some_script.R`""

Resources