Hello,
I have a custom R script, that I need to run on HPC server, not in Rstudio. I am unable to run this script;
How can I start R code in Linux shell?
There are two preferred ways to explicitly invoke R scripts, either via R CMD BATCH (old) or Rscript (newer).
Make sure that Rscript is available to you on the server, i.e. just typing Rscript should print the manual for Rscript to the screen.
Rscript
Usage: /path/to/Rscript [--options] [-e expr [-e expr2 ...] | file] [args]
--options accepted are
--help Print usage and exit
--version Print version and exit
--verbose Print information on progress
--default-packages=list
Where 'list' is a comma-separated set
of package names, or 'NULL'
or options to R, in addition to --slave --no-restore, such as
--save Do save workspace at the end of the session
--no-environ Don't read the site and user environment files
--no-site-file Don't read the site-wide Rprofile
--no-init-file Don't read the user R profile
--restore Do restore previously saved objects at startup
--vanilla Combine --no-save, --no-restore, --no-site-file
--no-init-file and --no-environ
Rscript myScript.r --vanilla should then do the trick (or whatever other parameter for Rscript you prefer. I recommend to check that all packages your script may rely on are actually installed on the server. For this, you could open an interactive R session simply by typing $ R. This will create the same type of session that you normally have in your RStudio console (if R is installed on the server and supplied in your path. Sometimes you will have to figure out the path to where the software on the servers is installed, but that's independent of R).
PS: The solution suggested above where the script is executed via the ./myScript.R expects that the first line in that script points to Rscript or the R installation via a shebang line. You can read more details on stackoverflow, e.g. here.
Assuming your script has a proper shebang: