Introduction

This exercise is provided to make sure that everybody is on the same page before we begin our coursework. Some of the content in this lab may be review for you, while some is likely to be new material.

Even though you may have seen some of this before, please glance through the contents and work through any of the material that is rusty or unfamiliar.

We will use R, a free and open-source statistical programming language, for all of our coursework.

We will also use many of the datasets accompanying the main text for the course Spatial Ecology and Conservation Modeling, Fletcher and Fortin 2018.

Objectives

The objectives of this preliminary lab, are to:
  1. Review of filesystem paths
  2. Install R and RStudio
  3. Create an RProject for the course labs
  4. Install git and create a github account
  5. Verify our course project setup
  6. Install spatial R packages
  7. Obtain datasets for the lab exercises

Files and paths

Since our analyses will require coordinating the contents and locations of multiple files, you need to be very comfortable with the concepts of files, directories, absolute paths, and relative paths.

I’m not going to provide materials in class since there are many good tutorials about the topics, such as this one that discusses paths in the context of HTML.

There are many operating system-specific tutorials on the details of the filesystems used on different operating systems. There are more similarities than differences, and I will leave it up to you to educate yourself.

Note that the characters for file separators may differ on different operating systems.

R is pretty good at guessing the correct symbol. For the most part you will be safe to use “/” in your code, but keep in mind the possibility of specifying the wrong file separator in your future debugging endeavors.

Before you continue make sure you feel comfortable with absolute and relative paths. If you are clear on these concepts, you can save hours of frustration when you need to import data files or save data and figures.

self-assessment question: - Can you explain why using relative paths makes your code much easier to share?

Course Software, Data, and Directory Setup

You’ll need to work through the steps below to set up:

  1. Create a GitHub account.
  2. Create a course git repository
  3. Install GitHub Desktop
  4. Clone your repository
  5. R and RStudio
  6. Course RProject
  7. Install R Packages
  8. Download and prepare the text supplemental data

1 GitHub Account

  • Navigate to github.com and create a free account. You should choose a professional-sounding username.

Once you have your account set up, follow me: michaelfrancenelson.

  • Take a screenshot of your GitHub.com profile page.

2 Course Git Repository

ON github.com, create a new repository:

Your should name your repository spatial_data_in_r.

Select the Private Repo option and create a .gitignore file.

Click the dropdown under the Add .gitignore and type R to search for the R template .gitignore file.

Take a screenshot of your repository page.

3 GitHub Desktop

Navigate to https://desktop.github.com/ and follow the instructions to install GhtHub Desktop on your computer.

4 Clone Your Repository

In GitHub Desktop, input your GitHub credentials so that you can access your account through the application.

Next, clone the course repository you just created on github.com. Remember the directory you clone your repository into.

5 R and RStudio

You can ignore this tab if you already have R and RStudio on your computer.

If you don’t already have R installed on your computer, you will need to install it before you can set up RStudio. R is free and open-source statistical programming language. Many powerful packages for dealing with spatial data have been developed in recent years, making R a great tool for spatial analysis!

To We’ll use RStudio, an Integrated Development Environment (IDE) specialized for use with the R language.

You can find download links on the RStudio page.

Select the free version appropriate for your operating system (Windows, Mac OS, Linux).

6 Course RProject

It’s convenient to store all of the files you use for a project in a common location. RStudio has a convenient functionality for this: the use of RProjects. An RProject a way to organize a group of scripts, data, and other files related to a single project.

Course Directory

Use the file browser on your computer to navigate to the directory where you cloned your repository.

Within this directory, create the following two subdirectories:

  • data
  • labs

Make sure you remember the the location of your course R directory!

At this point, your course folder should look something like this:

Create an RProject

Now, within RStudio create a new RProject:

Step 1: Create Project Wizard

Select the Existing Directory option:

Step 2: Choose Your Project Location

Next you should:

  1. Browse for the course folder you just created.
  2. Check the Open in new session box.
  3. Click Create Project.

This process will create some new files and a new folder in your course directory. It should now look something like this:

Note the .RProj file.

Step 3: Set Project Options

In an effort to be helpful, by default RStudio will make a copy of all the objects you have in memory when you quit the program.

It turns out that this is very helpful for creating hard-to-debug issues in your code.

Fortunately, you can disable this option, either for the entire program, or for specific RProjects.

Look in the Tools menu within RStudio to find the global and project options. Be sure to changes the settings so that RStudio does not save the workspace to a file called .RData when you exit, and that it does not restore your workspace from a .RData file when you start the program.

The correct options should look like this:

Step 4: Take a Screenshot

At this point, you should have an RStudio session running with your RProject open.

On my computer, my RStudio session looks like:

Verify that your RStudio session shows the open RProject (in the upper right) and that you can see the two subdirectories and the RProj file in the file browser.

Take a screenshot! You’ll need it for the lab report.

7 R Packages

We’ll be using lots of packages to deal with spatial data and perform spatial analyses.

To get started, install the following using install.packages()

  • here
  • ggplot2
  • raster
  • ncdf4
  • sp
  • rgdal
  • rgeos
  • sf

To verify that the installations were successful you can run the following block of test code:

require(here)
require(ggplot2)
require(raster)
require(ncdf4)
require(sp)
require(sf)
require(rgdal)
require(rgeos)

8 Companion data for Fletcher and Fortin

Download the data

Note: You have to download the data for the Fletcher and Fortin textbook. You can find the file here:

https://ufdc.ufl.edu/IR00010770/00001

Unzip the archive and move the unzipped folder to the data subfolder of your class project folder.

Your data subfolder should now look like this:

Process the NLCD Raster

Now that we’ve got the book data unzipped and in-place, we need to process one of the raster files so that it will be accepted as an upload on GitHub.

If you’ve managed to install the R packages above, and you have the data in the correct location, you can run the following code block to process the raster. For now, don’t worry about what the code is doing, this is just a pre-course setup step.

This raster processing step may take quite a while to complete. The process took about 10 minutes on my computer.

require(raster)
require(here)

# Read the raster data
nlcd_r = raster(here(
  "data",
  "Fletcher_Fortin-2018-Supporting_Files",
  "data",
  "nlcd2011SE"))

# Re-write it in a compressed NetCDF file
writeRaster(
  nlcd_r, 
  filename = here(
    "data",
    "Fletcher_Fortin-2018-Supporting_Files",
    "data",
    "nlcd2011SE.nc"),
  compression = 8)

Verify the Raster

Now, you need to double check that the raster was properly written to the file.

  • In a file browser on your computer, navigate to the data subfolder of the book supplemental materials folder.

You should see a file called nlcd2011SE.nc:

Zip the Old Raster

Almost ready!

Now you need to compress the folder called nlcd2011SE, saving at as a zip file. When you’re finished you should see both the nlcd2011SE.nc file and a new file called nlcd2011SE.zip:

  • After you’ve verified that you have the two files, delete the folder called nlcd2011SE.
  • Take a screenshot of the folder showing both the .nc and .zip files.

Report

Create a document and include the following screenshots:

  1. GitHub Profile Page
  2. GitHub course repository
  3. RStudio session showing the open RProject.
  4. File browser window showing the processed NLCD raster .nc and .zip files.