This assignment will walk through installing R and RStudio, the basics of the RStudio interface, and how to make a plot from a function. At the end of this assignment you will need to turn in the two plots that you generate in section 5, “Plotting in R.”

1. Downloading R and RStudio

Warning: I am assuming that you are using a Mac or a Windows machine.  If you are using a Chromebook, the installation is different and rather involved.  See https://levente.littvay.hu/chromebook/

First you need to get “R,” so click the following link. Once you are there, go to “download,” then pick a close mirror (“0-cloud” at the top of the list will automatically pick the nearest mirror), then pick your platform (Mac, Windows, or Linux), and then download the “base” package. You can always add contributed packages later if you want/need them.  R goes through rather frequent new versions.  The current (6/16/2023) version is "Beagle Scouts."  Where the heck did that name come from, you ask. R names always come from Peanuts.  My favorite version name was Bunny Wunnies Freak Out.

You can install R and RStudio in "one fell swoop."   OK, we are in the Literatures, Cultures, and Linguistics building, so "one fell swoop" is from Macduff in Shakespeare's Macbeth.   RStudio is a friendlier user interface than the basic R GUI (graphical user interface). Use the link below to download and install both R and RStudio.

Install R & Rstudio

Once “R” and RStudio are installed you can get into the RStudio GUI by clicking on the RStudio icon, shown below (on the right), or if you want to just use R you can click on the icon below (on the left).

One more installation (for Mac OS).  If you are using a Mac you will want to download and install Xquartz from:

Xquartz

2. Working Directory

When you start a session in RStudio you will see four panels. The top left panel is where you will open, read, and write R scripts. The “console” tab of the bottom left panel is where you will run R code. You can either type R commands into the console directly or type them into a file in the top panel, set your cursor on the line you want to run, and then click the “run” button.

The most important thing to do when you begin an RStudio session is to set your working directory. Your working directory is where any files you generate during your session will go. Open a new R script using File>New File>R Script. You should have a blank page in the top left panel. To see what working directory you are using, type the following on line 1 of the empty script, set your cursor on line 1 and click “Run”:

getwd()

The path to your working directory will appear in the console. It is good practice to use a working directory that is specific to whatever project you are working on. To that end, if you have a folder for this class, make and name an empty folder inside of it. Then in RStudio go to Session>Set Working Directory>Choose Directory. This will open your regular file explorer panel. Click the empty folder that you created and click “open”.

Now rerun the getwd() command. You can do this either by marking the command in your script, or typing it directly into the console. You should see the path to your new folder. Save your R script, and it will appear in the working directory.

3. Packages and Help

The bottom right panel has several functions including viewing plots, installing and loading packages, and viewing R help. Packages are collections of functions and datasets that extend the functionality of R. Most are generated by users. To install and load a package in RStudio, navigate to the packages tab in the bottom right panel and click install. In the popup window, type “ggplot2” the name of the package, make sure the “install dependencies” box is checked, and hit enter. Now you should see some red text scrolling past on the console. Once the “>” symbol reappears in the console, the package has finished installing.

To load the package, run the command

library(ggplot2)

This is a package that can be used to make very pretty plots. Once a package is loaded its functions are ready to be used.

To learn more about library run

?library

In the Help tab of the bottom right panel you will see a description of the function, how to run it, and what its arguments are (what things need to be included in the () for the function to run). At the bottom of the help page, there are examples of the function in use.

4. R environment and workspace

The environment tab in the top right panel shows what R objects are in your workspace. Right now it should be empty. Click the save button in the environment tab and save the file to your working directory.

Warning: if you don’t save using a filename all should work well in Windows, but if you save using a filename you will need to explicitly add the extension (e.g., “Anth241.RData”). Now exit as you would any other program. You should see an “R” icon where you saved the workspace. Now you can click on this again and open the workspace in R. Check your working directory using getwd(). You should be in the folder you created.

You can also check what’s in your workspace using ls():

ls() # ls() is the command for listing objects, and anything after a '#' is a comment
character(0)

character(0) is R’s way of telling you that you don’t have anything in your workspace… yet. So let’s get something into your workspace. Select the text below, copy it, and then run it in RStudio.

Fig3.1=function (start=5,stop=50)
{
H=0
for(i in start:stop) H[i-start+1] = 1 - i*(1/i)^2
plot(start:stop,H,type='b',xlab='Number of Alleles',
ylab='Frequency of Heterozygotes')
}

Which in your console will look like so:

> ls()
character(0)
> Fig3.1=function (start=5,stop=50)
+ {
+ H=0
+ for(i in start:stop) H[i-start+1] = 1 - i*(1/i)^2
+ plot(start:stop,H,type='b',xlab='Number of Alleles',
+ ylab='Frequency of Heterozygotes')
+ }
>

The “+” symbol is just R’s way of telling you that it is continuing lines, while the “>” is R’s prompt. When you get to the end of the “pasting” if you see “+ }” as your last line just hit enter (return) and it will take you to the “>” prompt. Now when you do a listing (ls()) you get the following:

ls()
 [1] "Fig3.1"

and if you type out “Fig3.1” (but not “fig3.1” because R is case sensitive) you get:

Fig3.1
function (start=5,stop=50)
{
H=0
for(i in start:stop) H[i-start+1] = 1 - i*(1/i)^2
plot(start:stop,H,type='b',xlab='Number of Alleles',
ylab='Frequency of Heterozygotes')
}

This is a short script, so it is easy enough to see on one screen. For a long script you can scroll, edit in your script file, or you can edit the script to open it in your default text editor. To do this you use:

fix(Fig3.1)

If we were being lazy (and why not?), we could have “scrolled up one” in the history by pushing the up arrow key, which would get us to “Fig3.1” and then we could edit that line to read fix(Fig3.1). Speaking of fix(Fig3.1) this shows how R scripts typically work. fix (but not Fix, remember case sensitive) is just a command that says to fix a file (i.e., edit it). If you type “fix” without parentheses you will get a listing of the command, just as you did when you typed Fig3.1.

5. Plotting in Rstudio

Now, let’s FINALLY do something moderately interesting. Type the following at the “>” prompt:

Fig3.1()

Now you get a version of Figure 3.1 on page 55 of Mielke, Konigsberg, and Relethford appearing in the plots tab of the bottom right panel. Because the plot is showing in just the bottom right corner of the screen, the image is small.  Click on "Zoom" to expand the view. Now click export and you will get the option to save the plot as an image or copy it to your clipboard. Using either option, insert the image into a document. Now let’s try a variant:

Fig3.1(1,3)

Again, copy this and insert it into the same document as the previous one.  Be sure to turn in your document file to Canvas.  Please use a file format that is readable on a Windows or MAC machine (Word *.docx, Word *.doc, or Adobe *.pdf).  If you do not have MS Word doc you can get it from :https://webstore.illinois.edu/shop/product.aspx?zpid=2816.  I cannot open native MAC formats like *.pages.  Sorry!  I have set Canvas so that it will only accept files in the three listed formats.  Some people use Google Docs.  If this works for you then that's fine, but as Google Docs operates in the "cloud," it is easy to get lost in the clouds.  The simplest way to do the homeworks is to download the *.html file so you can work off-line,create an empty new Word doc, and then paste results and write answers to the Word doc. 

OK, let's go (almost) totally graphic.  When you have opened RStudio, go up to the menu line and select "File" and then "New Project"

Menu 1

This will open the "New Project Wizard."  Select "New Directory" (this will make a new folder for your R stuff).

Menu 2

Select "New Directory" (this will make a new folder for your R stuff).

Menu 3

Type a directory name (folder name) in the box.  I'm using Windows, so the default location is my Desktop.

Menu 4


Menu 5

I typed "Anth 241 Homework" (a logical enough name).  I am using a Windows laptop so the default location is on the "Desktop."  If all goes as planned, you will see one panel on the left and two on the right.  Click on the blue-circled icon to open a second panel on the left.

Menu 6
In the upper left panel paste a copy of the script for the zeroth homework and select the blue circled icon "Source"

Menu 7

Notice how the upper right panel ("Environment"), which was empty before, now has a function called Fig3.1.  Now be darn sure you are in the bottom left panel and type "Fig3.1()"

Menu 8

Two things.  First, R is case sensitive, so if you type fig3.1 that will not work.  Second, you will see text in yellow that is giving you the default arguments.  After you make this run you will run Fig3.1(1,3).


Menu 9


In the bottom right panel you will see a plot, but it will be rather small and "scrunchy," so click on the (blue circled) "Zoom" icon.

Menu 10

If you right click on the image you will get a menu from whence you can save the image or copy it to the clipboard and then paste into a Word document.  This is on a Windows machine.  On a Mac, this probably works a little differently.

Menu 11