Appendix

Installing R and RStudio

Before you use R, of course, you first have to install R. Although you can use the built-in code editor, you may want to install an editor with more functionality as well. Because RStudio runs on all platforms and is integrated nicely with R, we also discuss the installation of RStudio on your system.

tip.eps In this appendix, we don’t have enough space to provide installation instructions for every possible operating system. You can find that information on the R and RStudio websites:

check.png R: www.r-project.org

check.png RStudio: www.rstudio.org

tip.eps If you use Linux, depending on the distribution you use, you may find that R comes with the operating system and doesn’t require a separate installation.

Installing and Configuring R

Installing R isn’t difficult, but tweaking it to fit your own needs requires a bit of explanation.

remember.eps We can’t cover all possibilities here, so be sure to read the information on the R website for more insight on how to install and configure the software.

Installing R

You can find the installation files and all necessary information regarding installation on one of the mirror sites of the Comprehensive R Archive Network (CRAN; http://cran.r-project.org). Select the link for your operating system, which will take you to the download site for the latest distribution of R.

You can find detailed installation instructions in the R Installation and Administration manual on CRAN (http://cran.r-project.org/doc/manuals/R-admin.html). For Windows, you take the following steps:

1. Go to CRAN, click Download R for Windows, click Base, and download the installer for the latest R version.

2. Right-click the installer file and select Run as Administrator from the pop-up menu.

3. Select the language to be used during installation.

This doesn’t change the language used by R; all messages and Help files remain in English.

4. Follow the instructions of the installer.

You can safely use the default settings and just keep clicking Next until R starts installing.

tip.eps R exists in a 32-bit and 64-bit architecture. If you have a 64-bit Windows version, you can easily install both architectures next to each other. (The installer will do this by default.) For other systems, you can find more information in the R Installation and Administration manual. The 32-bit version of R is perfectly fine — sometimes it’s even a bit faster than the 64-bit version. You need the 64-bit version only if you require more work memory than the 32-bit version can handle. (On Windows, the maximum is about 3GB for a 32-bit system.)

If you want to be able to personalize your R installation as explained here, you should install R outside the Program Files folder (for example, in C:R). This way, you avoid trouble with the default Windows folder protection.

warning_bomb.eps Mac OS X and Linux users especially need to read the instructions on the CRAN site carefully. R can be installed on all systems, but depending on your version of OS X or Linux, you may need to follow special procedures to install R successfully. Not following these procedures could harm your system.

Configuring R

Apart from accepting the options in the installation procedure, you can change a number of startup options by adapting the Rprofile.site file. This file is located inside the installation directory, in the subfolder …/R-n.n.n/etc (for example, …/R-2.14.1/etc). The file is sourced by R at startup, so all R code in this file is carried out. The default installation of R contains a perfectly valid Rprofile.site file, so you have to change this only if you want to personalize your startup.

Rprofile.site is a normal text file, so you can edit it as you would any other text file. The file already contains some options that are commented out, so you get a good idea of what’s possible when you open the file in a text editor (such as Notepad). Be sure to check the Help page ?options to get more information on all possible options.

You can personalize R further by adding a code file called .Rprofile to your personal home folder. You can find this folder from within R by setting the working directory to “~/”, like this:

> setwd(“~/”)

> getwd()

[1] “C:/Users/Joris FA Meys/Documents

Adding an .Rprofile file isn’t necessary, but R will always look for one, either in the folder from which you call R or in the user’s home directory. Whereas an Rprofile.site file is linked to a specific installation of R, the .Rprofile file can differ for every user on the same system. If you update R, you can leave the .Rprofile file where it is and the new R version will automatically find it and apply the options you specified there. So, after updating R to the latest version, you have to adapt the Rprofile.site again only if you want to personalize it.

technicalstuff.eps Functions you define or objects you create with code in Rprofile.site won’t be visible if you use ls(), although you can use them without trouble. This also means you can’t delete them easily from the workspace.

An Rprofile.site or .Rprofile file may look like the following example:

# Sample profile file

# Set CRAN mirror to a default location

options(repos = “http://cran.uk.r-project.org”)

# R interactive prompt

options(prompt=”R: “)

# sets work directory back to original

go.home <- function() setwd(“D:/MyWorkspace”)

With this file, R will start up with a different prompt (R: instead of >) and set the server from the UK as the default server from which to install packages. You also define the go.home() function, which you can use at any point to set your working directory back to your home directory (D:/MyWorkspace, in this example.

Installing and Configuring RStudio

RStudio is a relatively new and shiny editor for R. It’s our first choice for this book because it’s easy to use, it has a decent Help page, it has very good support, and it incorporates R in a practical way. Of course, you’re free to work with any text editor you like; in Chapter 2, we discuss some alternatives.

Installing RStudio

Installing RStudio is easy. Just follow these steps:

1. Go to www.rstudio.org/download.

2. Click the Download RStudio Desktop button.

3. Select the installation file for your system.

4. Run the installation file.

RStudio will be installed on your system. It normally detects your latest installed R version automatically. If you didn’t do anything funky, you should be able to use R from within RStudio without extra configuration.

Configuring RStudio

You may want to use a different R version from the one RStudio detected. For example, you may want to use R in a 64-bit context. Or RStudio may not have recognized your installation of R. In that case, you can set which R version to use by choosing Tools⇒Options to open the Options pane (see Figure A-1).

To change the R version, click the Change button. Then you can switch between the default 32-bit R installation or the 64-bit R installation (if installed), or you can choose a specific version of R. (RStudio lists all the versions it can find.)

tip.eps If you click Browse, you can select the root directory for any R version you want to use. This folder normally looks something like …/R/R-n.n.n. If you select an R version that has both 32-bit and 64-bit builds, RStudio will ask you which build you want to use.

Figure A-1: The Options pane of RStudio. RStudio may display different default values and directories for the options.

9781119963134-fgaa01.eps

In the Options pane (refer to Figure A-1), you also can tweak the behavior of R in RStudio. If you click the General icon in the left column, you get all the default options for R in RStudio:

check.png Initial working directory: You can set the default working directory R will use at startup.

check.png Save workspace to .RData on exit: Your options are Ask, Never, or Always. By default, RStudio asks you whether you want to save the workspace when you quit.

check.png Restore .RData into workspace at startup: Select this check box to let RStudio restore the workspace automatically at startup. RStudio will look for a saved workspace in the root folder or the default working directory.

check.png Always save history (even when not saving .RData): Select this check box to have RStudio always save the history. If you don’t select this check box, RStudio doesn’t save the history when you exit the program.

check.png Use single global history (rather than per-working directory): Select this check box to have RStudio always save the history in a single global file.

check.png Remove duplicate entries in history: If you select this check box, RStudio removes duplicate entries from your history when the history is saved.

check.png CRAN mirror: Click the Change button to set the CRAN mirror site that RStudio uses to install new packages (see Chapter 3 for more information).

remember.eps These settings work for R only from within RStudio. If you use R with another editor or by itself, the settings in RStudio will have no effect.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset