In this chapter, you will be exposed to the recipes on how to install and configure R and its libraries in Windows and Linux platforms. Specifically, you will come across the following recipes:
- Installing and configuring base R in Windows
- Installing and configuring base R in Linux
- Installing and configuring RStudio IDE in Windows
- Installing and configuring RStudio IDE in Linux
- Installing and configuring R tools for Visual Studio in Windows
- Installing R libraries from various sources
- Installing a specific version of R library
It is expected that you have basic knowledge of installing software on the platform that you use regularly. However, it is helpful to have an overview of some technical aspects of installing R and the integrated development environment (IDE) such as RStudio. This chapter will serve as a reference point for the technical issues during installation and configuration of R and its libraries for Windows and Linux platforms. Each of the recipes contains detailed description with the necessary screenshots so that you feel very comfortable, even if you are not in front of your computer. After completing all the recipes of this chapter, you will be confident enough to install R and its libraries in Windows and Linux platforms. So, let’s get started.
In this recipe, you will learn to install R in the Windows platform and we will address other necessary configuration issues that are related to the Windows operating system.
To start this recipe, you will need to know your version of the Windows operating system, for example, whether it is Windows 7, 8, or 10. Also, you need to know specific architecture, such as 32-bit or 64-bit. Once you know the particulars of the operating system, you are ready to install base R by following the steps in the next section. Another thing that you need to check is whether R is already installed on your computer or not. You can easily check by inspecting the Start menu or task bar or desktop icon. Now, let's assume that you did not install R previously in your computer and this is the first time you are going to do so.
Once you get detailed information about your operating system, you will need to download the executable file for the Windows operating system. To find the latest version of R, you can visit the Comprehensive R Archive Network (CRAN) at http://cran.r-project.org/. On this web page, you will get to know the latest release of R and other related information.
To download the latest release of R for Windows, perform the following steps:
- Visit https://cran.r-project.org/bin/windows/, which will show you the following screen:
- On this web page, you will see base under the Subdirectories category. As a first-time user of R, you need to download the executable file from this link.
- Once you click on base, you will end up on this page, https://cran.r-project.org/bin/windows/base/ as shown in the following screenshot:
- Now, click on Download R 3.x.x (This version number might differ because during preparation of this recipe, the version was 3.3.3) for Windows. The executable file will be downloaded into your local storage.
Once you have downloaded the executable file, you are ready to install it on your computer. Perform the following steps and choose the options accordingly. The screenshots are for your convenience only:
- Go to the folder where you have stored the executable file that you have downloaded, by following the instructions in the previous section.
- If you have administrator privileges, then just double-click on the executable file, or right-click on the mouse and select Run as administrator:
- In Windows 7, it will show a notification with the title User Access Control. In this case, you must choose Yes to proceed.
- The first thing it will ask you to do is choose a language, as shown in the following screenshot. Once you select your chosen language, click on the OK button. The R Setup Wizard will appear, and you will see various options to select in different pages:
- Click on Next > on this page to proceed with the installation:
- On this page, the GNU GENERAL PUBLIC LICENSE information will be displayed, and you will be asked to read and understand the licensing agreement and then click on the Next > button:
- Now, you will be asked to choose the destination folder where you want to store the installation files. Usually, by default, it shows C:/Program Files/, and sometimes, it also shows one more level C:/Program Files/R/R-x.x.x. You can keep the default location, or you can choose a separate destination based on your choice. Once you have selected the destination, click on the Next > button:
- At this stage, you will get the option to select the component that you want to install. You can choose either 32-bit Files only, 64-bit Files only, or both along with core files and message translation. After selecting the components, again click on the Next > button:
- Now, you will be asked whether you want to customize your startup or accept the defaults. The recommended option at this stage is to accept the defaults and click on the Next > button:
- You can decide whether you want to create the start menu folder or not and you can specify its name at this stage of installation:
- Finally, you will be given the option to choose whether you want to create a desktop icon and also create a quick launch icon. You can specify whether you want to save the version number into the computer's registry or not, and the last option is to associate file name extensions .R and .RData with the software. Once you are done with selecting the options, click on Next > and the installation will be started:
Once you follow the steps described in the How to do it… section, the R software will be installed, and it's ready to use. You can open R using either the desktop icon or quick launch icon, or from the Start menu.
However, to do some advanced-level work in R or if you want to open the R console from the Windows Command Prompt, you need to do some configuration that is specific to the operating system. In this subsection, you will see how the configuration works.
If you want to access the R terminal from the Windows Command Prompt, then you must open the Windows Command Prompt, and then type R and press Enter. However, without correctly configuring R, you won't be able to open the R terminal in this way. The following are the steps to configure R:
- Right-click on the Computer icon and select Properties from the following screen:
- Now, click on Advanced system settings. It will open up the System Properties window as shown in the following screenshot:
- At the bottom of the System Properties window, you will see Environment Variables…. Click on it and it will show the following screen:
- The Environment Variables window will pop up in which you will see two separate sections, one for User variables and another for System variables.
- In the System variables section, select the Path variable and then click on Edit…:
- Now, in this Path variable, add the location where you have installed R software. Choose the location where the R.exe and R.dll file belongs, for example, C:\Program Files\R\R-3.3.2\bin\x64.
- Put the location at the end of the Path variable after putting in a semicolon (;) and then click on OK on each of the window opened.
Once you have performed the preceding steps, it is the time to check whether you have correctly configured environment variables by performing the following steps:
- Go to the Start menu.
- Type cmd in the search box.
- Then, click on cmd.exe. It will open a black screen called Windows Command Prompt:
- Type R and hit Enter. If the R console opens, then everything is alright and you have correctly configured R in Windows:
If you want to customize the R console, its display font, font size, and others, you can edit the preferences from the menu. For example, navigate to Edit | GUI preference…. You will see the following screen. You are now able to change anything you want to customize your R console and other things:
In this recipe, you have gone through the steps based on the Windows operating system. If you want to install and configure R for Linux, see the next recipe Installing and configuring base R in Linux.
It is not necessarily the case that every user will use Windows platform. In this recipe, you will see how to install and configure R on the Linux platform. Each of the steps is described with an appropriate screenshot so that you can easily understand the steps.
To install R in the Linux operating system, first, you need to know the version of Linux you are using. To know the version of Linux installed on your computer, there are many options. One of the options is the following command:
This command will give the release information as shown in the following screenshot:
In this recipe, you will see the example from the Ubuntu 14.04 release.
Once you have the version number, you simply need to install base R onto your computer. The step-by-step procedure is as follows:
- Update etc/apt/source.list by adding an entry as follows:
deb <cran mirror address>/bin/linux/ubuntu trusty/
- Replace <cran mirror address> by putting an actual mirror link from the list of available mirrors from https://cran.r-project.org/mirrors.html. Remember that you must have administrator privileges to edit etc/apt/source.list.
- Once you have performed the preceding step, type the following command to install R:
sudo apt-get update
sudo apt-get install r-base
- The preceding two commands will complete the installation process and you can check whether R has been installed correctly or not, by typing R and hitting Enter on the Terminal window. If the R console opens, then you are ready to go:
- Some of the users may need to compile R packages from the source code. In that case, you will need to install the following component of R:
sudo apt-get install r-base-dev
In this recipe, you have installed base R into Ubuntu 14.04 release. If your operating system has another release of Linux, then visit https://cran.r-project.org/, select Download R for Linux, and follow the steps outlined there.
To understand how to install an IDE into either Windows or Linux, follow the next recipe.
In the first two recipes, the steps were related to installing base R. To use R with an IDE where syntax highlighting features are available along with other options, you will need to install a preferred IDE software. RStudio is one of the most popular IDEs for R. In this recipe, you will learn from where, and how, to download RStudio and install it onto your computer.
To install an IDE, the first step is to install base R; that is, you need to make sure R has been installed prior to installing RStudio IDE. RStudio is an open source and enterprise-ready professional software for R. Now, let's take a look at the following few steps to download and install RStudio IDE:
- To download open source components of RStudio IDE, visit the web page at https://www.rstudio.com/products/rstudio/.
- From this link, choose RStudio for desktop and download the executable file. The latest version of RStudio is 1.0.136 at the time of the preparation of this recipe. Select an appropriate executable file from the list that matches with your operating system:
The following steps will help you go through the installation process:
- Go to the folder where you have stored the executable file of the latest release of RStudio:
- Right-click on it and select Run as administrator. The installation Wizard will open. Then, click on Next > on this screen.
- At this stage, you will be asked to give the location where you want to install it. The default location usually shows as C:\Program Files\RStudio. However, if you want to install into a different location, then you can do this from here and click on Next >:
- Now, you will be asked to give a name of the start menu folder and then click on Install.
Once you run the executable file of RStudio, it then checks whether there is any base R installed in the computer. If you do not already have R installed, then you will be given a message stating that base R is required to install RStudio. Once you have made sure that base R is already installed, it will install the IDE without any errors by following the steps mentioned in the How to do it… section.
The following screenshot is the initial user interface of RStudio where you will see four major components:
Let's take a look at the following four major components shown in the preceding screenshot:
- In the upper-left section is the R script writing section where you will usually write R code to solve your data analytics, data management, or data visualization problems.
- In the lower-left section is the R console where you will be able to see immediate output and error messages along the line of code that has been run already.
- The upper-right section contains the history of R sessions and the current global environment. The environment section displays all current active R objects, their type, and other information.
- The lower-right corner contains several subcomponents, for example, the Help documentation window, graphical output (the Plots tab), the Files tab showing the list of files and folders in the selected directory, and also the list of R packages (the Packages tab).
There is another similar IDE available, R tools for Visual Studio. This is another IDE developed and released by Microsoft. You will come into this recipe in this chapter.
In this recipe, you will see how to install RStudio IDE in the Linux operating system. The RStudio IDE user interface will be the same as that in Windows, but the installation process is different. You will walk through the process with the appropriate screenshot.
To install RStudio IDE, the prior requirement is to install base R. Let's assume that base R is already installed on the computer. Now, visit the following web page and download the appropriate installer from the available list at https://www.rstudio.com/products/rstudio/ and store it in the home directory for convenience. Depending on the version of Linux, the executable file comes with the various formats. In this recipe, you will download the .deb file and store it in the home directory. Also, you can directly install it from the internet using the wget command with the appropriate link to the file such as https://download1.rstudio.org/rstudio-1.0.136-amd64.deb.
Once you have downloaded the appropriate executable file, you are ready to install it. Since, you have downloaded the .deb file, you will require gdebi-core to execute that file. Here are the steps to install gdebi-core and then install RStudio IDE:
- To install gdebi-core, run the following command:
sudo apt-get install gdebi-core
- To download the RStudio IDE installer .deb file, run the following command:
The preceding command would generate the following output:
- To install the IDE, run the following command:
sudo gdebi rstudio-1.0.136-amd64.deb
The preceding command will generate the following output:
- In the preceding screenshot, the terminal is asking whether you want to install the software or not. Type y and hit the Enter button here. Once the installation process is completed, you will see the following screen in the Terminal:
First, the sudo apt-get install gdebi-core command helps you download and install the gdebi-core package into the Linux system. By using the wget command, you are actually downloading the necessary file to install RStudio from the RStudio website. After that when you enter the sudo gdebi rstudio-1.0.136-amd64.deb command, it installs the software for you, and after completing this step, you are ready to use it.
To know more about RStudio, you can visit the RStudio website for more documentation, but this recipe covered the most basic steps to install the software.
R Tools for Visual Studio (RTVS) is another development environment for R like RStudio, but it has extended facilities developed by Microsoft. RTVS is an extension of Visual Studio 2015, and it's free with the Community Edition of Visual Studio 2015. In this recipe, you will go through the installation process.
To install RTVS, your computer needs to fulfill some of the following prerequisites:
- Visual Studio 2015 Community Edition (this is freely available)
- Visual Studio Update 3 to install RTVS
- Microsoft R Open/Open source base R 64-bit with 3.2.1 or higher
- Microsoft R Client
Let's take a look at the following steps:
- Download and install Visual Studio 2015 Community Edition and install Update 3 or install Visual Studio Community 2017.
- Install Microsoft R Open or CRAN R 64-bit with R version 3.2.1 or higher.
- Download and install R Client from https://msdn.microsoft.com/en-us/microsoft-r/r-client-get-started:
- Once you have completed the prerequisites, download and install RTVS from https://aka.ms/rtvs-current:
Once the installation is completed, the user interface is similar to RStudio. Here is the screenshot of RTVS:
Here are the relevant links for more information about RTVS. It is suggested that you take a look at them for updated information:
R library or packages refer to a collection of previously programmed functions for specific tasks. When you install base R, you will see that it comes with a number of default libraries installed, but users need to use customized libraries to solve their problems. In this recipe, you will see how you can install libraries from different sources, such as CRAN, GitHub, and Bioconductor (BioC).
Suppose you are interested in visualizing your data using the ggplot2 library, but when you call the library using the library(ggplot2) code, you end up getting an error saying that ggplot2 is not found. Now, you need to install ggplot2. In this recipe, you will install the following libraries from the sources mentioned:
- The ggplot2 library from CRAN
- The devtools library from CRAN
- The dplyr library from GitHub
- The GenomicFeatures library from BioC
Under the default utils library, there is a function called install.packages() to install a package from within the R console. You can use the command install.packages(). This command will prompt you to select the appropriate server CRAN.
Lets take a look at the following steps to install the ggplot2 library:
- Open the R console or terminal and then type the following command:
The preceding command line will then ask you to select a server as follows:
--- Please select a CRAN mirror for use in this session ---
- It will now install ggplot2 and its dependent libraries. If you want to avoid selecting a mirror server, then you can specify the mirror server within the install.packages() function using repos=.
This is another library that extends the functionalities of the utils library of base R. This library is convenient for developing various tools within R, and using this library, you can install the required library from GitHub. To install devtools along with its dependent libraries, use the install.packages() function as you did for ggplot2.
To install any library from GitHub, you can use the install_github() function from the devtools library. You just need to know the name of the library and the GitHub ID of the repository owner. See the following installation code for the dplyr library from GitHub:
To install any library from the BioC repository, you have to use the biocLite.R file and then use the biocLite() function to install the library. The following code snippet is to install the GenomicFeatures library from the BioC repository:
In any of the commands to install a library, either install.packages(), install_github(), or biocLite(), first, it connects with the mirror server where the source code / binary-released version of the specified library is located. Then, it checks whether the dependent libraries are installed onto the computer or not. If the required dependent library is absent, then it will download and install those required libraries before installing the one you specified through the command. The command will also search for the location where the installed library will be stored. You can explicitly specify the location or you can use the default. The recommended approach is to specify a location and install all customized libraries into that folder.
To specify the installation location, you can use the lib= option within the function. Make sure you have created the folder that you are going to use as the destination folder. Here is an example:
Using devtools, you can install from either CRAN or from GitHub or even from the BioC repository. There are specific functions available within the devtools library as follows:
The user might need a specific version of a library to do a certain task, and that version could be an older one. To install the specific version of the library, see the next recipe that talks about it.
Open source software is being updated by the maintainers and new features are being added to the existing ones. Sometimes, a task is dependent on a specific version of a library. In this recipe, you will see how to install a specific version of a library and how to check the version of an installed library.
To install a specific version of an R library, there is another facility available. The necessary functions are bound into the versions library to do this job. You need to install versions before using the function within it. To get ready, you have to run the following command:
Take a look at the following steps:
- If you want to check which version of ggplot2 has been installed onto your computer, you can run the following command:
The preceding command will show the following output on the console screen:
Checking package in 'C:/rPackages'
(as 'lib' is unspecified)
- Now, to install the required version of any library, you first need to know the version number and how that has been written in the library documentation. Then, use that version number as follows:
install.versions("ggplot2", versions = "2.2.0")
Whenever you call the installed.versions() function, it looks at the DESCRIPTION file of that library and extracts the version number and displays it on the console.
Alternatively, whenever you call install.versions() to install the specified version of a library, it checks the date of that version and runs the install.dates() function internally to download the appropriate version onto the computer.