Set up R

Create an RStudio project

First of all, let’s create a new RStudio project, to help us keep our data, code and outputs organised. The final step here creates a new script (text file of functions) for us to start typing our functions into:

  1. Open RStudio and go to File > New Project…,
  2. Click on New Directory, and then click on Empty Project,
  3. Type in a directory name: ‘Distance’,
  4. Click the Browse button to choose where to save your Distance folder,
  5. Click Create Project

Create and save a new R script

We’re going to follow standard practice by saving the commands that we want to run in R in a separate file called a script file. We also encourage you to document these commands by using comments: text which is marked by a hash # symbol so that R knows it’s not a command and doesn’t need to be acted on

  1. Go to File > New File > R Script to open up a blank text file in the top left (Source) quadrant of your RStudio window
  2. Go to File > Save and save the script file in a sensible folder, where you will be able to find it later when you want to continue with these course activities, or do your own distance sampling analysis

RStudio

This is what your RStudio window should look like now, with the different panels labelled:

Screenshot of RStudio window with the four quadrants highlighted

Install the unmarked package

Before we can begin running an distance sampling analysis, we need to install the necessary packages in R: unmarked for the distance sampling analysis, and any other packages which are required by unmarked. It’s easy to install packages using RStudio

  1. In RStudio, go to Tools > ‘Install Packages…’. The ‘Install Packages’ dialogue box will open
  2. Begin typing the name of the package ‘unmarked’ that you want to install into the ‘Packages’ box, and RStudio will automatically show you a list of the packages that begin with those letters. Choose the package you want from the drop-down list. If you ever want to install multiple packages in a single step, you can separate the package names with a space or comma
  3. Ensure that the ‘Install dependencies’ box is ticked
  4. Click the Install button and wait for RStudio to finish installing (i.e. wait for the blue arrow > to re-appear in the Console window, bottom left)

Activate the unmarked package

Activate the packages with the library() function

Type the following R commands into your new empty R script file in the Source area

library(unmarked) # activate unmarked package

Remember to add comments!

Add a comment, preceded by #, to each line of code to remind yourself what it does

Run commands in R

When you type commands into your script, they’re not run in R – you’re just saving them as a record of the code you’re writing

For R to process these commands, you need to pass them to the Console window (bottom left quadrant of RStudio). The quickest way is to:

  1. Place your cursor on the first line of code
  2. Hold down Ctrl and press Enter on your keyboard to run that line
  3. Press Ctrl-Enter again to run the second line etc

Run code fragments or chunks

The same approach works to run fragments, or multiple lines - select the code and press Ctrl-Enter

When you see code like this: R function, type or copy-paste it into your script and use Ctrl-Enter to run it in the Console, so that:

  1. You retain clean, commented code in your script in the Source area, uncluttered by the…
  2. statistical output generated in the lower Console area

View help files in R

Remember that at any point you can ask R to show you the help file for a particular function, by typing a question mark followed by its name

RStudio will open the help file in the lower right Plots/Help quadrant

?read.table
?nrow
?table