|Primary Contact(s)||Created||Required Software||Example Datafile|
|Rich Glor||7 March 2009||R||See Introduction|
A. Installing R and Associated Packages for Phylogenetics
Begin by downloading and installing the basic R framework from http://cran.r-project.org/. After you’ve done this, you’ll need to open R and install a number of phylogenetic-specific applications using R’s interactive installation function. Doing this will require that you’re logged onto the internet. We’re going to install four packagess: APE (Paradis 2004) and GEIGER (Harmon et al. 2007), Ade4, and Picante.
1. Open R by double-clicking the application icon.
2. Type the following line after the R command prompt ‘>’:
Quotes around the package name are required, if you don’t have them you’ll get an “object not found” message. If you are a new user of R you may be prompted to select a preferred location for this and subsequent downloads. If all goes well, a few lines should appear on your screen, ending with one telling you “The downloaded packages are in...”
3. Repeat step 2 for each of the packages listed above.
Note that an alternate way to install R packages for phylogenetics is to use the phylogenetics task view:
This will not work well at Bodega, where the internet speed is very slow.
Before you use the packages you just installed, you’ll need to load them into R. You need to do this each time you open R and want to make use of the package. If you don’t do this, you’re likely to get an error telling you that commands from that package cannot be found. To load a package you need to use the library command:
NOTE: ape is not surrounded with quotes in this case. Learning when to use or not use quotes around a package or file name is one of the most confusing aspects of R and not something that can be expressed as a simple rule.
4. Proceed to step B after all of the packages have been installed and loaded.
B. Basic (Non-Phylogenetic) Functions in R
The great thing about R is that many basic functions are already built in. I’m not going to review all of the basic R functions here, but I do want you to start getting an idea of how R works. There are lots of other places to find more detailed information about basic R functions (e.g., Ch. 2 of Paradis 2006).
Let’s begin with some basic math. If you type ‘8+1’ at the command prompt and hit return you should get the answer ‘9‘ from R. Try experimenting with some other basic mathematical functions like multiplication (*) and division (/).
Now let’s try to save the output of simple functions in R. This can be done with the ‘assign’ commands in R, which consist of a dash (-) plus a greater than (>) or less than sign (<). Together these characters can be used to make arrows (‘->’ or ‘<-‘) that point from the operation whose output you’d like to save (e.g., 8+1) to the place you’d like to save it (e.g., x). Try typing:
To confirm that the value ‘9’ has been saved to ‘x’, just type ‘x’ and hit return. The use of assignment functions will be among the most commonly used functions of R’s syntax, so you should get familiar with how to use them now.
C. Commenting in R
Before we go any further, there is one other basic command to know about R; the number sign (#) is used to indicate comments in R’s syntax. Those of you with programming experience will know that comments are an important part of any programming language and can be used to tell the language to formally ignore text that appears following the comment indicator. Thus, text appearing after a number sign is ignored by R. Try typing:
>8+1 #Testing whether basic addition works in R
R should give you the same result as typing just ‘8+1’. Comments will become increasingly important as you learn to program in R because they allow you make notes to yourself and others that are embedded in your R scripts.
D. Getting Help in R
Every R package has help documentation. These help documents should be the first place you turn when you run into trouble. Learning how to use them should be one of the first things you do in R. I’ll warn you right now, R help documents can be difficult to interpret and, in many cases, won’t provide you with the help you’re looking for, but this is no excuse not to check them. As you gain experience with R, you’ll find the help functions increasingly useful.
Let’s say you’d like to get help with the command for reading trees into R. If you already know that this function is called read.tree there are two simple commands you can use to get help:
When you type either of these commands a new window should pop up with some basic information about the function in question.
If you don’t know the name of the command, but have some idea of what it’s intended to do, you can use the search available help documentation by typing:
Note that quotes are required in this case. Use of the help search function should give you a list of all the commands that contain the word tree in their name or in the associated help file.
If you want help figuring out what a particular package does, you can use the following line to get a list of all the functions offered by a particular package:
Before you ask anyone for help with R be sure you’ve checked the help file. Another good way to get help is to use Google to search through the many internet help forums. If you type ‘R read.tree’ into Google, for example, you should find a number of helpful documents and discussions.