R#
R is a free programming language and software environment specifically designed for statistics.
R at NHPCC#
Multiple versions of R are available. The default version can be loaded and run by typing in your terminal (you need to request an interactive job by using the Interactive Shell app or the srun command):
ml R
and then:
R
to start the interactive R session.
R version 4.3.2 (2023-10-31) -- "Eye Holes"
Copyright (C) 2023 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.
>
Using R in batch scripts#
#!/usr/bin/bash
#SBATCH --time=00:10:00
#SBATCH --mem=10G
# load the module
ml R
# run R code
# Function to compute the area of a circle
circle_area <- function(radius) {
area <- pi * radius^2
return(area)
}
# Example usage
radius <- 5
area <- circle_area(radius)
print(paste("The area of the circle with radius", radius, "is", area))
Installing R packages#
The paths to system and user packages can be seen with this R command:
.libPaths()
In the interactive R session, you can install an R package (e.g. microbenchmark) using:
install.packages("microbenchmark")
This will download and install the package from the default CRAN repository along with any dependencies. You can install several packages at once by passing a character vector:
install.packages(c("dplyr", "ggplot2", "tidyr", "readr"))
To speed up downloads, you can specify a closer or faster CRAN mirror. For example, a CRAN mirror in Iran:
install.packages("microbenchmark", repos = "https://cran.um.ac.ir/")
Or set it as the default repository for your session:
chooseCRANmirror()
# Select the mirror number from the list, or set it manually:
options(repos = c(CRAN = "https://cran.um.ac.ir/"))
To Check installed packages and versions:
installed.packages()
Or for a specific package:
packageVersion("dplyr")
Removing R packages#
To remove an installed package, use the remove.packages() function:
remove.packages("microbenchmark")
You can remove several packages at once by passing a character vector:
remove.packages(c("dplyr", "ggplot2", "tidyr"))
Working with Rstudio#
To use RStudio, you can access it via the Rstudio app in our Ondemand Web Portal.