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`""
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 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?
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.
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 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