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.
Keeping you updated with latest technology trends, Join DataFlair on Telegram
Steps to Install R, RStudio, and R Packages
Here we will discuss the simple steps to install RStudio. But, before installing RStudio, we will have to install R. So, first, let’s learn the process of R installation.
We have to follow three basic steps in the same order to run R and RStudio on your system.
- Install R
- Then, install RStudio
- 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:
- Download the latest version of the .pkg file from the following link:
- 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.
Lastly, for Windows
Step 1: Go to the website – CRAN R Project Windows
Step 2: Click on the “Download R 3.6.0 for Windows”
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.
Step 5: Clicking on Next will lead you to the important information of the license. Click on Next.
Step 6: Then, it will provide you with an interface for selecting the destination for your 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.
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.
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.
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.
Step 11: Clicking on Next will prompt the installation to start. After this, the installation is complete and R is installed.
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 – RStudio download
Step 2: Here, you will find various versions of R. Click on the Download button.
Step 1: Clicking on Download will shift the page lower to the various platforms of R installation. Click on Windows 7+ (64 Bit)
Step 2: After the download is finished, click on the installer and click Next.
Step 3: Clicking on Next will direct you to the installation location.
Step 4: Clicking on Next will direct you to choose the Start Menu Folder.
Step 5: Clicking on Install will start the installation process.
Step 6: Finally, click on the Finish button.
Step 7: In the final step, test if R is properly installed by opening RStudio and typing the following lines:
> line <- "Hello World" > line
You have successfully installed R on Windows!
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 packages 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.
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 date package with the help of the update.packages() function.
Doing so will prompt the system to ask your permission prior to the 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 the 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()), 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.Here are a few packages, we recommend to a beginner:
- Tidyversre: tidyverse is a collection of packages that are useful for many tasks like data management, data reshaping, data formatting, memory management, etc.
- Ggplot2: the ggplot2 package is the most popular graphics package in R. It extends R’s graphical capabilities by a large margin able to make static, dynamic as well as animated graphics with simple commands.
- Installr: the installr package is highly useful for installing, updating and managing versions of R, RStudio, and R packages.
- Stringr: Stringr is a package very popular for its string manipulation functions. Stringr functions are much easier to use and more powerful than the base R functions.
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 15,000 extension packages that have been contributed to CRAN.
Basic R Concepts
- The interpreter is a special program that processes the programs written in the 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:
- After pressing the Enter key, the R interpreter executes and returns the answer to the user.
- 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
Every good package consists of documentation. Users 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.
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.
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.