The first step in using R and R Studio is to download the two programs from the Internet. R must be downloaded first. R and R Studio can be downloaded for the modern operating systems Windows, Mac OS X, and Linux. In this chapter, you will learn how to download and install R plus the 30 basic packages and R Studio, as well as how to install other packages and update R. Updating packages in R Studio is covered in Chapter 2. You will also learn how to use R in individual folders within the file system of the computer.

Downloading R and R Studio

You can download R from the web site of the Comprehensive R Archive Network (CRAN). CRAN updates the installation process from time to time; however, the instructions in this book are for the current steps at time of publication. CRAN provides instructions on the web site if the process has changed.

Begin the download process by going to the web site https://www.r-project.org/ . At the web site, click on the link to choose the CRAN mirror. Choose a mirror near you. Links to the current versions for Windows, Mac OS X, and Linux systems are listed at the top of the window that opens when the mirror link is clicked. Select the appropriate version.

Windows

On the page that opens with the Windows link, select the link base, which is the top link. In the next window, click on the download link for the given Windows version. (Currently, the link is Download R 3.5.1 for Windows.) If R has not already been installed on the computer, the downloader will create a default folder in the Documents folder to hold R files. Unless there is a reason to change the folder name or location, accept the default. R will begin to download.

When the program finishes downloading, find the downloaded file in your file system. Downloads are put in C://Users/User_folder/Downloads, where User_folder is the folder of the user, unless another folder was specified earlier in the installation. Click on the downloaded file, which is an .exe installation file (currently R-3.5.1-win.exe.) A question about the safety of the program may pop up. The installation program is safe, so run the program.

The installation wizard will open. The installation process steps through several pages. On the first page, read the GNU GENERAL PUBLIC LICENSE; then, click on Next. For the rest of the pages, accepting the defaults on each page is fine, so click on Next on each page.

At the page of additional choices, click on Next, and the program will begin to install. When the installation is finished, click on Finish to complete the installation. The program and the 30 base packages are now installed. An icon for R will be on the computer desktop and, for Windows 10, in the start menu. To run R, click on the icon.

Mac OS X

On the page that opens from the Mac OS X link, first read the section under R for Mac OS X. The R project gives the advice to check the files for viruses and other problems.

Under Latest release: the package choice is the current version. Selecting the current version (the .pkg link, currently R-3.5.1.pkg) will download the package. When the packages have finished downloading, open the download folder under the username in Finder.

Select the R version .pkg file in the download folder. Opening the version will open the installer. With the installer open, click on Continue to go to the next page of the installer. Read the message from CRAN; then click Continue. Again, read the message from CRAN; then click Continue.

On the next page, you will find the license. After reading the license, click Agree to download R. On the next page, select either of the choices; then click on Continue. (The Continue button will not light up until a choice is made.)

On the next page, select Install. The installation program will ask for a password. After you have entered a password, the installation will begin. When the installation is finished, click on Close. You will next have the choice of keeping the installation or discarding it. If you keep R, R will be in the applications folder and on the launchpad and the 30 base packages will be loaded. Start R by opening the launchpad and selecting the R icon or by clicking on R in the applications folder.

Linux

At the CRAN site, CRAN provides source code for R for the Linux distributions Debian, openSuse, and Ubuntu. The Debian and Ubuntu distributions have been updated in 2018. The openSuse distribution dates from 2012.

The developers state that R is available through the package management system for most distributions of Linux. Look under GNU R. If the command line version of R is not available using the package management system, installing R directly from the terminal is an option. At http://cran.r-project.org/bin/linux/distribution , where distribution is debian, suse, or ubuntu, you can find instructions for installing R from the terminal command prompt.

The link to Red Hat at the CRAN site goes nowhere.

R Studio

At the R Studio site, R studio provides free source code for R Studio, as well as versions that cost. R Studio is available for Windows, Mac OS X, and the Linux distributions Debian, Red Hat, openSUSE, Ubuntu, and Fedora. To download the free version of R Studio, go to https://www.rstudio.com/products/rstudio/download/ and go to the heading, Installers for Supported Platforms. Click on the link for your operating system and download and run the installer program. Follow the directions of the installer for your operating system. The instructions are similar to those for R.

On the Mac OS X system, the file RStudio-1.1.456.dmg in Filer must be opened each time the computer is booted up in order to have R Studio available in the system.

For Linux, R Studio may be available in the package manager. Search under R Studio.

Installing and Updating Packages

When initially installed, by default R comes with 30 packages. Often the user will want to use the power of the many other packages available in R. Installing and updating a package is straightforward .

Using the command line in R, for any of the operating systems, if the name of a package is known, typing

install.packages("package name")

at the R command prompt, where package name is the name of the package, will install the package. To update packages, typing

update.packages()

at the R command prompt will find those packages with updates and update the packages. To see which packages are already installed on the computer, enter

installed.packages()

at the R prompt.

If the name of the package is not known (also for known names), using the installer for the operating systems Windows and Mac OS X is easy. For Linux, instructions can be found at the CRAN web site, http://cran.r-project.org . Here you can find instructions for Windows and Mac OS X.

Installing and updating packages in R Studio is much easier. How will be given in Chapter 2, when the R Studio windows are described .

Windows

To install a package in Windows not using the command line, start by opening R. On the menu bar at the top of the screen, select Packages. A menu will drop down. Select Install package(s)…. Either the CRAN mirror window or the Packages window will come up. If the CRAN mirror window comes up, select a close mirror and click OK, which will bring up the Packages window.

The Packages window consists of a list of all of the available packages. Scroll down the list to find the package(s) you wish to install and select the package(s). Click on OK to begin the installation. As the installation proceeds, the steps of the installation will scroll on the R console. When the R prompt returns to the screen, the installation is complete.

To update packages not using the command line, select Packages on the menu bar and then select Update packages…. The Packages window to be updated will open, and it will have a list of all of the installed packages with updates. If there are none, the window will be empty. Choose the packages for updating and click on the OK button. If a question about using a personal library pops up, choose Yes. The packages will update. When the R prompt returns to the screen, the updates are complete .

Mac OS X

To install packages in Mac OS X, start by opening R. On the drop-down menu bar at the top of the screen, select Packages & Data. From the drop-down menu, select Package Installer, which brings up the R Package Installer. Click on Get List for a full list of packages or use the Package Search option to search for a package. Under either option, select the package(s) to be installed from the list.

Below the list of packages are choices for the location to put the packages. Hover over the list of location options for more information. Usually, one of the first two options will be correct. To the right of the location options are the Install Selected and Update All buttons. Before clicking on Install Selected, check the Install Dependencies box to make sure that any necessary packages are installed. Click on Install Selected to start the installation process. The selected packages will install.

To update packages, select Packages & Data from the menu bar at the top of the screen. From the drop-down menu, select Package Installer , which opens up the R Package Installer. At the bottom right of the Installer, select Update All and follow instructions.

Updating R

Since CRAN does not provide automatic updates for R, you must update it manually. The processes for Windows and Mac OS X are easy. For the Linux distributions Debian, Suse, and Ubuntu, instructions can be found in the ReadMe files at http://cran.r-project/bin/linux/distribution , where distribution is either Debian, Suse, or Ubuntu.

Windows

The first step in updating R in Windows is to open R and install the package installr if the package has not already been installed. Next, use the function library to provide access to installr. Type

library(installr)

at the command prompt and press enter. Then, to update R, type

updateR()

at the command prompt and press enter. R will either do an update or give a message that the program is up-to-date and return False.

Once installr has been installed, installr does not need to be installed again. The library must be accessed every time R is run.

Mac OS X

The first step in updating R in Mac OS X is to open R and select R from the drop-down menu bar at the top of the page. To run the updater, select Check for R Updates in the drop-down menu under R and follow instructions.

Using R in Separate Folders

Separate workspace images for R can be maintained in separate folders for Windows, Mac OS X, and Linux. This property of R is very handy for using R on separate projects. While the process of opening R in a given folder varies by the operating system, once in a folder, saving the workspace image is straightforward. When closing an R session, the program asks if the user would like to save the workspace image. If Yes is selected, then .RData and .Rhistory (.Rapp.history for Mac OS X) files are saved in the current directory. (For Mac OS X, the files are hidden, but the files are there.)

The .RData file contains the objects that were in R at the beginning of the session plus any objects that were added during the session minus any objects that were erased during the session. The .Rhistory (.Rapp.history for Mac OS X) file contains the history of the lines input at the R console. By default, all lines up to the last 512 lines are saved in Windows. For Mac OS X and Linux, the default is 250 lines. Access to the lines carries over from session to session if the history is saved.

Windows

To initially set up R in a folder, open R at the desktop. (Click on the R icon on the desktop or click on R in the list of programs or, in Windows 10, the Start menu.) Select File on the menu bar at the top of the screen. From the drop-down menu, select Change dir…. The Browse to folder window will open. Navigate to the folder of choice.

When exiting R, save the workspace image and R will create .RData and .Rhistory files in the folder. The .RData file will have a blue R icon associated with the file. In the future, going to the folder and clicking on the R icon will open R, and the history and objects saved within the folder will be present.

As a note for the initial setup, any objects in the desktop R will still be in R when the folder is changed. You can easily remove the objects. Type rm(list=ls()) at the command prompt to remove all objects from the folder .

Mac OS X

For working within different folders in Mac OS X , there are two ways: dragging and dropping or using the terminal. For R in the Applications menu of Finder, if R is not open, dragging the folder in the Documents menu of Finder to the R application will open R in the folder using the .RData and .Rapp.history for that folder. (An image of the R application can be put in the Documents folder to make the dragging easier.)

To open R using the terminal, open the terminal (located under Applications/Utilities in Finder.) and type

open -a R folder

where folder is the location of the folder. Be sure to include the Documents folder in the name and to quote the name. R will open in the folder using the .RData and .Rapp.history files for that folder.

Linux

To open R in a given folder in Linux, change the directory to the folder and type R at the command prompt.

Projects in R Studio

Another way to work with separate projects is by opening new projects in R Studio. Each project has its own name and can be created using the menus in R Studio. The project can be accessed by clicking on the name in the directory where the project is saved. The extension for a project is .Rproj.