Posted on September 8, 2023 by nexonhost
How To Install R On CentOS 7
This tutorial will teach you how to install R on CentOS 7 and how to install R packages from the official Comprehensive R Archive Network (CRAN) .
Installing R on Centos
At the time of writing the latest stable version of R is version 3.5.
To install R on CentOS 7, follow these steps:
R packages are available in the EPEL repositories. If you don’t have EPEL repository installed on your machine you can do it by typing:
sudo yum install epel-release
You will be prompted to accept the repository GPG key, type y and hit Enter.
Once the repository is added, install R by typing:
sudo yum install R
R is a meta package that contains all the necessary R components.
Verify the installation by typing the following command which will print the R version:
R --version
R version 3.6.0 (2019-04-26) -- "Planting of a Tree" Copyright (C) 2019 The R Foundation for Statistical Computing Platform: x86_64-redhat-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under the terms of the GNU General Public License versions 2 or 3. For more information about these matters see https://www.gnu.org/licenses/.
Installing R Packages from CRAN
One of the main reasons why R is so popular is the wide array of packages available through the Comprehensive R Archive Network (CRAN).
For demonstration purposes, we’ll install a package named stringr , which provides fast and correct implementations of common string manipulations.
When started as root the packages will be installed globally and available for all system users. If you start R without sudo, a personal library will be set up for your user.
Start by opening the R console as a root:
sudo -i R
R version 3.6.0 (2019-04-26) -- "Planting of a Tree" Copyright (C) 2019 The R Foundation for Statistical Computing Platform: x86_64-redhat-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. >
Install the stringr package by running the following command:
install.packages("stringr")
You will be asked to select a CRAN mirror:
Installing package into ‘/usr/lib64/R/library’ (as ‘lib’ is unspecified) --- Please select a CRAN mirror for use in this session --- Secure CRAN mirrors
Select the mirror that is closest to your location.
The installation will take some time and once completed, load the library by typing:
library(stringr)
Next, create a simple character vector named tutorial:
tutorial <- c("How", "to", "Install", "R", "on", "CentOS", "7")
Run the following function which will print the length of each string:
str_length(tutorial)
[1] 3 2 7 1 2 6 1
Conclusion
You have successfully installed R your CentOS machine and learned how to install R packages.