R Batch Mode - Suppress output file - r

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

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

Error in .BAT file R Script

I have a .bat file as follows. task.bat
#echo off
R CMD BATCH C:\Users\Raghavan\Desktop\MyTask.R
The file needs to run an R script. The following is the R script.
A<-read.csv("C:\Users\Raghavan\Desktop\A.csv")
write.csv(A, file = "B.csv")
The R script runs by itself. However, when I try running it through the .bat file or through windows scheduler, the script fails to run. I have tried many sites.I request someoen to see if this error can be fixed. Most likely, the error is in the .bat file
I run my R files with the following command inside of the .bat script:
#echo Off
"C:\Program Files\R\R-3.4.2\bin\R.exe" CMD BATCH --vanilla --slave "C:\File.R"
Define the full paths to executable R and to the file that you wanna run. Hope this helps.
See also ?BATCH and this resource on arguments of R CMD BATCH.
I made the following edits to the code.
# echo off
"C:\Program Files\R\R-3.3.2\bin\R.exe" R CMD BATCH "D:\Newfolder\task1.R"
The files were then shifted to the D drive. This resulted in the code working.

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.

running R in batch-mode - print to screen?

When running
R CMD BATCH [options] filename.r
I want to control where the output is printed. I can suppress the creation of the .Rout file with
R CMD BATCH [options] filename.r /dev/null
but is it possible to direct the output to the screen? Like when I run it by
R [options] < filename.r
?
Guess you're on linux. Tried already to redirect to /dev/console ?
Edit -add info from the comments -:
/dev/console apparently doesn't work, /dev/tty does. Depending on the system, /dev/tty0 might be an option too
Cheers
Try Rscript or R --no-save < filename.R:
biocoreap1:Desktop vinceb$ Rscript test.R
test
biocoreap1:Desktop vinceb$ R --no-save < test.R
R version 2.10.1 (2009-12-14)
Copyright (C) 2009 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
[...]
[Previously saved workspace restored]
> cat('test\n')
test
>
You can maybe do both at the same time (creating the file and printing on screen) if you run R CMD BATCH [options] filename.r and then tail -f filename.Rout

Resources