Overview

Cover imageThis book is intended to be a first course in R programming for natural resource professionals. It is by no means comprehensive (no book about R ever could be), but instead attempts to introduce the main topics needed to get a beginner up and running with applying R to their own work. It is intended to be a companion to in-person workshop sessions, in which each chapter is covered in a 2 hour session, however it can be used as “self-teach” manual as well. Although the examples shown have a natural resource/ecological theme, the general skills presented are general to R users across all scientific disciplines.

What is Covered?

The book is composed of six chapters intended to cover a suite of topics in introductory R programming. In general, the material builds in complexity from chapter to chapter and earlier chapters can be seen as prerequisites for later chapters.

  • Chapter 1 covers the basics of working in R through RStudio, including the basics of the R coding language and environment.
  • Chapter 2 covers the basics of plotting using the base R graphics functionality.
  • Chapter 3 covers the basics of fitting statistical models using built-in functionality for generalized linear models as well as non-linear models.
  • Chapter 4 covers the basics of simulation modeling in R.
  • Chapter 5 covers the basics of the {dplyr} and {reshape2} packages for manipulating and summarizing large data sets using highly readable code.
  • Chapter 6 covers the basics of producing maps and performing spatial analysis in R. This chapter was contributed by Henry Hershey

Prerequisites

Chapter 1 starts at the first step (installing R) and progresses by assuming no prior knowledge of programming in R or in any other language. In the later chapters, e.g., Chapters 3 and 4, an understanding of statistics at the introductory undergraduate level would be helpful but not strictly essential.

There are, however, some tasks you’ll need to complete before using this book, which are described in the two sections that follow.

Prepare Your Computer

Installation

First off, you will need to get R and RStudio1 onto your computer. Go to:

Download the appropriate installation file for your operating system and run that file. All default settings should be fine.

Optional Configuration

As a matter of personal preference, you are recommended to configure a few settings. Open up RStudio and go to Tools > Global Options, and in the section listed “General”:

  • Make sure Restore .RData into workspace at startup is unchecked
  • Make sure Save workspace to .RData on exit is set to Never
  • Make sure Always save history (even when not saving .RData) is unchecked.

These settings will prevent you from getting a bunch of useless files and dialog boxes every time you open and close R.

Create a Book Directory

You should create a devoted folder on your computer for this book. All examples will assume this folder is located here: C:/Users/YOU/Documents/R-Book. Change YOU to be specific for your computer.

Data Sets

The data sets2 used in this book are hosted on a GitHub repository maintained by the author. It is located here: https://github.com/bstaton1/au-r-workshop-data.

To acquire the data for this book, you should:

  1. Navigate to the GitHub repository
  2. click the green Clone or download button at the top right,
  3. click Download ZIP
  4. unzip the contents of this folder into the location: C:/Users/YOU/Documents/R-Book/Data

File organization will be very important for your success in learning to use R. This book will assume your R-Book directory is organized as shown below. Notice that there is a separate folder for the data downloaded from GitHub as well as one for each chapter that will house the R code for that chapter. Do not worry about making all of these folders now, you will do this at the appropriate time as you work your way through this book. For now, just make sure there is a Data folder that contains all of the unzipped contents from the GitHub repository your main R-Book directory.

##          levelName
## 1  R-Book         
## 2   ¦--Data       
## 3   ¦   ¦--asl.csv
## 4   ¦   ¦--...    
## 5   ¦   °--Ch6    
## 6   ¦--Chapter1   
## 7   ¦   ¦--Ch1.R  
## 8   ¦   ¦--Ex1A.R 
## 9   ¦   °--Ex1B.R 
## 10  ¦--Chapter2   
## 11  ¦   ¦--Ch2.R  
## 12  ¦   °--Ex2.R  
## 13  ¦--Chapter3   
## 14  ¦--Chapter4   
## 15  ¦--Chapter5   
## 16  °--Chapter6

Exercises

Following each chapter, there is a set of exercises. You should attempt and complete them, as they give you an opportunity to practice what you learned while reading and typing along. Solutions are provided at the end of this book, however you are strongly recommended to attempt to figure the problems out on your own before looking to how the author would solve them.

Some exercises have bonus questions. These are intended to challenge you with some of the more difficult tasks shown in the chapter or ask you to extend what you learned to a completely different problem. If you can get all of the non-bonus questions without looking at the solutions too much, you can consider yourself to have good understanding of that chapter’s material. If you can complete the bonus questions with little or no help, that means you have mastered that chapter’s material!

Text Conventions

  • Regular text: a description of what you you should do, how some code works, or a general narrative of something.
  • monospace: references something in R
    • this() references some function
    • this references some other object
    • {this} references an R package
    • C:/This is a file path
  • Bold is intended to provide more emphasis to a word or topic. In general, new topics are introduced this way.
  • Links: this is a link to some other location in this book. External links are provided with a full URL.
  • \(Equations\): it is sometimes useful to describe concepts mathematically before showing how to do it in R.
  • 3: a footnote containing more information.

Keyboard Shortcuts

Several parts of this book in this book make reference to keyboard shortcuts. They are never necessary, but can help you be more efficient if you commit them to muscle memory. This book assumes you are using a PC for the keyboard shortcuts. If you are using a Mac, they will be different4. For a complete list of RStudio’s keyboard shortcuts specific to your operating system, go to Help > Keyboard Shortcuts Help.

Development of this Book

This book represents the third reincarnation of the Auburn R Workshop Series. The first version was written in Fall 2014 using Microsoft Word, but the author found that making even small changes was clunky - each change to code in the document required a copy-paste of code and output from R to Word. Individual session materials (i.e., handout, exercises, solutions, data) were created in separate documents, saved as PDFs and .xlsx files, and uploaded to a wordpress webpage.

The second version was written through R (R Core Team 2018) and RStudio using the R packages {rmarkdown} (Allaire et al. 2018) and {knitr} (Xie 2018b, 2015), which allowed the integration of text, code, and output all into one output file. This version was completed in Fall 2015. Like the first version, individual session materials were created in separate documents, and replaced those previously found on the wordpress site.

This third version was written through R and RStudio but used the R package {bookdown} (Xie 2018a) which allowed for the individual sessions to be combined into one “book” by turning each session into a chapter. This facilitated cross-references to topics covered in previous chapters and allows the reader to only refer to one location when trying to remember how to use a skill. It also allowed for multiple formats to be published including both HTML and PDF versions.

The book is hosted on GitHub Pages, and was last built on 08-22-2019.

About the Author

Ben Staton is a PhD candidate in the School of Fisheries at Auburn University. He studies quantitative methods for assessing fish populations for use in harvest management, with a focus on Pacific salmon in western Alaska. His interests are in population dynamics, Bayesian methods, Monte Carlo methods, and reproducible research. Ben has been using R on a daily basis since the beginning of his graduate work in 2014, and is enthusiastic about helping others learn to use R for their own work.

References

Allaire, JJ, Yihui Xie, Jonathan McPherson, Javier Luraschi, Kevin Ushey, Aron Atkins, Hadley Wickham, Joe Cheng, and Winston Chang. 2018. Rmarkdown: Dynamic Documents for R. https://CRAN.R-project.org/package=rmarkdown.

R Core Team. 2018. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.

Xie, Yihui. 2015. Dynamic Documents with R and Knitr. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC. http://yihui.name/knitr/.

Xie, Yihui. 2018a. Bookdown: Authoring Books and Technical Documents with R Markdown. https://CRAN.R-project.org/package=bookdown.

Xie, Yihui. 2018b. Knitr: A General-Purpose Package for Dynamic Report Generation in R. https://CRAN.R-project.org/package=knitr.


  1. While it is possible to run R on its own, it is clunky. You are strongly advised to use the RStudio IDE (integrated development environment) given its compactness, neat features, code tools (like syntax and parentheses highlighting). This workshop will assume you are using RStudio

  2. Many of the data sets used in this book were simulated by the author. Cases in which the data set used was not simulated are noted and a citation to the data source is provided. More details on the individual data sets can be found on the GitHub repository.

  3. This is a footnote. If you’re viewing this on GitHub Pages, click the arrow to the right to return to the text

  4. For some keyboard shortcuts, you may just need to swap out the CTRL keystroke for the CMD keystroke for a Mac computer