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
Related
I'm trying for my first time to run an R script from command line on WINDOWS to automate calculation with another program. So I made my R script using R studio and saved it a .bat file with this line
C:\Program Files\R\R-4.0.2\bin CMD BATCH C:\my_directory\my_script.R
Then I putted the script and files the script have to be executed on in the same directory, the one that contains files to run also the program I need. I came throught command line in that directory and I executed the batch file but it doesn't work. I have this as a error message:
"C:\Program Files\R\R-4.0.2\bin"it is not recognized as an internal or external command,
an executable program or batch file.
Where I am wrong? thank you!
You should use Rscript instead, assuming its in your PATH.
E.g.
Rscript C:\my_directory\my_script.R
Or a single expression
Rscript -e "print(123)"
I am running a script that starts as follows:
#!/usr/bin/env Rscript
#./geneiase -t static -i mydata.tab
If I run the script on my data directly in the command line, it starts without errors or warnings.
But the program is very demanding computationally so I need to submit my jobs to a cluster using a job scheduler called Slurm.
When I write the exact same expression (as in the second paragraph) within the batch job file, and then I submit the job using sbatch, it is immediatelly terminated and does not return any error or output that can help me understand the problem.
I think it has to do with having Rscript in $PATH, but even though I added the directory where Rscript is located to $PATH by: PATH=$PATH:path/to/R/build/R-3.4.0/lib64/R/bin, the problem remains.
Is there a way that I can make Rscript be run in a Slurm batch job?
You'll need to keep the environment of your SLURM script as bash
#!/bin/bash
Since you can run your R script from the command line, it likely means the path to R is already included in your $PATH. On the command line, you might already do something like:
Rscript ./path-to-script/script
To run R from within your SLURM script, it's the same as running from the command line:
Rscript ./path-to-script/script
I am trying to run a .bat file, which does run perfectly when I double click in it (Windows OS), but fails when I try to run it in R
com <- "C:\\SASLocal\\RUN3614\\56a8c11b-84b2-4af7-a155-01190936b1c1\\M1_superGOtest.bat"
system(com)
I am getting back a message of had status 2
Just an FYI, this triggers a SAS program, which I need to run in SAS as it is for comparison purposes between SAS and R.
In Windows, to run batch files from command line you need to call a command line interpreter, Command Prompt or PowerShell, passing batch file as an argument.
A .bat script by itself is like an .R script and does not do anything until an executable runs it (i.e., Rscript.exe, R.exe, Rcmd.exe, Rterm.exe) and in this case, cmd.exe and powershell.exe:
# COMMAND PROMPT
system('cmd /c "C:\\SASLocal\\RUN3614\\56a8c11b-84b2-4af7-a155-01190936b1c1\\M1_superGOtest.bat"')
# POWERSHELL
system('powershell -c & "\'C:\\SASLocal\\RUN3614\\56a8c11b-84b2-4af7-a155-01190936b1c1\\M1_superGOtest.bat\'"')
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.