R on ARC: Difference between revisions
Line 50: | Line 50: | ||
</pre> | </pre> | ||
There are three ways to run an R script. | |||
=== From standard input === | |||
An R script can be sent to the standard input of the '''R''' interactive shell. | |||
This is similar to typing the commands in '''R''': | This is similar to typing the commands in '''R''': | ||
<pre> | <pre> | ||
Line 94: | Line 95: | ||
=== Using <code>CMD BATCH</code> command === | |||
An R script can be passed as an argument to the "R CMD BATCH" command. | |||
The output does not go to the screen, but is saved to the '''.Rout''' file: | The output does not go to the screen, but is saved to the '''.Rout''' file: | ||
<pre> | <pre> | ||
$ R CMD BATCH test.R | $ R CMD BATCH test.R | ||
$ ls -l | $ ls -l | ||
-rw-r--r-- 1 drozmano drozmano 176 May 7 15:03 test.R | -rw-r--r-- 1 drozmano drozmano 176 May 7 15:03 test.R | ||
-rw-r--r-- 1 drozmano drozmano 1121 May 7 15:19 test.Rout | -rw-r--r-- 1 drozmano drozmano 1121 May 7 15:19 test.Rout | ||
</pre> | |||
To see the output use the '''cat''' or '''less''' commands: | |||
<pre> | |||
$ cat test.Rout | $ cat test.Rout | ||
Line 142: | Line 148: | ||
Again, both the commands and the output are shown. | Again, both the commands and the output are shown. | ||
=== Using Rscript version of R === | |||
Probably the best non-interactive way to run an R script is to use a special non-interactive version of R, <code>Rscript</code>: | |||
<pre> | <pre> | ||
$ Rscript test.R | $ Rscript test.R |
Revision as of 06:01, 9 May 2020
General
Text mode interactive shell
When you start R usual way you get into interactive R shell where you can type commands and get the results back. Like this:
$ module load R/3.6.2 $ R R version 3.6.2 (2019-12-12) -- "Dark and Stormy Night" Copyright (C) 2019 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) .... Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > Sys.info() sysname release "Linux" "3.10.0-1127.el7.x86_64" version nodename "#1 SMP Tue Mar 31 23:36:51 UTC 2020" "arc" machine login "x86_64" "drozmano" user effective_user "drozmano" "drozmano" > quit() $
Running R scripts from the command line
In order to run R scripts / programs on ARC as jobs you have to pre-record the commands you want in a text file,
for example test.R
,
and run it as a script non-interactively.
test.R:
cwd = getwd() cat(" Current Directory: ", cwd, "\n") t = Sys.time() cat(" Current time: ", format(t), "\n") u = Sys.info()["user"] cat(" User name: ", u, "\n")
There are three ways to run an R script.
From standard input
An R script can be sent to the standard input of the R interactive shell. This is similar to typing the commands in R:
$ R --no-save < test.R R version 3.6.2 (2019-12-12) -- "Dark and Stormy Night" Copyright (C) 2019 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > cwd = getwd() > cat(" Current Directory: ", cwd, "\n") Current Directory: /global/software/src/r/tests > > t = Sys.time() > cat(" Current time: ", format(t), "\n") Current time: 2020-05-07 15:16:12 > > u = Sys.info()["user"] > cat(" User name: ", u, "\n") User name: drozmano > >
After executing all the commands from the script, R terminates. Note that both the commands and the printed output are shown.
Using CMD BATCH
command
An R script can be passed as an argument to the "R CMD BATCH" command. The output does not go to the screen, but is saved to the .Rout file:
$ R CMD BATCH test.R $ ls -l -rw-r--r-- 1 drozmano drozmano 176 May 7 15:03 test.R -rw-r--r-- 1 drozmano drozmano 1121 May 7 15:19 test.Rout
To see the output use the cat or less commands:
$ cat test.Rout R version 3.6.2 (2019-12-12) -- "Dark and Stormy Night" Copyright (C) 2019 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > cwd = getwd() > cat(" Current Directory: ", cwd, "\n") Current Directory: /global/software/src/r/tests > > t = Sys.time() > cat(" Current time: ", format(t), "\n") Current time: 2020-05-07 15:19:07 > > u = Sys.info()["user"] > cat(" User name: ", u, "\n") User name: drozmano > > > proc.time() user system elapsed 0.219 0.079 0.369
The output if very similar to the first way, but contains some additional timing information. Again, both the commands and the output are shown.
Using Rscript version of R
Probably the best non-interactive way to run an R script is to use a special non-interactive version of R, Rscript
:
$ Rscript test.R Current Directory: /global/software/src/r/tests Current time: 2020-05-07 15:22:17 User name: drozmano
In this case R does not print any extra information, and only explicitly printed values are shown in the output, the commands themselves are not printed.