Home Programming Modern R Programming Cookbook

Modern R Programming Cookbook

By Jaynal Abedin
books-svg-icon Book
eBook $39.99 $27.98
Print $48.99
Subscription $15.99 $10 p/m for three months
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
eBook $39.99 $27.98
Print $48.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Installing and Configuring R and its Libraries
About this book
R is a powerful tool for statistics, graphics, and statistical programming. It is used by tens of thousands of people daily to perform serious statistical analyses. It is a free, open source system whose implementation is the collective accomplishment of many intelligent, hard-working people. There are more than 2,000 available add-ons, and R is a serious rival to all commercial statistical packages. The objective of this book is to show how to work with different programming aspects of R. The emerging R developers and data science could have very good programming knowledge but might have limited understanding about R syntax and semantics. Our book will be a platform develop practical solution out of real world problem in scalable fashion and with very good understanding. You will work with various versions of R libraries that are essential for scalable data science solutions. You will learn to work with Input / Output issues when working with relatively larger dataset. At the end of this book readers will also learn how to work with databases from within R and also what and how meta programming helps in developing applications.
Publication date:
October 2017
Publisher
Packt
Pages
236
ISBN
9781787129054

 

Installing and Configuring R and its Libraries

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
 

Introduction

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.

 

Installing and configuring base R in Windows

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.

Getting ready

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:

  1. Visit https://cran.r-project.org/bin/windows/, which will show you the following screen:
  1. 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.
  2. 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:
  1. 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.

How to do it...

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:

  1. Go to the folder where you have stored the executable file that you have downloaded, by following the instructions in the previous section.
You must have administrator privileges to install the software.
  1. If you have administrator privileges, then just double-click on the executable file, or right-click on the mouse and select Run as administrator:
  1. In Windows 7, it will show a notification with the title User Access Control. In this case, you must choose Yes to proceed.
  2. 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:
  1. Click on Next > on this page to proceed with the installation:
  1. 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:
  1. 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:
  1. 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:
  1. 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:
  1. 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:
  1. 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:

How it works...

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:

  1. Right-click on the Computer icon and select Properties from the following screen:
  1. Now, click on Advanced system settings. It will open up the System Properties window as shown in the following screenshot:
  1. At the bottom of the System Properties window, you will see Environment Variables…. Click on it and it will show the following screen:
  1. The Environment Variables window will pop up in which you will see two separate sections, one for User variables and another for System variables.
  2. In the System variables section, select the Path variable and then click on Edit…:
  1. 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.
  2. 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:

  1. Go to the Start menu.
  2. Type cmd in the search box.
  3. Then, click on cmd.exe. It will open a black screen called Windows Command Prompt:
  1. Type R and hit Enter. If the R console opens, then everything is alright and you have correctly configured R in Windows:

There's more...

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:

See also

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.

 

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.

Getting ready

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:

lsb_release -a

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.

How to do it...

Once you have the version number, you simply need to install base R onto your computer. The step-by-step procedure is as follows:

  1. Update etc/apt/source.list by adding an entry as follows:
        deb <cran mirror address>/bin/linux/ubuntu trusty/
  1. 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.
  2. Once you have performed the preceding step, type the following command to install R:
      sudo apt-get update
sudo apt-get install r-base
  1. 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:
  1. 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
Your computer must connect to the internet to install R.

There's more...

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.

See also

To understand how to install an IDE into either Windows or Linux, follow the next recipe.

 

Installing and configuring RStudio IDE in Windows

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.

Getting ready

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:

  1. To download open source components of RStudio IDE, visit the web page at https://www.rstudio.com/products/rstudio/.
  1. 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:

How to do it…

The following steps will help you go through the installation process:

  1. Go to the folder where you have stored the executable file of the latest release of RStudio:
  1. Right-click on it and select Run as administrator. The installation Wizard will open. Then, click on Next > on this screen.
  1. 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 >:
  1. Now, you will be asked to give a name of the start menu folder and then click on Install.

How it works…

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.

There's more…

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).

See also

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.

 

Installing and configuring RStudio IDE in Linux

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.

Getting ready

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.

How to do it…

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:

  1. To install gdebi-core, run the following command:
      sudo apt-get install gdebi-core
  1. To download the RStudio IDE installer .deb file, run the following command:
      wget https://download1.rstudio.org/rstudio-1.0.136-amd64.deb

The preceding command would generate the following output:

  1. To install the IDE, run the following command:
      sudo gdebi rstudio-1.0.136-amd64.deb

The preceding command will generate the following output:

  1. 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:
During installation, make sure you are connected to the internet.

How it works...

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.

See also

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.

 

Installing and configuring R tools for Visual Studio in Windows

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.

Getting ready

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

How to do it…

How it works…

Once the installation is completed, the user interface is similar to RStudio. Here is the screenshot of RTVS:

See also

 

Installing R libraries from various sources

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).

Getting ready

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

How to do it…

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.

To install packages using this approach, the computer must have an active internet connection.

The ggplot2 library

Lets take a look at the following steps to install the ggplot2 library:

  1. Open the R console or terminal and then type the following command:
      install.packages("ggplot2")

The preceding command line will then ask you to select a server as follows:

      install.packages("ggplot2")
--- Please select a CRAN mirror for use in this session ---
  1. 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=.

The devtools library

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.

Installing a library from GitHub

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:

    library(devtools)
install_github("hadley/dplyr")

Installing a library from the BioC repository

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:

    source(https://bioconductor.org/biocLite.R)
biocLite("GenomicFeatures")

How it works…

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:

    install.packages("ggplot2", lib="c:/rPackages")

There's more…

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:

  • install_github()
  • install_bioc()
  • install_bitbucket()
  • install_cran()
  • install_git()

See also

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.

 

Installing a specific version of R library

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.

Getting ready

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:

    install.packages("versions")

How to do it…

Take a look at the following steps:

  1. If you want to check which version of ggplot2 has been installed onto your computer, you can run the following command:
        library(versions)
installed.versions("ggplot2")

The preceding command will show the following output on the console screen:

        > installed.versions("ggplot2")
Checking package in 'C:/rPackages'
(as 'lib' is unspecified)
[1] "2.2.0"
>
  1. 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")

How it works…

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.

About the Author
  • Jaynal Abedin

    Jaynal Abedin is currently doing research as a PhD student at Unit for Biomedical Data Analytics (BDA) of INSIGHT at the National University of Ireland Galway. His research work is focused on the sports science and sports medicine area in a targeted project with ORRECO --an Irish startup company that provides evidence-based advice to individual athletes through biomarker and GPS data. Before joining INSIGHT as a PhD student he was leading a team of statisticians at an international public health research organization (icddr,b). His primary role there was to develop internal statistical capabilities for researchers who come from various disciplines. He was involved in designing and delivering statistical training to the researchers. He has a bachelors and masters degree in statistics, and he has written two books in R programming: Data Manipulation with R and R Graphs Cookbook (Second Edition) with Packt. His current research interests are predictive modeling to predict probable injury of an athlete and scoring extremeness of multivariate data to get an early signal of an anomaly. Moreover, he has an excellent reputation as a freelance R programmer and statistician in an online platform such as upwork.

    Browse publications by this author
Modern R Programming Cookbook
Unlock this book and the full library FREE for 7 days
Start now