I am looking for a way to put a breakpoint during an execution of an R script either using Rscript script.R or R --vanilla --silent -f script.R.
I'd expect that R --vanilla --slave -e 'browser()' would not just output Called from: top level and exit, but instead break and open the Browse[1]> > shell.
Is there a way to achieve that in R using browser(), debugger(), etc?
Related
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
I would like to call Rscript without a proper script, just by writing it in a console. is it possible? something like
c:/path/to/R/bin/Rscript.exe "here is a R code, not a R file"
I am on Windows.
Run Rscript with -e argument:
Rscript -e "getwd()"
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.
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.
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`""