Running R script through qsub - r

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.

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

Run multiple R scripts concurrently through a batch file

I'm trying to follow the conventional method to trigger R scripts through batch like
RScript Example.R
but what i look to is some way to run multiple R scripts through a batch file.
I tried to do use Start command to open multiple sessions but that doesn't work either. (RScript START ex1.R START ex2.R)
PS complete noob to batch files.
On Windows - if you want to run them in parallel make sure you add start in yoyr batch (.bat) script. Otherwise Example2.R waits for Example1.R to complete etc.
start RScript Example1.R
start RScript Example2.R
...
If you're using sh to launch your scripts, this could do it.
cd /path_to_script1/
sh script1.sh &
cd /path_to_script2/
sh script2.sh &
cd /path_to_script3/
sh scipt2.sh &
This launches parallel R sessions (one for each script) so careful with memory and CPU use. Each script file contains Rscript command.
Simply save the RScript commands in a Windows batch file (.bat) then double-click the file in directory or call it via command line. Below assumes RScript is an environment variable.
Batch file (type below in Notepad and save with extension .bat, not default .txt)
cd "C:\Path\To\Scripts"
RScript Example1.R
RScript Example2.R
RScript Example3.R
RScript Example4.R
RScript Example5.R
CMD Command line
call myRScriptBatchFile.bat
PowerShell Command line
cmd.exe /c myRScriptBatchFile.bat

In order : launch new terminal, launch R environment in the new terminal, execute commands in R environment

I would like a shell script that launches a new terminal, opens the R environment in the terminal and executes commands in the R environment of the new terminal.
Here's what I did which is not working :
#!/bin/sh
for i in $(seq 25)
do
gnome-terminal -x sh "R; source('source.r'); function($i)"
done
Where, function() is an r function in the file "source.r"
Please help.
N.B. I don't want to launch the program using the command "Rscript"
EDIT 1 : I don't want to use the command Rscript because the execution halts after sometime (don't know why). In an R environment the script works fine though. Here's what I tried with the command Rscript :
#!/bin/sh
for i in $(seq 25)
do
gnome-terminal -e "Rscript script.r $i"
done
EDIT 2 : I found the reason why the script execution halted with the command Rscript. It was a bug in the code. Now I can make things work with what I did in EDIT 1. Would be nice to know if things can be made to work by not using Rscript i.e. launching R in different terminals and executing commands in each terminal from the shell script.

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.

R Batch Mode - Suppress output file

I have some scripts that I run using R's batch mode.
/usr/bin/R CMD BATCH --vanilla --no-timing ~/scripts/R/sess_dur.R
I redirect the output to a file using:
> sink("~/scripts_output/R_output.txt",append=TRUE)
The problem is that when I run this script, files are created with the same name of the script and the "out" suffix (sess_dur.Rout).
There is some way to tell R not to generate these files?
Have you tried something like:
R CMD BATCH --vanilla --no-timing ~/scripts/R/sess_dur.R /dev/null

Resources