How to Install R, RStudio and R Packages – The Easiest Process Ever!

With this tutorial, we will learn to install RStudio and R for Mac, Linux and Windows along with its process of updating in all these operating systems. We will also cover the concept of R packages in detail.

Let’s start the R installation tutorial.

Stay updated with the latest technology trends while you're on the move - Join DataFlair's Telegram Channel

Steps to Install R, RStudio, and R Packages

Here we will discuss the simple steps to install R. But, before installing R, we will have to install RStudio. So, first, let’s learn the process of RStudio installation.

We have to follow three basic steps in the same order to run R and RStudio on your system.

  1. Install R
  2. Then, install RStudio
  3. Lastly, install R Packages

1. Install R

The process of installing R depends on the type of your operating system. R can be installed in the following ways:

For Mac

  • Download the latest version of the .pkg file from the following link:

https://cran.r-project.org/bin/macosx/

  • Open the downloaded .pkg file and install R.

Now, for Linux

  • For Ubuntu with Apt-get installed, execute sudo apt-get install r-base in terminal.

R-Installation-Ubuntu-Command

Lastly, for Windows

Step 1: Go to the website – https://cran.r-project.org/bin/windows/base/

Step 2: Click on the “Download R 3.6.0 for Windows”

cran r project - Install R

Step 3: Clicking on the tab will download the R installer. Double click on the installer to launch it.

Step 4: Select the language of your choice in the installer and click OK.

select language - Install R

Step 5: Clicking on Next will lead you to the important information of the license. Click on Next.

information of license - Install R

Step 6: Then, it will provide you with an interface for selecting the destination for your R installation.

destination for R installation

Step 7: After clicking on Next, you will be directed to select components for installation. It is ideal to keep all the components.

Components for R Installation

Step 8: In the next step, you will be asked if you would want to customize the startup. Click on No(accept defaults) as an ideal option.

Customize Startup Options - Install R

Step 9: In the next step, you will be asked to select the start menu folder. By default, start menu folder cannot be created but you can change that by unchecking the checkbox.

select start menu folder - Install R

Step 10: In the next step, you will be asked for additional details. You can create a desktop shortcut or a Quick Launch Startup by checking the boxes.

Additional Tasks for R Installation

Step 11: Clicking on Next will prompt the installation to start. After this, the installation is complete and R is installed.

Final R Installation Load

You must definitely check the Latest Features of R Programming

2. Install RStudio

Now, in order to install the IDE RStudio, we will take the following steps:

Step 1: Go to the website – https://www.rstudio.com/products/rstudio/download/

Step 2: Here, you will find various versions of R. Click on the Download button.

Step 3: Clicking on Download will shift the page lower to the various platforms of R installation. Click on Windows 7+ (64 Bit)

Installers for Supported Platforms

Step 4: After the download is finished, click on the installer and click Next.

RStudio Setup Wizard

Step 5: Clicking on Next will direct you to the installation location.

RStudio Installation Location

Step 6: Clicking on Next will direct you to choose the Start Menu Folder.

Start menu folder

Step 7: Clicking on Install will start the installation process.

RStudio-Files-Extract

Step 8: Finally, click on the Finish button.

RStudio Finish option

Step 9: In the final step, test if R is properly installed by opening RStudio and typing following lines:

> line <- "Hello World"
> line

Output:

line hello world

You have successfully installed R!

3. Install the R Packages (Optional)

In RStudio, if you require a particular library, then you can go through the following instructions:

  • First, run RStudio.
  • After clicking on the packages tab, click on install. The following dialog box will appear.
  • In the Install Packages dialog, write the package name you want to install under the Packages field and then click install. This will install the package you searched for or give you a list of matching package based on your package text.

Updating R and RStudio

1. Updating R

Mac and Ubuntu

On Mac and Ubuntu, you need to go to the CRAN website, https://www.r-project.org/ to install the newer package installer.

Windows

A package called installr, which is only for Windows can be used to ease the process. Updating on windows is tricky.

If you don’t have installr package then first install it

#installing the latest installr package:

install.packages(“installr”); library(installr) # For installing & loading installr

updateR()

After running the updateR(), R will detect if there is a new version available. If there is, it will download and install it.

2. Updating RStudio

Updating RStudio is easy, go to Help > Check for Updates to install a newer version.

Don’t forget to explore the RStudio Tutorial for Beginners

3. Updating R Packages

3.1 Updating Out of date Packages

Install.packages() is used to install any package of CRAN. For example:

Typing Install.packages(“ggplot2”) in the console will install the latest package of ggplot2

You can also update any out of data package with the help of update.packages() function. 

update.packages()

Doing so will prompt the system to ask your permission prior to installation of the package. In order to not let the system prompt for your permission, use the ask = False attribute in the packages() function. 

update.packages(ask = FALSE)

Unfortunately, this won’t update packages installed by devtools::install_github().

3.2 Updating all Packages after R update

By using package installr, we can handle it on windows. But for other operating systems, after updating R, a lot of packages might have been built under the old R version. It would be safer to re-install all the packages already installed. This can be done with the following commands. Again this won’t update packages installed with devtools::install_github().

## get packages installed
packs = as.data.frame(installed.packages(.libPaths()[1]), stringsAsFactors = F)
## and now re-install install packages using install.packages()
install.packages(packs$Package)

Master the complete process of Installing and Using Packages in R Programming

R Packages (Libraries)

Packages comprise of R functions, data and compiled code that are present in a well-defined format. The location where the packages are installed is known as a library. R comes with a standard set of packages. Others are available for download and installation. If we will install it once, we have to be load it into the session to be used.

1. Recommended Packages

  • In packages, we use many R functions, free libraries of code written by R’s active user community.
  • Open an R session and type this in the command line to install an R package.
install.packages("<the package's name>")
  • R package will be downloaded from CRAN.

There are thousands of useful R packages for us, but navigating them all can be a challenge.

2. Basic Notions

The R Language

  • Statistical computations are carried from R computer language.
  • R was created by Robert Gentleman and Ross Ihaka at the University of Auckland.
  • It has now evolved into a featured tool for carrying out statistical operations.

The R Project

  • It is an international collaboration of researchers who work on statistical computing. 
  • 20 members are responsible for the maintenance and enhancement of R. 
  • Releases of the R environment are made through the CRAN (Comprehensive R Archive Network) twice per year.
  • The software has a ‘free software’ license which makes it possible for anyone to download and use it.
  • There are over 3500 extension packages that have been contributed to CRAN.

Basic R Concepts

  • The interpreter is a special program that processes the programs written in R language. It converts the R code to machine code. 
  • An interpreter is also responsible for translating a program, line by line into its corresponding machine code.
  • On UNIX or LINUX machines, we can start R by typing the command “ R ” to a command interpreter prompt in a terminal window.
  • Better yet, we can run R within an Emacs window by typing M-x R.

Basic Interaction with R Console

  • The most common form of interaction with R is through the command line in the console.
  • The user enters the command in the console. 
    • The user types a command:
print('Testing')

Output:

print testing - Install R

  • After pressing the Enter key, the R interpreter executes and returns the answer to the user.

Output:

testing 2

  • It is also possible to store a sequence of commands in a file. Use .R extension and then ask R to execute all commands in the file that has .R extension.

Following is an example of a calculator made with R:

  • It is also possible to store a sequence of commands in a file. Use .R extension and then ask R to execute all commands in the file that has .R extension.
  • We may also use the console as a simple calculator.

Using R as a Calculator

  • Users type expressions to the R interpreter.
  • R responds by computing and printing the answers.
> 1+2
> 1/2
> 11^2

Output:

calculator

Documentation

Every good package consists of documentation. User will not know how to make sense of the package. Documentation is also useful for future-you, and for developers extending your package.

  • Object documentation is a type of reference documentation. It works like a dictionary.
  • If you are acquainted with the name of the object, documentation is really helpful. 

Datasets

There are a large number of datasets that are open for free analysis in R. Furthermore, there are other open datasets that are available on Kaggle which you can directly utilize for analyzing and predicting. 

Summary

We understood the process of installing RStudio and R in all the operating systems. We also learned the steps to update R, RStudio and R packages.

Learn to implement various Data Types in R Programming Language

Any feedback or suggestion for us? Share your views in the comment section below. We will be glad to hear from you.

1 Response

  1. Manish says:

    Error: Unable to establish connection with R session
    Above error shown in my system at the time of launching of RStudio.
    I am having windows 7(64bit).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.