Figure A-1: The Options pane of RStudio. RStudio may display different default values and directories for the options.
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.
RStudio: www.rstudio.org
Installing and Configuring R
Installing R isn’t difficult, but tweaking it to fit your own needs requires a bit of explanation.
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.
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.
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.
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.)
Figure A-1: The Options pane of RStudio. RStudio may display different default values and directories for the options.
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:
Initial working directory: You can set the default working directory R will use at startup.
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.
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.
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.
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.
Remove duplicate entries in history: If you select this check box, RStudio removes duplicate entries from your history when the history is saved.
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).